Move elevation code to ControlService class

This commit is contained in:
Travis Nickles 2020-02-13 17:01:14 -06:00
parent 4444ce94c8
commit 20962895b0
2 changed files with 53 additions and 12 deletions

View File

@ -176,6 +176,31 @@ namespace DS4Windows
} }
outputslotMan = new OutputSlotManager(); 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() public void LaunchHidGuardHelper()

View File

@ -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 public class DS4Devices
{ {
// (HID device path, DS4Device) // (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) // Keep instance of opened exclusive mode devices not in use (Charging while using BT connection)
private static List<HidDevice> DisabledDevices = new List<HidDevice>(); private static List<HidDevice> DisabledDevices = new List<HidDevice>();
private static Stopwatch sw = new Stopwatch(); private static Stopwatch sw = new Stopwatch();
public static event RequestElevationDelegate RequestElevation;
public static bool isExclusiveMode = false; public static bool isExclusiveMode = false;
internal const int SONY_VID = 0x054C; internal const int SONY_VID = 0x054C;
internal const int RAZER_VID = 0x1532; internal const int RAZER_VID = 0x1532;
@ -120,24 +142,18 @@ namespace DS4Windows
{ {
try try
{ {
// Check if running with elevated permissions
WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity); WindowsPrincipal principal = new WindowsPrincipal(identity);
bool elevated = principal.IsInRole(WindowsBuiltInRole.Administrator); bool elevated = principal.IsInRole(WindowsBuiltInRole.Administrator);
if (!elevated) if (!elevated)
{ {
// Launches an elevated child process to re-enable device // Tell the client to launch routine to re-enable a device
string exeName = Process.GetCurrentProcess().MainModule.FileName; RequestElevationArgs eleArgs =
ProcessStartInfo startInfo = new ProcessStartInfo(exeName); new RequestElevationArgs(devicePathToInstanceId(hDevice.DevicePath));
startInfo.Verb = "runas"; RequestElevation?.Invoke(eleArgs);
startInfo.Arguments = "re-enabledevice " + devicePathToInstanceId(hDevice.DevicePath); if (eleArgs.StatusCode == RequestElevationArgs.STATUS_SUCCESS)
Process child = Process.Start(startInfo);
if (!child.WaitForExit(30000))
{
child.Kill();
}
else if (child.ExitCode == 0)
{ {
hDevice.OpenDevice(isExclusiveMode); hDevice.OpenDevice(isExclusiveMode);
} }