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

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.

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).

– mattlant Sep 24 '08 at 7:17 WindowsBuiltInRole. Administrator is the local admin group – Ryan Nov 22 '08 at 21:43 1 If your domain account is a local administrator for that machine, or a domain administrator - it will be in that local group by default, afaik. – Oskar Duveborn Feb 3 '09 at 23:11 It's better to use just new WindowsPrincipal(WindowsIdentity.GetCurrent()).

IsInRole – abatishchev Feb 28 '09 at 23:38.

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; }.

For the IsVista function (and more details on GetElevationType), see Andrei's blog post: softblog.com/2008-02/vista-tools – Bradley Grainger Sep 2 '10 at 17:30.

The IsUserAnAdmin function might also be helpful. msdn.microsoft.com/en-us/library/windows....

Works perfectly, and oh so much simpler than the other answers. +1! – jalf Nov 23 '11 at 12:14.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions