VC判断进程是否具有administrator权限的方法
时间:2021-02-01 09:56:21|栏目:C代码|点击: 次
本文实例讲述了VC判断进程是否具有admin权限的方法。是的话返回TRUE,否则为FALSE。分享给大家供大家参考。具体实现方法如下:
复制代码 代码如下:
static BOOL IsAdmin(void)
{
HANDLE hAccessToken;
BYTE InfoBuffer[1024];
PTOKEN_GROUPS ptgGroups;
DWORD dwInfoBufferSize;
PSID psidAdministrators;
SID_IDENTIFIER_AUTHORITY siaNtAuthority = SECURITY_NT_AUTHORITY;
UINT i;
BOOL bRet = FALSE;
if(!OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&hAccessToken))
return bRet;
bRet = GetTokenInformation(hAccessToken, TokenGroups, InfoBuffer, 1024, &dwInfoBufferSize);
CloseHandle(hAccessToken);
if(!bRet)
return bRet;
if(!AllocateAndInitializeSid(&siaNtAuthority,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0,0,0,0,0,0,
&psidAdministrators))
return FALSE;
bRet = FALSE;
ptgGroups = (PTOKEN_GROUPS)InfoBuffer;
for(i=0;i<ptgGroups->GroupCount;i++)
if(EqualSid(psidAdministrators,ptgGroups->Groups[i].Sid))
bRet = TRUE;
FreeSid(psidAdministrators);
return bRet;
}
{
HANDLE hAccessToken;
BYTE InfoBuffer[1024];
PTOKEN_GROUPS ptgGroups;
DWORD dwInfoBufferSize;
PSID psidAdministrators;
SID_IDENTIFIER_AUTHORITY siaNtAuthority = SECURITY_NT_AUTHORITY;
UINT i;
BOOL bRet = FALSE;
if(!OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&hAccessToken))
return bRet;
bRet = GetTokenInformation(hAccessToken, TokenGroups, InfoBuffer, 1024, &dwInfoBufferSize);
CloseHandle(hAccessToken);
if(!bRet)
return bRet;
if(!AllocateAndInitializeSid(&siaNtAuthority,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0,0,0,0,0,0,
&psidAdministrators))
return FALSE;
bRet = FALSE;
ptgGroups = (PTOKEN_GROUPS)InfoBuffer;
for(i=0;i<ptgGroups->GroupCount;i++)
if(EqualSid(psidAdministrators,ptgGroups->Groups[i].Sid))
bRet = TRUE;
FreeSid(psidAdministrators);
return bRet;
}
希望本文所述对大家的VC程序设计有所帮助。