Hi everyone -
Im trying to change a name associated with a file using SetNamedSecurityInfo. The parameters Im passing in are C:\temp\test.txt for path and "tommy boy" for name. I created an accoun on my Windows XP Professional and created the test.txt file using my admin account. When i display the security information it does show that its mine. When I try and change the account name that is associated with the test.txt file it gives me an error code of 87 at dRtnErrorCode with SetNamedSecurityInfo.... anyone have any idea?
int setOwner(char* path, char* name){
DWORD dwRtnCode = 0;
LPTSTR pSid, DomainName;
DWORD dwSid = 1, dwDomainName = 1;
BOOL bRtnBool = TRUE;
LPTSTR fileName = path;
LPCTSTR acctName = name;
SID_NAME_USE eUse = SidTypeUnknown;
// Get Buffer Size for LookupAccountName
bRtnBool = LookupAccountName(
NULL,
acctName,
(PSID) &pSid,
(LPDWORD) &dwSid,
(LPTSTR) &DomainName,
(LPDWORD) &dwDomainName,
&eUse);
// Reallocate memory for the buffers.
pSid = (char *)GlobalAlloc(
GMEM_FIXED,
dwSid);
// Check GetLastError for GlobalAlloc error condition.
if (pSid == NULL) {
DWORD dwErrorCode = 0;
dwErrorCode = GetLastError();
_tprintf(TEXT("GlobalAlloc error = %d\n"), dwErrorCode);
return -1;
}
DomainName = (char *)GlobalAlloc(
GMEM_FIXED,
dwDomainName);
// Check GetLastError for GlobalAlloc error condition.
if (DomainName == NULL) {
DWORD dwErrorCode = 0;
dwErrorCode = GetLastError();
_tprintf(TEXT("GlobalAlloc error = %d\n"), dwErrorCode);
return -1;
}
// Second call to LookupAccountName to get the account name.
bRtnBool = LookupAccountName(
NULL, // name of local or remote computer
acctName, // name of account
pSid, // sid buffer
(LPDWORD)&dwSid, // size of sid buffer
DomainName, // domain name
(LPDWORD)&dwDomainName, // size of domain name buffer
&eUse); // SID type
// Check GetLastError for LookupAccountName error condition.
if (bRtnBool == FALSE) {
DWORD dwErrorCode = 0;
dwErrorCode = GetLastError();
if (dwErrorCode == ERROR_NONE_MAPPED)
_tprintf(TEXT("SID not found for specified Account Name.\n"));
else
_tprintf(TEXT("Error in LookupAccountName.\n"));
return -1;
} else if (bRtnBool == TRUE){
// Set the owner SID of the file.
dwRtnCode = SetNamedSecurityInfo(
fileName,
SE_FILE_OBJECT,
OWNER_SECURITY_INFORMATION,
&pSid,
NULL,
NULL,
NULL);
// Check GetLastError for SetNamedSecurityInfo error condition.
if (dwRtnCode != ERROR_SUCCESS) {
DWORD dwErrorCode = 0;
dwErrorCode = GetLastError();
_tprintf(TEXT("SetNamedSecurityInfo error = %d\n"), dwErrorCode);
return -1;
}
// Print the account name.
displayOwner(path);
}
return 0;
}
Im trying to change a name associated with a file using SetNamedSecurityInfo. The parameters Im passing in are C:\temp\test.txt for path and "tommy boy" for name. I created an accoun on my Windows XP Professional and created the test.txt file using my admin account. When i display the security information it does show that its mine. When I try and change the account name that is associated with the test.txt file it gives me an error code of 87 at dRtnErrorCode with SetNamedSecurityInfo.... anyone have any idea?
int setOwner(char* path, char* name){
DWORD dwRtnCode = 0;
LPTSTR pSid, DomainName;
DWORD dwSid = 1, dwDomainName = 1;
BOOL bRtnBool = TRUE;
LPTSTR fileName = path;
LPCTSTR acctName = name;
SID_NAME_USE eUse = SidTypeUnknown;
// Get Buffer Size for LookupAccountName
bRtnBool = LookupAccountName(
NULL,
acctName,
(PSID) &pSid,
(LPDWORD) &dwSid,
(LPTSTR) &DomainName,
(LPDWORD) &dwDomainName,
&eUse);
// Reallocate memory for the buffers.
pSid = (char *)GlobalAlloc(
GMEM_FIXED,
dwSid);
// Check GetLastError for GlobalAlloc error condition.
if (pSid == NULL) {
DWORD dwErrorCode = 0;
dwErrorCode = GetLastError();
_tprintf(TEXT("GlobalAlloc error = %d\n"), dwErrorCode);
return -1;
}
DomainName = (char *)GlobalAlloc(
GMEM_FIXED,
dwDomainName);
// Check GetLastError for GlobalAlloc error condition.
if (DomainName == NULL) {
DWORD dwErrorCode = 0;
dwErrorCode = GetLastError();
_tprintf(TEXT("GlobalAlloc error = %d\n"), dwErrorCode);
return -1;
}
// Second call to LookupAccountName to get the account name.
bRtnBool = LookupAccountName(
NULL, // name of local or remote computer
acctName, // name of account
pSid, // sid buffer
(LPDWORD)&dwSid, // size of sid buffer
DomainName, // domain name
(LPDWORD)&dwDomainName, // size of domain name buffer
&eUse); // SID type
// Check GetLastError for LookupAccountName error condition.
if (bRtnBool == FALSE) {
DWORD dwErrorCode = 0;
dwErrorCode = GetLastError();
if (dwErrorCode == ERROR_NONE_MAPPED)
_tprintf(TEXT("SID not found for specified Account Name.\n"));
else
_tprintf(TEXT("Error in LookupAccountName.\n"));
return -1;
} else if (bRtnBool == TRUE){
// Set the owner SID of the file.
dwRtnCode = SetNamedSecurityInfo(
fileName,
SE_FILE_OBJECT,
OWNER_SECURITY_INFORMATION,
&pSid,
NULL,
NULL,
NULL);
// Check GetLastError for SetNamedSecurityInfo error condition.
if (dwRtnCode != ERROR_SUCCESS) {
DWORD dwErrorCode = 0;
dwErrorCode = GetLastError();
_tprintf(TEXT("SetNamedSecurityInfo error = %d\n"), dwErrorCode);
return -1;
}
// Print the account name.
displayOwner(path);
}
return 0;
}