|
| 1 | +using System.ComponentModel; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.ServiceProcess; |
| 4 | +using GamerGuardian.Models; |
| 5 | + |
| 6 | +namespace GamerGuardian.Services; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Read/modify Windows service start type and running state. |
| 10 | +/// |
| 11 | +/// Reads use <see cref="ServiceController"/> for status and the registry for |
| 12 | +/// start type (no elevation needed). Writes use sc.exe via Verb=runas — same |
| 13 | +/// pattern as <see cref="ElevatedRegistry"/>, one UAC prompt per call. |
| 14 | +/// </summary> |
| 15 | +public static class WindowsServiceController |
| 16 | +{ |
| 17 | + public static bool Exists(string serviceName) |
| 18 | + { |
| 19 | + try |
| 20 | + { |
| 21 | + using var sc = new ServiceController(serviceName); |
| 22 | + // Touching Status forces a query that throws if the service isn't installed. |
| 23 | + _ = sc.Status; |
| 24 | + return true; |
| 25 | + } |
| 26 | + catch { return false; } |
| 27 | + } |
| 28 | + |
| 29 | + public static ServiceStartType ReadStartType(string serviceName) |
| 30 | + { |
| 31 | + try |
| 32 | + { |
| 33 | + using var k = Microsoft.Win32.Registry.LocalMachine.OpenSubKey( |
| 34 | + $@"SYSTEM\CurrentControlSet\Services\{serviceName}", writable: false); |
| 35 | + if (k is null) return ServiceStartType.Unknown; |
| 36 | + if (k.GetValue("Start") is not int start) return ServiceStartType.Unknown; |
| 37 | + // 2 = Automatic; check DelayedAutostart for the "Automatic (Delayed)" variant. |
| 38 | + if (start == 2) |
| 39 | + { |
| 40 | + var delayed = k.GetValue("DelayedAutostart") is int d && d == 1; |
| 41 | + return delayed ? ServiceStartType.AutomaticDelayed : ServiceStartType.Automatic; |
| 42 | + } |
| 43 | + return start switch |
| 44 | + { |
| 45 | + 0 => ServiceStartType.Boot, |
| 46 | + 1 => ServiceStartType.System, |
| 47 | + 3 => ServiceStartType.Manual, |
| 48 | + 4 => ServiceStartType.Disabled, |
| 49 | + _ => ServiceStartType.Unknown, |
| 50 | + }; |
| 51 | + } |
| 52 | + catch { return ServiceStartType.Unknown; } |
| 53 | + } |
| 54 | + |
| 55 | + public static ServiceControllerStatus? ReadStatus(string serviceName) |
| 56 | + { |
| 57 | + try |
| 58 | + { |
| 59 | + using var sc = new ServiceController(serviceName); |
| 60 | + return sc.Status; |
| 61 | + } |
| 62 | + catch { return null; } |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Stop the service (if running) and set start type to Disabled. Single |
| 67 | + /// UAC prompt; tolerates "already stopped" / "stop pending" exit codes |
| 68 | + /// from sc.exe so the configure step still runs. |
| 69 | + /// </summary> |
| 70 | + public static bool DisableElevated(string serviceName) => |
| 71 | + RunChained( |
| 72 | + $"sc stop \"{serviceName}\"", |
| 73 | + $"sc config \"{serviceName}\" start= disabled"); |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Restore a service's start type to its default. Does not start it — the |
| 77 | + /// next reboot or trigger will pick that up if/when needed. |
| 78 | + /// </summary> |
| 79 | + public static bool RestoreDefaultElevated(string serviceName, ServiceStartType defaultStart) |
| 80 | + { |
| 81 | + var startArg = defaultStart switch |
| 82 | + { |
| 83 | + ServiceStartType.Boot => "boot", |
| 84 | + ServiceStartType.System => "system", |
| 85 | + ServiceStartType.Automatic => "auto", |
| 86 | + ServiceStartType.AutomaticDelayed => "delayed-auto", |
| 87 | + ServiceStartType.Manual => "demand", |
| 88 | + ServiceStartType.Disabled => "disabled", |
| 89 | + _ => "demand", |
| 90 | + }; |
| 91 | + return Run($"config \"{serviceName}\" start= {startArg}"); |
| 92 | + } |
| 93 | + |
| 94 | + private static bool Run(string scArgs) |
| 95 | + { |
| 96 | + var psi = new ProcessStartInfo |
| 97 | + { |
| 98 | + FileName = "sc.exe", |
| 99 | + Arguments = scArgs, |
| 100 | + Verb = "runas", |
| 101 | + UseShellExecute = true, |
| 102 | + CreateNoWindow = true, |
| 103 | + WindowStyle = ProcessWindowStyle.Hidden, |
| 104 | + }; |
| 105 | + try |
| 106 | + { |
| 107 | + using var p = Process.Start(psi); |
| 108 | + if (p is null) return false; |
| 109 | + p.WaitForExit(10_000); |
| 110 | + return p.HasExited && p.ExitCode == 0; |
| 111 | + } |
| 112 | + catch (Win32Exception) |
| 113 | + { |
| 114 | + return false; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private static bool RunChained(params string[] scCommands) |
| 119 | + { |
| 120 | + // Combine into one cmd /c call so the user only sees a single UAC prompt. |
| 121 | + // The first command (typically `sc stop`) is allowed to fail — `&` (not `&&`) |
| 122 | + // means cmd executes the second command regardless. |
| 123 | + var joined = string.Join(" & ", scCommands); |
| 124 | + var psi = new ProcessStartInfo |
| 125 | + { |
| 126 | + FileName = "cmd.exe", |
| 127 | + Arguments = $"/c {joined}", |
| 128 | + Verb = "runas", |
| 129 | + UseShellExecute = true, |
| 130 | + CreateNoWindow = true, |
| 131 | + WindowStyle = ProcessWindowStyle.Hidden, |
| 132 | + }; |
| 133 | + try |
| 134 | + { |
| 135 | + using var p = Process.Start(psi); |
| 136 | + if (p is null) return false; |
| 137 | + p.WaitForExit(15_000); |
| 138 | + // Last command's exit code wins — that's the `sc config`, which is the one we care about. |
| 139 | + return p.HasExited && p.ExitCode == 0; |
| 140 | + } |
| 141 | + catch (Win32Exception) |
| 142 | + { |
| 143 | + return false; |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments