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. if(!OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&hAccessToken))
  16. {
  17. delete InfoBuffer;
  18. return FALSE;
  19. }
  20. if(!GetTokenInformation(hAccessToken,TokenGroups,InfoBuffer,1024,&dwInfoBufferSize))
  21. {
  22. delete InfoBuffer;
  23. CloseHandle(hAccessToken);
  24. return FALSE;
  25. }
  26. CloseHandle(hAccessToken);
  27. if(!AllocateAndInitializeSid(&siaNtAuthority,
  28. 2,
  29. SECURITY_BUILTIN_DOMAIN_RID,
  30. DOMAIN_ALIAS_RID_ADMINS,
  31. 0,0,0,0,0,0,
  32. &psidAdministrators))
  33. {
  34. delete InfoBuffer;
  35. return FALSE;
  36. }
  37. ptgGroups = (PTOKEN_GROUPS)InfoBuffer;
  38. for(UINT i = 0; i < ptgGroups->GroupCount; i++)
  39. {
  40. if(EqualSid(psidAdministrators,ptgGroups->Groups[i].Sid))
  41. {
  42. FreeSid(psidAdministrators);
  43. delete InfoBuffer;
  44. return TRUE;
  45. }
  46. }
  47. return FALSE;
  48. }
 
 

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

04-15 10:03