How to detect whether Vista UAC is enabled?

后端 未结 8 1293
渐次进展
渐次进展 2021-01-31 04:48

I need my application to behave differently depending on whether Vista UAC is enabled or not. How can my application detect the state of UAC on the user\'s computer?

8条回答
  •  醉话见心
    2021-01-31 05:20

    You can do it be examining the DWORD value EnableLUA in the following registry key:

    HKLM/SOFTWARE/Microsoft/Windows/CurrentVersion/Policies/System

    If the value is 0 (or does not exist) then the UAC is OFF. If it's present and non-zero, then UAC is ON:

    BOOL IsUacEnabled( )
    {
        LPCTSTR pszSubKey = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
        LPCTSTR pszValue = _T("EnableLUA");
        DWORD dwType = 0;
        DWORD dwValue = 0;
        DWORD dwValueSize = sizeof( DWORD );
    
        if ( ERROR_SUCCESS != SHGetValue( HKEY_LOCAL_MACHINE, pszSubKey, pszValueOn, 
            &dwType, &dwValue, &dwValueSize) )
        {
                return FALSE;
        }
    
        return dwValue != 0;
    } 
    

    Note that if the user has changed the state of UAC but has not restarted the computer yet, this function will return an inconsistent result.

提交回复
热议问题