为什么会崩溃?

时间:2010-04-06 21:08:42

标签: c++ winapi

我一直在敲我的脑袋......我不能假装成一个C ++家伙......

    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"));

消息'为用户创建SID [x]已写入'但随后应用程序崩溃。我假设是LookupAccountName电话。

编辑:

Whoops userName是MFC CString

1 个答案:

答案 0 :(得分:7)

参数6(cchReferencedDomainName)应指向DWORD。当文档说“如果ReferencedDomainName参数为NULL,则此参数必须为零”,我认为它们意味着引用的DWORD必须为0.

尝试添加:

DWORD cchReferencedDomainName = 0;
if (!LookupAccountName(NULL, (LPSTR)pszUserName, &sid, &cbSid, NULL, &cchReferencedDomainName, &sidNameUse))
...