[BUG] Child processes won't elevate (#488), version 1.3.2.6

This commit is contained in:
Markus Hofknecht 2023-08-12 10:48:09 +02:00
parent 9683e5c461
commit 68258eb104
4 changed files with 40 additions and 3 deletions

View file

@ -23,6 +23,7 @@ namespace SystemTrayMenu
Config.SetFolderByWindowsContextMenu(args); Config.SetFolderByWindowsContextMenu(args);
Config.LoadOrSetByUser(); Config.LoadOrSetByUser();
Config.Initialize(); Config.Initialize();
PrivilegeChecker.Initialize();
if (SingleAppInstance.Initialize()) if (SingleAppInstance.Initialize())
{ {

View file

@ -39,5 +39,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.3.2.4")] [assembly: AssemblyVersion("1.3.2.6")]
[assembly: AssemblyFileVersion("1.3.2.4")] [assembly: AssemblyFileVersion("1.3.2.6")]

View file

@ -159,8 +159,15 @@ namespace SystemTrayMenu.Utilities
try try
{ {
string verb = string.Empty;
if (!PrivilegeChecker.IsCurrentUserInAdminGroup)
{
verb = "runas";
}
using Process p = new() using Process p = new()
{ {
StartInfo = new ProcessStartInfo(fileName) StartInfo = new ProcessStartInfo(fileName)
{ {
FileName = fileName, FileName = fileName,
@ -168,7 +175,7 @@ namespace SystemTrayMenu.Utilities
WorkingDirectory = workingDirectory, WorkingDirectory = workingDirectory,
CreateNoWindow = createNoWindow, CreateNoWindow = createNoWindow,
UseShellExecute = true, UseShellExecute = true,
Verb = "runas", Verb = verb,
}, },
}; };
p.Start(); p.Start();

View file

@ -0,0 +1,29 @@
// <copyright file="PrivilegeChecker.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.Utilities
{
using System.Linq;
using System.Security.Principal;
internal static class PrivilegeChecker
{
public static bool IsCurrentUserInAdminGroup { get; set; }
public static void Initialize()
{
// https://stackoverflow.com/questions/3600322/check-if-the-current-user-is-administrator
// https://learn.microsoft.com/en-us/troubleshoot/windows-server/identity/security-identifiers-in-windows
// S-1-5-32-544
// A built-in group. After the initial installation of the operating system,
// the only member of the group is the Administrator account.
// When a computer joins a domain, the Domain Admins group is added to
// the Administrators group. When a server becomes a domain controller,
// the Enterprise Admins group also is added to the Administrators group.
var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
var claims = principal.Claims;
IsCurrentUserInAdminGroup = claims.FirstOrDefault(c => c.Value == "S-1-5-32-544") != null;
}
}
}