windows 获取当前用户的sid

1)cmd命令查询

windows 获取当前用户的sid_第1张图片

2)代码获取

char userName[MAX_NAME] = "";
char sid[MAX_NAME] = "";
DWORD nameSize = sizeof(userName);
GetUserName((LPWSTR)userName, &nameSize);


char userSID[MAX_NAME] = "";
char userDomain[MAX_NAME] = "";
DWORD sidSize = sizeof(userSID);
DWORD domainSize = sizeof(userDomain);


SID_NAME_USE snu;
LookupAccountName(NULL,
(LPWSTR)userName,
(PSID)userSID,
&sidSize,
(LPWSTR)userDomain,
&domainSize,
&snu);


PSID_IDENTIFIER_AUTHORITY psia = GetSidIdentifierAuthority(userSID);
sidSize = sprintf(sid, "S-%lu-", SID_REVISION);
sidSize += sprintf(sid + strlen(sid), "%-lu", psia->Value[5]);


int i = 0;
int subAuthorities = *GetSidSubAuthorityCount(userSID);


for (i = 0; i < subAuthorities; i++)
{
sidSize += sprintf(sid + sidSize, "-%lu", *GetSidSubAuthority(userSID, i));
}


你可能感兴趣的:(c与c++)