如何检查进程是否具有管理权限

编程入门 行业动态 更新时间:2024-10-27 04:34:14
本文介绍了如何检查进程是否具有管理权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

我检查了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 Windows7.

解决方案

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; }

更多推荐

如何检查进程是否具有管理权限

本文发布于:2023-11-11 06:45:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577662.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管理权限   进程

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!