使用 C++ 获取本地管理员用户名

Get local administrators users' names using C++

我想知道是否可以让 用户属于我的本地管理员组并列出他们。有没有办法使用 C++ 来做到这一点?也许是任何 WinAPI 方式?

非常感谢。

您可以使用 NetUserGetLocalGroups and NetUserGetInfo to retrive your information and check the value of usri1_priv in the USER_INFO_1 结构。

我认为这样的事情应该可以帮助您入门(摘自 MSDN):

BOOL IsUserAdmin(VOID)
/*++ 
Routine Description: This routine returns TRUE if the caller's
process is a member of the Administrators local group. Caller is NOT
expected to be impersonating anyone and is expected to be able to
open its own process and process token. 
Arguments: None. 
Return Value: 
   TRUE - Caller has Administrators local group. 
   FALSE - Caller does not have Administrators local group. --
*/ 
{
BOOL b;
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
PSID AdministratorsGroup; 
b = AllocateAndInitializeSid(
    &NtAuthority,
    2,
    SECURITY_BUILTIN_DOMAIN_RID,
    DOMAIN_ALIAS_RID_ADMINS,
    0, 0, 0, 0, 0, 0,
    &AdministratorsGroup); 
if(b) 
{
    if (!CheckTokenMembership( NULL, AdministratorsGroup, &b)) 
    {
         b = FALSE;
    } 
    FreeSid(AdministratorsGroup); 
}

return(b);
}

你也可以参考这个page(很旧,但应该可以用)