如何检查进程是否具有管理权限 [英] How to check if a process has the administrative rights

查看:123
本文介绍了如何检查进程是否具有管理权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何正确检查进程是否正在使用管理权限运行?



我检查了MSDN中的 IsUserAnAdim 函数,但不推荐使用,因为它可能会被更改或不可用版本的Windows。相反,建议使用 CheckTokenMembership 函数。



然后我看看MSDN中的备用示例函数 CheckTokenMembership 的描述。但是,Stefan Ozminski在MSDN中的评论指出,如果 UAC 这个示例在Windows Vista中无法正常工作, a>被禁用。



最后,我试图从MSDN使用Stefan Ozminski的代码,但它确定该进程具有管理权限,即使我在普通用户下启动它

$ p

解决方案

这将告诉您如果您使用提升的权限运行。您可以将清单设置为最可能运行,如果您希望它提示。还有其他方法通过代码为窗口请求备用凭据。



 
BOOL IsElevated
BOOL fRet = FALSE;
HANDLE hToken = NULL;
if(OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,& hToken)){
TOKEN_ELEVATION提升;
DWORD cbSize = sizeof(TOKEN_ELEVATION);
if(GetTokenInformation(hToken,TokenElevation,& Elevation,sizeof(Elevation),& cbSize)){
fRet = Elevation.TokenIsElevated;
}
}
if(hToken){
CloseHandle(hToken);
}
return fRet;
}


How do I properly check if a process is running with administrative rights?

I checked the IsUserAnAdim function in MSDN, but it is not recommended as it might be altered or unavailable in subsequent versions of Windows. Instead, it is recommended to use the CheckTokenMembership function.

Then I looked at the alternate example in MSDN from a description of the CheckTokenMembership function. However, there is Stefan Ozminski's comment in MSDN that mentions that this example does not work properly in Windows Vista if UAC is disabled.

Finally I tried to use Stefan Ozminski's code from MSDN, but it determines that the process has administrative rights even if I launch it under an ordinary user without the administrative rights in Windows 7.

解决方案

This will tell you if you are running with elevated privileges or not. You can set the manifest to run with most possible if you want it to prompt. There are also other ways to ask windows through code for alternate credentials.

BOOL IsElevated( ) {
    BOOL fRet = FALSE;
    HANDLE hToken = NULL;
    if( OpenProcessToken( GetCurrentProcess( ),TOKEN_QUERY,&hToken ) ) {
        TOKEN_ELEVATION Elevation;
        DWORD cbSize = sizeof( TOKEN_ELEVATION );
        if( GetTokenInformation( hToken, TokenElevation, &Elevation, sizeof( Elevation ), &cbSize ) ) {
            fRet = Elevation.TokenIsElevated;
        }
    }
    if( hToken ) {
        CloseHandle( hToken );
    }
    return fRet;
}

这篇关于如何检查进程是否具有管理权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆