I'm not a C++ developer, so I guess it's just my fault that my program doesn't work. I want to look up the SID for a windows group and return a readable SID.
wchar_t* SpcLookupName(LPCTSTR lpszSystemName, LPCTSTR lpszAccountName) {
PSID Sid;
DWORD cbReferencedDomainName, cbSid;
LPTSTR ReferencedDomainName;
SID_NAME_USE eUse;
cbReferencedDomainName = cbSid = 0;
if (LookupAccountName(lpszSystemName, lpszAccountName, 0, &cbSid,
0, &cbReferencedDomainName, &eUse)) {
SetLastError(ERROR_NONE_MAPPED);
return 0;
}
if (GetLastError( ) != ERROR_INSUFFICIENT_BUFFER) return 0;
if (!(Sid = (PSID)LocalAlloc(LMEM_FIXED, cbSid))) return 0;
ReferencedDomainName = (LPTSTR)LocalAlloc(LMEM_FIXED, cbReferencedDomainName);
if (!ReferencedDomainName) {
LocalFree(Sid);
return 0;
}
if (!LookupAccountName(lpszSystemName, lpszAccountName, Sid, &cbSid,
ReferencedDomainName, &cbReferencedDomainName, &eUse)) {
LocalFree(ReferencedDomainName);
LocalFree(Sid);
return 0;
}
wchar_t* psz;
// Loading ConvertSidToStringSid
typedef BOOL (WINAPI *tConvertSidToStringSid)(PSID,wchar_t*);
tConvertSidToStringSid pConvertSidToStringSid=0;
HINSTANCE handle = ::LoadLibrary("Advapi32.dll");
pConvertSidToStringSid = (tConvertSidToStringSid) ::GetProcAddress(handle, "ConvertSidToStringSidA");
if(pConvertSidToStringSid(Sid, psz)){
return psz;
}
}
My problem is that the function returns only some strange characters and not the SID, why?
There are a couple of obvious things wrong with your code...
1) the prototype for
ConvertSidToStringSid()
isBOOL ConvertSidToStringSid(PSID Sid, LPTSTR *StringSid)
; which means that your typedef should betypedef BOOL (WINAPI *tConvertSidToStringSid)(PSID,LPTSTR *);
2) you are looking up the ANSI version and passing a wchar_t *... You should either look up the wide character version
ConvertSidToStringSid()
or, a better solution, IMHO, look upConvertSidToStringSid
and use aTCHAR *
rather than awchar_t *
as this will work no matter what your unicode compile settings are, and it matches the typedef.3) finally due to the broken typedef you're being allowed to pass the wrong data type to the function. It wants a
LPTSTR *
and you're passing it aLPTSTR
(well, actually you're passing awchar_t *
... TheLPTSTR
is in fact the unicode aware type that maps to awchar_t *
if you're using wide characters but the function that you are calling wants a pointer to that pointer and not the pointer itself.So, the fixed code is:
Note the change to the typedef, the change to the GetProcAddress call (where we use the # to stringize the function name (which will either be ConvertSidToStringSidA or ConvertSidToStringSidW depending on your unicode settings) and the change to pass the address of psz rather than the pointer itself.
And now your only problem is that you're potentially leaking that psz unless you are making sure you free it in the caller with a call to
LocalFree()
.Note that depending on the calling code, you might want to convert the typedef into
wchar_t **
rather thanLPTSTR *
and then use the ConvertSidToStringSidW version to force the result to always be a wide character string.