StephaneLenclud@436: // UacHelpers.CppLibrary.h
StephaneLenclud@436:
StephaneLenclud@436: #pragma once
StephaneLenclud@436:
StephaneLenclud@436: using namespace System::Diagnostics;
StephaneLenclud@436: using namespace System::Security::Principal;
StephaneLenclud@436:
StephaneLenclud@436: namespace UacHelpers {
StephaneLenclud@436:
StephaneLenclud@436: ///
StephaneLenclud@436: ///Provides facilities for enabling and disabling User Account Control (UAC),
StephaneLenclud@436: ///determining elevation and virtualization status, and launching a process
StephaneLenclud@436: ///under elevated credentials.
StephaneLenclud@436: ///
StephaneLenclud@436: ///
StephaneLenclud@436: ///Note that there's a delicate scenario where the registry key has already been
StephaneLenclud@436: ///changed, but the user has not logged off yet so the token hasn't been filtered.
StephaneLenclud@436: ///In that case, we will think that UAC is on but the user is not an admin (because
StephaneLenclud@436: ///the token is not a split token).
StephaneLenclud@436: ///
StephaneLenclud@436: public ref class UserAccountControl abstract sealed
StephaneLenclud@436: {
StephaneLenclud@436: public:
StephaneLenclud@436: ///
StephaneLenclud@436: ///Returns true if the current user has administrator privileges.
StephaneLenclud@436: ///
StephaneLenclud@436: ///
StephaneLenclud@436: ///If UAC is on, then this property will return true even if the
StephaneLenclud@436: ///current process is not running elevated. If UAC is off, then this
StephaneLenclud@436: ///property will return true if the user is part of the built-in
StephaneLenclud@436: ///Administrators group.
StephaneLenclud@436: ///
StephaneLenclud@436: static property bool IsUserAdmin
StephaneLenclud@436: {
StephaneLenclud@436: bool get();
StephaneLenclud@436: }
StephaneLenclud@436:
StephaneLenclud@436: ///
StephaneLenclud@436: ///Returns true if User Account Control (UAC) is enabled on
StephaneLenclud@436: ///this machine.
StephaneLenclud@436: ///
StephaneLenclud@436: ///
StephaneLenclud@436: ///This value is obtained by checking the LUA registry key. It is possible
StephaneLenclud@436: ///that the user has not restarted the machine after enabling/disabling UAC.
StephaneLenclud@436: ///In that case, the value of the registry key does not reflect the true state
StephaneLenclud@436: ///of affairs. It is possible to devise a custom solution that would provide
StephaneLenclud@436: ///a mechanism for tracking whether a restart occurred since UAC settings were
StephaneLenclud@436: ///changed (using the RunOnce mechanism, temporary files, or volatile registry keys).
StephaneLenclud@436: ///
StephaneLenclud@436: static property bool IsUacEnabled
StephaneLenclud@436: {
StephaneLenclud@436: bool get();
StephaneLenclud@436: }
StephaneLenclud@436:
StephaneLenclud@436: ///
StephaneLenclud@436: ///Returns true if the current process is using UAC virtualization.
StephaneLenclud@436: ///
StephaneLenclud@436: ///
StephaneLenclud@436: ///Under UAC virtualization, file system and registry accesses to specific
StephaneLenclud@436: ///locations performed by an application are redirected to provide backwards-
StephaneLenclud@436: ///compatibility. 64-bit applications or applications that have an associated
StephaneLenclud@436: ///manifest do not enjoy UAC virtualization because they are assumed to be
StephaneLenclud@436: ///compatible with Vista and UAC.
StephaneLenclud@436: ///
StephaneLenclud@436: static property bool IsCurrentProcessVirtualized
StephaneLenclud@436: {
StephaneLenclud@436: bool get();
StephaneLenclud@436: }
StephaneLenclud@436:
StephaneLenclud@436: ///
StephaneLenclud@436: ///Returns true if the current process is elevated, i.e. if the process
StephaneLenclud@436: ///went through an elevation consent phase.
StephaneLenclud@436: ///
StephaneLenclud@436: ///
StephaneLenclud@436: ///This property will return false if UAC is disabled and the process
StephaneLenclud@436: ///is running as admin. It only determines whether the process went through
StephaneLenclud@436: ///the elevation procedure.
StephaneLenclud@436: ///
StephaneLenclud@436: static property bool IsCurrentProcessElevated
StephaneLenclud@436: {
StephaneLenclud@436: bool get();
StephaneLenclud@436: }
StephaneLenclud@436:
StephaneLenclud@436: ///
StephaneLenclud@436: ///Disables User Account Control by changing the LUA registry key.
StephaneLenclud@436: ///The changes do not have effect until the system is restarted.
StephaneLenclud@436: ///
StephaneLenclud@436: static void DisableUac();
StephaneLenclud@436:
StephaneLenclud@436: ///
StephaneLenclud@436: ///Disables User Account Control and restarts the system.
StephaneLenclud@436: ///
StephaneLenclud@436: static void DisableUacAndRestartWindows();
StephaneLenclud@436:
StephaneLenclud@436: ///
StephaneLenclud@436: ///Enables User Account Control by changing the LUA registry key.
StephaneLenclud@436: ///The changes do not have effect until the system is restarted.
StephaneLenclud@436: ///
StephaneLenclud@436: static void EnableUac();
StephaneLenclud@436:
StephaneLenclud@436: ///
StephaneLenclud@436: ///Enables User Account Control and restarts the system.
StephaneLenclud@436: ///
StephaneLenclud@436: static void EnableUacAndRestartWindows();
StephaneLenclud@436:
StephaneLenclud@436: ///
StephaneLenclud@436: ///Creates a process under the elevated token, regardless of UAC settings
StephaneLenclud@436: ///or the manifest associated with that process.
StephaneLenclud@436: ///
StephaneLenclud@436: ///The path to the executable file.
StephaneLenclud@436: ///The command-line arguments to pass to the process.
StephaneLenclud@436: ///A object representing the newly created process.
StephaneLenclud@436: static Process^ CreateProcessAsAdmin(System::String^ exePath, System::String^ arguments);
StephaneLenclud@436:
StephaneLenclud@436: ///
StephaneLenclud@436: ///Creates a process under the standard user if the current process is elevated. The identity
StephaneLenclud@436: ///of the standard user is determined by retrieving the user token of the currently running Explorer
StephaneLenclud@436: //(shell) process. If the current process is not elevated, the standard user is used.
StephaneLenclud@436: ///
StephaneLenclud@436: ///The path to the executable file.
StephaneLenclud@436: ///The command-line arguments to pass to the process.
StephaneLenclud@436: ///A object representing the newly created process.
StephaneLenclud@436: static Process^ CreateProcessAsStandardUser(System::String^ exePath, System::String^ arguments);
StephaneLenclud@436:
StephaneLenclud@436: private:
StephaneLenclud@436: static int GetProcessTokenElevationType();
StephaneLenclud@436: static void SetUacRegistryValue(bool enable);
StephaneLenclud@436: static void RestartWindows();
StephaneLenclud@436:
StephaneLenclud@436: static System::String^ UacRegistryKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
StephaneLenclud@436: static System::String^ UacRegistryValue = "EnableLUA";
StephaneLenclud@436: };
StephaneLenclud@436: } // end namespace UacHelpers