From 20962895b0b24bb419f46869df3898ce35a7b3e9 Mon Sep 17 00:00:00 2001 From: Travis Nickles Date: Thu, 13 Feb 2020 17:01:14 -0600 Subject: [PATCH] Move elevation code to ControlService class --- DS4Windows/DS4Control/ControlService.cs | 25 ++++++++++++++++ DS4Windows/DS4Library/DS4Devices.cs | 40 +++++++++++++++++-------- 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/DS4Windows/DS4Control/ControlService.cs b/DS4Windows/DS4Control/ControlService.cs index 6eac48b..fe44df7 100644 --- a/DS4Windows/DS4Control/ControlService.cs +++ b/DS4Windows/DS4Control/ControlService.cs @@ -176,6 +176,31 @@ namespace DS4Windows } outputslotMan = new OutputSlotManager(); + DS4Devices.RequestElevation += DS4Devices_RequestElevation; + } + + private void DS4Devices_RequestElevation(RequestElevationArgs args) + { + // Launches an elevated child process to re-enable device + string exeName = Global.exelocation; + ProcessStartInfo startInfo = new ProcessStartInfo(exeName); + startInfo.Verb = "runas"; + startInfo.Arguments = "re-enabledevice " + args.InstanceId; + + try + { + Process child = Process.Start(startInfo); + if (!child.WaitForExit(30000)) + { + child.Kill(); + } + else + { + args.StatusCode = child.ExitCode; + } + child.Dispose(); + } + catch { } } public void LaunchHidGuardHelper() diff --git a/DS4Windows/DS4Library/DS4Devices.cs b/DS4Windows/DS4Library/DS4Devices.cs index 588ccb7..c76905c 100644 --- a/DS4Windows/DS4Library/DS4Devices.cs +++ b/DS4Windows/DS4Library/DS4Devices.cs @@ -20,6 +20,27 @@ namespace DS4Windows } } + public class RequestElevationArgs : EventArgs + { + public const int STATUS_SUCCESS = 0; + public const int STATUS_INIT_FAILURE = -1; + private int statusCode = STATUS_INIT_FAILURE; + private string instanceId; + public int StatusCode + { + get => statusCode; + set => statusCode = value; + } + public string InstanceId { get => instanceId; } + + public RequestElevationArgs(string instanceId) + { + this.instanceId = instanceId; + } + } + + public delegate void RequestElevationDelegate(RequestElevationArgs args); + public class DS4Devices { // (HID device path, DS4Device) @@ -29,6 +50,7 @@ namespace DS4Windows // Keep instance of opened exclusive mode devices not in use (Charging while using BT connection) private static List DisabledDevices = new List(); private static Stopwatch sw = new Stopwatch(); + public static event RequestElevationDelegate RequestElevation; public static bool isExclusiveMode = false; internal const int SONY_VID = 0x054C; internal const int RAZER_VID = 0x1532; @@ -120,24 +142,18 @@ namespace DS4Windows { try { + // Check if running with elevated permissions WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); bool elevated = principal.IsInRole(WindowsBuiltInRole.Administrator); if (!elevated) { - // Launches an elevated child process to re-enable device - string exeName = Process.GetCurrentProcess().MainModule.FileName; - ProcessStartInfo startInfo = new ProcessStartInfo(exeName); - startInfo.Verb = "runas"; - startInfo.Arguments = "re-enabledevice " + devicePathToInstanceId(hDevice.DevicePath); - Process child = Process.Start(startInfo); - - if (!child.WaitForExit(30000)) - { - child.Kill(); - } - else if (child.ExitCode == 0) + // Tell the client to launch routine to re-enable a device + RequestElevationArgs eleArgs = + new RequestElevationArgs(devicePathToInstanceId(hDevice.DevicePath)); + RequestElevation?.Invoke(eleArgs); + if (eleArgs.StatusCode == RequestElevationArgs.STATUS_SUCCESS) { hDevice.OpenDevice(isExclusiveMode); }