Get local administrators users' names using C++

后端 未结 1 1738
花落未央
花落未央 2021-01-23 12:50

I\'m wondering if I can get witch users belongs to my local administrator group and list them. Is there any way to do that using C++? Any WinAPI way, maybe?

Thanks a lo

1条回答
  •  清歌不尽
    2021-01-23 13:30

    You can use NetUserGetLocalGroups and NetUserGetInfo to retrive your information and check the value of usri1_priv in the USER_INFO_1 structure.

    I think something like this should get you started (taken from 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);
    }
    

    You can also consult this page (it's old, but it should work)

    0 讨论(0)
提交回复
热议问题