当前位置:  开发笔记 > 编程语言 > 正文

如何检测我的进程是否正在运行UAC升级?

如何解决《如何检测我的进程是否正在运行UAC升级?》经验,为你挑选了2个好方法。

我的Vista应用程序需要知道用户是"以管理员"(提升)还是以标准用户(非提升)方式启动它.我怎样才能在运行时检测到它?



1> Adrian Clark..:

对于我们这些在C#中工作的人来说,在Windows SDK中有一个"UACDemo"应用程序作为"交叉技术示例"的一部分.他们发现当前用户是否是使用此方法的管理员:

private bool IsAdministrator
{
    get
    {
        WindowsIdentity wi = WindowsIdentity.GetCurrent();
        WindowsPrincipal wp = new WindowsPrincipal(wi);

        return wp.IsInRole(WindowsBuiltInRole.Administrator);
    }
}

(注意:我将原始代码重构为属性,而不是"if"语句)


如果您的域帐户是该计算机的本地管理员,或域管理员 - 默认情况下,它将位于该本地组中,afaik.
+1用于引用代码的原始源.我已经看到这些线条随处可见,但这是唯一一个提供参考的帖子.

2> Andrei Belog..:

以下C++函数可以做到这一点:

HRESULT GetElevationType( __out TOKEN_ELEVATION_TYPE * ptet );

/*
Parameters:

ptet
    [out] Pointer to a variable that receives the elevation type of the current process.

    The possible values are:

    TokenElevationTypeDefault - This value indicates that either UAC is disabled, 
        or the process is started by a standard user (not a member of the Administrators group).

    The following two values can be returned only if both the UAC is enabled
    and the user is a member of the Administrator's group:

    TokenElevationTypeFull - the process is running elevated. 

    TokenElevationTypeLimited - the process is not running elevated.

Return Values:

    If the function succeeds, the return value is S_OK. 
    If the function fails, the return value is E_FAIL. To get extended error information, call GetLastError().

Implementation:
*/

HRESULT GetElevationType( __out TOKEN_ELEVATION_TYPE * ptet )
{
    if ( !IsVista() )
        return E_FAIL;

    HRESULT hResult = E_FAIL; // assume an error occurred
    HANDLE hToken   = NULL;

    if ( !::OpenProcessToken( 
                ::GetCurrentProcess(), 
                TOKEN_QUERY, 
                &hToken ) )
    {
        return hResult;
    }

    DWORD dwReturnLength = 0;

    if ( ::GetTokenInformation(
                hToken,
                TokenElevationType,
                ptet,
                sizeof( *ptet ),
                &dwReturnLength ) )
    {
            ASSERT( dwReturnLength == sizeof( *ptet ) );
            hResult = S_OK;
    }

    ::CloseHandle( hToken );

    return hResult;
}

推荐阅读
LEEstarmmmmm
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有