views:

3527

answers:

5

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?

+1  A: 

Check for the registry value at HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System

The EnableLUA value determines if UAC is active.

Mark Schill
+2  A: 

This post has sample code in C# to test if UAC is on and if the current app has been given elevated rights. You can download the code and interpret as needed. Also linked there is a sample that shows the same in C++

http://www.itwriting.com/blog/198-c-code-to-detect-uac-elevation-on-vista.html

The code in that post does not just read from the registry. If UAC is enabled, chances are you may not have rights to read that from the registry.

Ryan Farley
A: 

AFAIK, UAC is apolicy setting on the local user or group. So you can read this property from within .Net. Sorry for not having more details but I hope this helps

GHad
+6  A: 

This registry key should tell you:

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System Value EnableLUA (DWORD)

1 enabled / 0 or missing disabled

But that assumes you have the rights to read it.

Programmatically you can try to read the user's token and guess if it's an admin running with UAC enabled (see here). Not foolproof, but it may work.

The issue here is more of a "why do you need to know" - it has bearing on the answer. Really, there is no API because from a OS behavior point of view, what matters is if the user is an administrator or not - how they choose to protect themselves as admin is their problem.

Philip Rieck
This is a catch 22. If UAC is enabled, chances are you won't have rights to read it.
Ryan Farley
A minor correction: the registry key is HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System, and EnableLUA is a DWORD value (it's not a key).HTH
Andrei Belogortseff
You should use != 0 and not compare with 1 according to the google chrome source (Apparently some systems have >1 values)
Anders
+1  A: 

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.

Andrei Belogortseff