ReferencedDomainName 参数实际上不是可选的。
LPCTSTR machine = NULL, username = /*TEXT("Anders")*/ argv[1];
TCHAR domain[MAX_PATH];
BYTE accountSIDbuf[SECURITY_MAX_SID_SIZE];
PSID accountSID = (PSID) accountSIDbuf;
DWORD cbSid = SECURITY_MAX_SID_SIZE, cchRD = MAX_PATH;
SID_NAME_USE snu;
if (!LookupAccountName(machine, username, accountSID, &cbSid, domain, &cchRD, &snu))
{
printf("Error %u\n", GetLastError());
return ;
}
LPTSTR sidstr;
if (!ConvertSidToStringSid(accountSID, &sidstr)) { return ; }
_tprintf(_T("SID of %s\\%s is %s\n"), domain, username, sidstr);
LocalFree(sidstr);
您的代码的另一个问题是ShowError(GetLastError());
你不能使用GetLastError()
调用其他函数后。重写为
DWORD error = GetLastError();
fwprintf(stderr, L"Error getting SID from name: \n");
ShowError(error);
但在这种情况下,即使这样也是错误的,因为NetLocalGroupAddMembers
不打电话SetLastError
,它只是直接返回错误代码。
Edit:
只是为了阐明参数用法;如果你想查询域缓冲区所需的大小,你可以这样做:
DWORD cchRD = 0;
LookupAccountName(..., NULL, &cchRD, &snu); // The function is still going to report failure
LPTSTR domain = malloc(cchRD * sizeof(*domain));
LookupAccountName(..., domain, &cchRD, &snu);
在我的示例中,我通过传入“足够大”的缓冲区来避免这种情况。