首页 > 代码库 > VC判断当前用户有无Administrator的权限(用EqualSid API函数判断与Admin是否在一个组的Sid)

VC判断当前用户有无Administrator的权限(用EqualSid API函数判断与Admin是否在一个组的Sid)

 


[cpp]
 view plain copy
 
 print?
  1. /************************************************************************/  
  2. /* 函数说明:判断有无Administrator的权限                                      
  3. /* 参    数:无                                  
  4. /* 返 回 值:成功返回TRUE,否则返回FALSE   
  5. /* By:Koma   2009.07.28 23:30                               
  6. /************************************************************************/  
  7. BOOL IsAdmin()    
  8. {    
  9.     HANDLE hAccessToken;    
  10.     BYTE * InfoBuffer = new BYTE[1024];    
  11.     PTOKEN_GROUPS ptgGroups;    
  12.     DWORD dwInfoBufferSize;    
  13.     PSID psidAdministrators;    
  14.     SID_IDENTIFIER_AUTHORITY siaNtAuthority = SECURITY_NT_AUTHORITY;    
  15.       
  16.     if(!OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&hAccessToken))    
  17.     {    
  18.         delete InfoBuffer;    
  19.         return FALSE;    
  20.     }    
  21.       
  22.     if(!GetTokenInformation(hAccessToken,TokenGroups,InfoBuffer,1024,&dwInfoBufferSize))    
  23.     {    
  24.         delete InfoBuffer;    
  25.         CloseHandle(hAccessToken);    
  26.         return FALSE;    
  27.     }    
  28.       
  29.     CloseHandle(hAccessToken);    
  30.       
  31.     if(!AllocateAndInitializeSid(&siaNtAuthority,    
  32.         2,    
  33.         SECURITY_BUILTIN_DOMAIN_RID,    
  34.         DOMAIN_ALIAS_RID_ADMINS,    
  35.         0,0,0,0,0,0,    
  36.         &psidAdministrators))    
  37.     {    
  38.         delete InfoBuffer;    
  39.         return FALSE;    
  40.     }    
  41.       
  42.     ptgGroups = (PTOKEN_GROUPS)InfoBuffer;    
  43.       
  44.     for(UINT i = 0; i < ptgGroups->GroupCount; i++)    
  45.     {    
  46.         if(EqualSid(psidAdministrators,ptgGroups->Groups[i].Sid))    
  47.         {    
  48.             FreeSid(psidAdministrators);    
  49.             delete InfoBuffer;    
  50.             return TRUE;    
  51.         }    
  52.     }    
  53.     return FALSE;    
  54. }   

 

 
 

http://blog.csdn.net/wangningyu/article/details/4389546

VC判断当前用户有无Administrator的权限(用EqualSid API函数判断与Admin是否在一个组的Sid)