【C/C++】获取当前进程是否处于系统管理员权限下

Code1:

 1 bool IsProcessRunAsAdmin()
 2 {
 3     SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
 4     PSID AdministratorsGroup;
 5 
 6     BOOL b = AllocateAndInitializeSid(
 7         &NtAuthority,
 8         2,
 9         SECURITY_BUILTIN_DOMAIN_RID,
10         DOMAIN_ALIAS_RID_ADMINS,
11         0, 0, 0, 0, 0, 0,
12         &AdministratorsGroup);
13 
14     if(b)
15     {
16         CheckTokenMembership(NULL, AdministratorsGroup, &b);
17         FreeSid(AdministratorsGroup);
18     }
19 
20     return b == TRUE;
21  }

Code2:

 1 BOOL IsAdministrator() //判断是否管理员模式
 2 {
 3     BOOL bIsElevated = FALSE;
 4     HANDLE hToken = NULL;
 5     UINT16 uWinVer = LOWORD(GetVersion());
 6     uWinVer = MAKEWORD(HIBYTE(uWinVer),LOBYTE(uWinVer));
 7 
 8     if (uWinVer < 0x0600)//不是VISTA、Windows7
 9         return(FALSE);
10 
11     if (OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&hToken)) {
12 
13         struct {
14             DWORD TokenIsElevated;
15         } /*TOKEN_ELEVATION*/te;
16         DWORD dwReturnLength = 0;
17 
18         if (GetTokenInformation(hToken,/*TokenElevation*/(_TOKEN_INFORMATION_CLASS)20,&te,sizeof(te),&dwReturnLength)) {
19             if (dwReturnLength == sizeof(te))
20                 bIsElevated = te.TokenIsElevated;
21         }
22         CloseHandle( hToken );
23     }
24     return bIsElevated;
25 }

转载于:https://www.cnblogs.com/kaichd/archive/2012/10/18/2729730.html

你可能感兴趣的:(【C/C++】获取当前进程是否处于系统管理员权限下)