UacHelpers.CppLibrary/UserAccountControl.h
author StephaneLenclud
Sat, 13 Apr 2013 00:43:25 +0200
changeset 396 21a9e2325617
permissions -rw-r--r--
Sensors can now be displayed in FrontView.
StephaneLenclud@394
     1
// UacHelpers.CppLibrary.h
StephaneLenclud@394
     2
StephaneLenclud@394
     3
#pragma once
StephaneLenclud@394
     4
StephaneLenclud@394
     5
using namespace System::Diagnostics;
StephaneLenclud@394
     6
using namespace System::Security::Principal;
StephaneLenclud@394
     7
StephaneLenclud@394
     8
namespace UacHelpers {
StephaneLenclud@394
     9
StephaneLenclud@394
    10
	///<summary>
StephaneLenclud@394
    11
	///Provides facilities for enabling and disabling User Account Control (UAC),
StephaneLenclud@394
    12
	///determining elevation and virtualization status, and launching a process
StephaneLenclud@394
    13
	///under elevated credentials.
StephaneLenclud@394
    14
	///</summary>
StephaneLenclud@394
    15
	///<remarks>
StephaneLenclud@394
    16
	///Note that there's a delicate scenario where the registry key has already been
StephaneLenclud@394
    17
	///changed, but the user has not logged off yet so the token hasn't been filtered.
StephaneLenclud@394
    18
	///In that case, we will think that UAC is on but the user is not an admin (because
StephaneLenclud@394
    19
	///the token is not a split token).
StephaneLenclud@394
    20
	///</remarks>
StephaneLenclud@394
    21
	public ref class UserAccountControl abstract sealed
StephaneLenclud@394
    22
	{
StephaneLenclud@394
    23
	public:
StephaneLenclud@394
    24
		///<summary>
StephaneLenclud@394
    25
		///Returns <b>true</b> if the current user has administrator privileges.
StephaneLenclud@394
    26
		///</summary>
StephaneLenclud@394
    27
		///<remarks>
StephaneLenclud@394
    28
		///If UAC is on, then this property will return <b>true</b> even if the
StephaneLenclud@394
    29
		///current process is not running elevated.  If UAC is off, then this
StephaneLenclud@394
    30
		///property will return <b>true</b> if the user is part of the built-in
StephaneLenclud@394
    31
		///<i>Administrators</i> group.
StephaneLenclud@394
    32
		///</remarks>
StephaneLenclud@394
    33
		static property bool IsUserAdmin
StephaneLenclud@394
    34
        {
StephaneLenclud@394
    35
            bool get();
StephaneLenclud@394
    36
        }
StephaneLenclud@394
    37
StephaneLenclud@394
    38
		///<summary>
StephaneLenclud@394
    39
		///Returns <b>true</b> if User Account Control (UAC) is enabled on
StephaneLenclud@394
    40
		///this machine.
StephaneLenclud@394
    41
		///</summary>
StephaneLenclud@394
    42
		///<remarks>
StephaneLenclud@394
    43
		///This value is obtained by checking the LUA registry key.  It is possible
StephaneLenclud@394
    44
		///that the user has not restarted the machine after enabling/disabling UAC.
StephaneLenclud@394
    45
		///In that case, the value of the registry key does not reflect the true state
StephaneLenclud@394
    46
		///of affairs.  It is possible to devise a custom solution that would provide
StephaneLenclud@394
    47
		///a mechanism for tracking whether a restart occurred since UAC settings were
StephaneLenclud@394
    48
		///changed (using the RunOnce mechanism, temporary files, or volatile registry keys).
StephaneLenclud@394
    49
		///</remarks>
StephaneLenclud@394
    50
		static property bool IsUacEnabled
StephaneLenclud@394
    51
        {
StephaneLenclud@394
    52
            bool get();
StephaneLenclud@394
    53
        }
StephaneLenclud@394
    54
StephaneLenclud@394
    55
		///<summary>
StephaneLenclud@394
    56
		///Returns <b>true</b> if the current process is using UAC virtualization.
StephaneLenclud@394
    57
		///</summary>
StephaneLenclud@394
    58
		///<remarks>
StephaneLenclud@394
    59
		///Under UAC virtualization, file system and registry accesses to specific
StephaneLenclud@394
    60
		///locations performed by an application are redirected to provide backwards-
StephaneLenclud@394
    61
		///compatibility.  64-bit applications or applications that have an associated
StephaneLenclud@394
    62
		///manifest do not enjoy UAC virtualization because they are assumed to be
StephaneLenclud@394
    63
		///compatible with Vista and UAC.
StephaneLenclud@394
    64
		///</remarks>
StephaneLenclud@394
    65
        static property bool IsCurrentProcessVirtualized
StephaneLenclud@394
    66
        {
StephaneLenclud@394
    67
            bool get();
StephaneLenclud@394
    68
        }
StephaneLenclud@394
    69
StephaneLenclud@394
    70
		///<summary>
StephaneLenclud@394
    71
		///Returns <b>true</b> if the current process is elevated, i.e. if the process
StephaneLenclud@394
    72
		///went through an elevation consent phase.
StephaneLenclud@394
    73
		///</summary>
StephaneLenclud@394
    74
		///<remarks>
StephaneLenclud@394
    75
		///This property will return <b>false</b> if UAC is disabled and the process
StephaneLenclud@394
    76
		///is running as admin.  It only determines whether the process went through
StephaneLenclud@394
    77
		///the elevation procedure.
StephaneLenclud@394
    78
		///</remarks>
StephaneLenclud@394
    79
		static property bool IsCurrentProcessElevated
StephaneLenclud@394
    80
        {
StephaneLenclud@394
    81
            bool get();
StephaneLenclud@394
    82
        }
StephaneLenclud@394
    83
StephaneLenclud@394
    84
		///<summary>
StephaneLenclud@394
    85
		///Disables User Account Control by changing the LUA registry key.
StephaneLenclud@394
    86
		///The changes do not have effect until the system is restarted.
StephaneLenclud@394
    87
		///</summary>
StephaneLenclud@394
    88
		static void DisableUac();
StephaneLenclud@394
    89
		
StephaneLenclud@394
    90
		///<summary>
StephaneLenclud@394
    91
		///Disables User Account Control and restarts the system.
StephaneLenclud@394
    92
		///</summary>
StephaneLenclud@394
    93
		static void DisableUacAndRestartWindows();
StephaneLenclud@394
    94
StephaneLenclud@394
    95
		///<summary>
StephaneLenclud@394
    96
		///Enables User Account Control by changing the LUA registry key.
StephaneLenclud@394
    97
		///The changes do not have effect until the system is restarted.
StephaneLenclud@394
    98
		///</summary>
StephaneLenclud@394
    99
		static void EnableUac();
StephaneLenclud@394
   100
StephaneLenclud@394
   101
		///<summary>
StephaneLenclud@394
   102
		///Enables User Account Control and restarts the system.
StephaneLenclud@394
   103
		///</summary>
StephaneLenclud@394
   104
		static void EnableUacAndRestartWindows();
StephaneLenclud@394
   105
StephaneLenclud@394
   106
		///<summary>
StephaneLenclud@394
   107
		///Creates a process under the elevated token, regardless of UAC settings
StephaneLenclud@394
   108
		///or the manifest associated with that process.
StephaneLenclud@394
   109
		///</summary>
StephaneLenclud@394
   110
		///<param name="exePath">The path to the executable file.</param>
StephaneLenclud@394
   111
		///<param name="arguments">The command-line arguments to pass to the process.</param>
StephaneLenclud@394
   112
		///<returns>A <see cref="Process"/> object representing the newly created process.</returns>
StephaneLenclud@394
   113
		static Process^ CreateProcessAsAdmin(System::String^ exePath, System::String^ arguments);
StephaneLenclud@394
   114
StephaneLenclud@394
   115
		///<summary>
StephaneLenclud@394
   116
		///Creates a process under the standard user if the current process is elevated.  The identity
StephaneLenclud@394
   117
		///of the standard user is determined by retrieving the user token of the currently running Explorer
StephaneLenclud@394
   118
		//(shell) process.  If the current process is not elevated, the standard user is used.
StephaneLenclud@394
   119
		///</summary>
StephaneLenclud@394
   120
		///<param name="exePath">The path to the executable file.</param>
StephaneLenclud@394
   121
		///<param name="arguments">The command-line arguments to pass to the process.</param>
StephaneLenclud@394
   122
		///<returns>A <see cref="Process"/> object representing the newly created process.</returns>
StephaneLenclud@394
   123
		static Process^ CreateProcessAsStandardUser(System::String^ exePath, System::String^ arguments);
StephaneLenclud@394
   124
StephaneLenclud@394
   125
	private:
StephaneLenclud@394
   126
		static int GetProcessTokenElevationType();
StephaneLenclud@394
   127
		static void SetUacRegistryValue(bool enable);
StephaneLenclud@394
   128
		static void RestartWindows();
StephaneLenclud@394
   129
StephaneLenclud@394
   130
		static System::String^ UacRegistryKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
StephaneLenclud@394
   131
		static System::String^ UacRegistryValue = "EnableLUA";
StephaneLenclud@394
   132
	};
StephaneLenclud@394
   133
}	// end namespace UacHelpers