ashmish2
ashmish2

Reputation: 2967

how to check if user is admin in windows 7?

I found this link in msdn to check if user is admin. its working fine on some win7 machine when process is elevated (run as administrator) but I found some machines (some win 2008) where its not working! Is this the right way to go about it ??

BOOL IsUserAdmin(VOID)
{
  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);
}    

Upvotes: 1

Views: 1762

Answers (1)

BigBoss
BigBoss

Reputation: 6914

You can use IsUserAnAdmin() that is a wrapper for the call above

Upvotes: 2

Related Questions