VC判断进程是否具有administrator权限的方法
本文实例讲述了VC判断进程是否具有admin权限的方法。是的话返回TRUE,否则为FALSE。分享给大家供大家参考。具体实现方法如下:
staticBOOLIsAdmin(void) { HANDLE hAccessToken; BYTE InfoBuffer[1024]; PTOKEN_GROUPS ptgGroups; DWORD dwInfoBufferSize; PSID psidAdministrators; SID_IDENTIFIER_AUTHORITYsiaNtAuthority=SECURITY_NT_AUTHORITY; UINT i; BOOL bRet=FALSE; if(!OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&hAccessToken)) returnbRet; bRet=GetTokenInformation(hAccessToken,TokenGroups,InfoBuffer,1024,&dwInfoBufferSize); CloseHandle(hAccessToken); if(!bRet) returnbRet; if(!AllocateAndInitializeSid(&siaNtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0,0,0,0,0,0, &psidAdministrators)) returnFALSE; bRet=FALSE; ptgGroups=(PTOKEN_GROUPS)InfoBuffer; for(i=0;i<ptgGroups->GroupCount;i++) if(EqualSid(psidAdministrators,ptgGroups->Groups[i].Sid)) bRet=TRUE; FreeSid(psidAdministrators); returnbRet; }
希望本文所述对大家的VC程序设计有所帮助。