Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect if my process is running UAC-elevated or not?

My Vista application needs to know whether the user has launched it "as administrator" (elevated) or as a standard user (non-elevated). How can I detect that at run time?

like image 567
Andrei Belogortseff Avatar asked Sep 18 '08 19:09

Andrei Belogortseff


People also ask

How do you check if I have elevated rights?

Right-click on the account and choose the Properties option. Click on the Member Of tab, and if it says both Administrators and Users, you have administrative privileges.

How can I tell if a process is running as administrator?

Scroll down until you find “Elevated,” check the box next to it, and then click “OK.” The Elevated column will now appear in Task Manager. If a process has “Yes” in the Elevated column, that process is running with administrative privileges. That's all there is to it.

How do I run as user without UAC privilege elevation?

run-app-as-non-admin.bat After that, to run any application without the administrator privileges, just select “Run as user without UAC privilege elevation” in the context menu of File Explorer. You can deploy this option to all computers in the domain by importing the registry parameters using GPO.


2 Answers

For those of us working in C#, in the Windows SDK there is a "UACDemo" application as a part of the "Cross Technology Samples". They find if the current user is an administrator using this method:

private bool IsAdministrator {     get     {         WindowsIdentity wi = WindowsIdentity.GetCurrent();         WindowsPrincipal wp = new WindowsPrincipal(wi);          return wp.IsInRole(WindowsBuiltInRole.Administrator);     } } 

(Note: I refactored the original code to be a property, rather than an "if" statement)

like image 142
Adrian Clark Avatar answered Sep 19 '22 11:09

Adrian Clark


The following C++ function can do that:

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; } 
like image 31
Andrei Belogortseff Avatar answered Sep 18 '22 11:09

Andrei Belogortseff