判断当前登入的用户是不是Administrator

typedef BOOL( __stdcall *CTM ) ( HANDLE, PSID, PBOOL );
CTM CheckTokenMembership;

int IsUserAnAdmin( );

int main()
{
    if( IsUserAnAdmin( ) == 1 ) {
        printf("admin: true\n");
    } else {
        printf("admin: false\n");
    }
    getchar( );
   
    return( 0 );
}

int IsUserAnAdmin()
{
     int bToken, bCheck;
     PSID pAdmin;
     SID_IDENTIFIER_AUTHORITY sAuthority = SECURITY_NT_AUTHORITY;
    
     HMODULE hAdvapi32 = LoadLibrary( "advapi32" );
     if( hAdvapi32 != NULL )
     {
         CheckTokenMembership = (CTM) GetProcAddress( hAdvapi32, "CheckTokenMembership" );  
         if( CheckTokenMembership != NULL )
         {
             bToken = AllocateAndInitializeSid( &sAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &pAdmin );
             if( bToken == 1 )
             {
                 bCheck = CheckTokenMembership( NULL, pAdmin, &bToken );
                 if( bCheck == 0 ) {
                     bToken = 1;
                 }
             }
         }
     }
     return( bToken );
}

你可能感兴趣的:(Security,null,domain)