tags:

views:

175

answers:

1

I've been banging my head...I can't pretend to be a C++ guy...

    TCHAR * pszUserName = userName.GetBuffer();
SID sid;
SecureZeroMemory(&sid, sizeof(sid));
SID_NAME_USE sidNameUse;
DWORD cbSid = sizeof(sid);

pLog->Log(_T("Getting the SID for user [%s]"), 1, userName);

if (!LookupAccountName(NULL, (LPSTR)pszUserName, &sid, &cbSid, NULL, 0, &sidNameUse))
{
    pLog->Log(_T("Failed to look up user SID. Error code: %d"),1,  GetLastError());
    return _T("");
}

pLog->Log(_T("Converting binary SID to string SID"));

The message 'Getting the SID for user [x] is written' but then the app crashes. I'm assuming is was the LookupAccountName call.

EDIT:

Whoops userName is a MFC CString

+4  A: 

Parameter 6 (cchReferencedDomainName) should point to a DWORD. When the documentation says, "if the ReferencedDomainName parameter is NULL, this parameter must be zero," I believe they mean that the referenced DWORD must be 0.

Try adding:

DWORD cchReferencedDomainName = 0;
if (!LookupAccountName(NULL, (LPSTR)pszUserName, &sid, &cbSid, NULL, &cchReferencedDomainName, &sidNameUse))
...
Peter Ruderman
Also note that the documentation says "If [the ReferencedDomainName parameter] is NULL, the function returns the required buffer size." As a result, I'm not sure this is doing what you expect -- it may just be looking up the size of the buffer you need to allocate and not populating the SID.
Nick Meyer
That was it! Misread the documentation.
Adam Driscoll
@Nick - I'll verify this. Thanks for pointing that out.
Adam Driscoll
@Nick - You were right I was using this incorrectly. I should pay better attention to the API docs...
Adam Driscoll