如何正确检查进程是否正在使用管理权限运行?
我检查了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; }
更多推荐
如何检查进程是否具有管理权限
发布评论