Skip to content

Commit fc9167a

Browse files
authored
fix stale PID causes crash on elevated tweaks (#130)
1 parent 17b49be commit fc9167a

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

.Source/GTweak/Utilities/Helpers/TrustedInstaller.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,19 @@ private enum ProcessAccessFlags : uint
153153
Synchronize = 0x00100000
154154
}
155155

156+
private static bool IsProcessRunning(int processId)
157+
{
158+
try
159+
{
160+
using Process process = Process.GetProcessById(processId);
161+
return !process.HasExited;
162+
}
163+
catch (ArgumentException)
164+
{
165+
return false;
166+
}
167+
}
168+
156169
private static bool ImpersonateSystem()
157170
{
158171
IntPtr tokenHandle = IntPtr.Zero;
@@ -269,6 +282,12 @@ internal static void CreateProcessAsTrustedInstaller(int parentProcessId, string
269282
throw new Win32Exception("Failed to impersonate SYSTEM identity");
270283
}
271284

285+
if (!IsProcessRunning(parentProcessId))
286+
{
287+
StartTrustedInstallerService();
288+
parentProcessId = CommandExecutor.PID;
289+
}
290+
272291
attributeList = Marshal.AllocHGlobal((IntPtr)(long)lpSize);
273292

274293
if (!InitializeProcThreadAttributeList(attributeList, 1, 0, ref lpSize))

0 commit comments

Comments
 (0)