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