From 4f86994ea0cd670724c1d7eebabbb5fdd58b1666 Mon Sep 17 00:00:00 2001 From: Sparronator9999 <86388887+Sparronator9999@users.noreply.github.com> Date: Wed, 8 Jan 2025 07:30:17 +1100 Subject: [PATCH] Fix unnecessary CMD windows spawning on service start/stop/install/uninstall - Add `admin` option to RunCmd to choose whether a process is created as admin - Only use shell execute if unprivileged process is trying to run a new process as admin --- YAMDCC.Common/Utils.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/YAMDCC.Common/Utils.cs b/YAMDCC.Common/Utils.cs index 9fe41cb..f295650 100644 --- a/YAMDCC.Common/Utils.cs +++ b/YAMDCC.Common/Utils.cs @@ -227,15 +227,23 @@ private static void DeleteInstallUtilLogs() } } - private static int RunCmd(string exe, string args) + public static int RunCmd(string exe, string args, bool admin = true) { + bool shellExecute = false; + if (admin && !IsAdmin()) + { + // if running unprivileged, we can't create an admin process + // directly, so use shell execute (creating new cmd window) instead + shellExecute = true; + } + Process p = new() { StartInfo = new ProcessStartInfo(exe) { CreateNoWindow = true, - UseShellExecute = true, - Verb = "runas", + UseShellExecute = shellExecute, + Verb = admin ? "runas" : string.Empty, Arguments = args, }, };