Skip to content

Commit

Permalink
Fix unnecessary CMD windows spawning on service start/stop/install/un…
Browse files Browse the repository at this point in the history
…install

- 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
  • Loading branch information
Sparronator9999 committed Jan 7, 2025
1 parent 30e316d commit 4f86994
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions YAMDCC.Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};
Expand Down

0 comments on commit 4f86994

Please sign in to comment.