C++判断当前进程是否以管理员身份运行源码
判断当前进程是否"以管理员身份运行"的,是否有管理员权限.成功返回真,失败返回假.
仅在Vista、Windows7有效,更低版本的系统直接返回假.
//判断是否管理员模式
BOOL IsAdministrator() {
BOOL bIsElevated = FALSE;
HANDLE hToken = NULL;
UINT16 uWinVer = LOWORD(GetVersion());
uWinVer = MAKEWORD(HIBYTE(uWinVer),LOBYTE(uWinVer));
if (uWinVer < 0x0600))//不是VISTA、Windows7
return(FALSE);
if (OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&hToken)) {
struct {
DWORD TokenIsElevated;
} /*TOKEN_ELEVATION*/te;
DWORD dwReturnLength = 0;
if (GetTokenInformation(hToken,/*TokenElevation*/(_TOKEN_INFORMATION_CLASS)20,&te,sizeof(te),&dwReturnLength)) {
if (dwReturnLength == sizeof(te))
bIsElevated = te.TokenIsElevated;
}
CloseHandle( hToken );
}
return bIsElevated;
}
页:
[1]