Skip to content

Portable Tool for an Ideal Windows Setup

add retry logic & error handling for TrustedInstaller process creation

Greedeks8f966862ee99
diff --git a/.Source/GTweak/Utilities/Helpers/TrustedInstaller.cs b/.Source/GTweak/Utilities/Helpers/TrustedInstaller.cs
index a112785a..ed93fecd 100644
--- a/.Source/GTweak/Utilities/Helpers/TrustedInstaller.cs
+++ b/.Source/GTweak/Utilities/Helpers/TrustedInstaller.cs
@@ -241,77 +241,96 @@ namespace GTweak.Utilities.Helpers
internal static void CreateProcessAsTrustedInstaller(int parentProcessId, string binaryPath, bool showWindow = false)
{
- ImpersonateSystem();
+ Exception lastException = null;
+ bool impersonated = false;
- STARTUPINFOEX siEx = new STARTUPINFOEX();
- IntPtr lpSize = IntPtr.Zero;
- IntPtr lpValueProc = IntPtr.Zero;
- IntPtr parentHandle = IntPtr.Zero;
+ for (int attempt = 0; attempt < 3; attempt++)
+ {
+ STARTUPINFOEX siEx = new STARTUPINFOEX();
+ IntPtr lpSize = IntPtr.Zero;
+ IntPtr lpValueProc = IntPtr.Zero;
+ IntPtr parentHandle = IntPtr.Zero;
- InitializeProcThreadAttributeList(IntPtr.Zero, 1, 0, ref lpSize);
- siEx.lpAttributeList = Marshal.AllocHGlobal(lpSize);
+ InitializeProcThreadAttributeList(IntPtr.Zero, 1, 0, ref lpSize);
+ siEx.lpAttributeList = Marshal.AllocHGlobal(lpSize);
- try
- {
- if (!InitializeProcThreadAttributeList(siEx.lpAttributeList, 1, 0, ref lpSize))
+ try
{
- throw new Win32Exception(Marshal.GetLastWin32Error());
- }
+ if (!impersonated)
+ {
+ impersonated = ImpersonateSystem();
+ }
- parentHandle = OpenProcess(ProcessAccessFlags.CreateProcess | ProcessAccessFlags.DuplicateHandle, false, parentProcessId);
- if (parentHandle == IntPtr.Zero)
- {
- throw new Win32Exception(Marshal.GetLastWin32Error());
- }
+ if (!impersonated)
+ {
+ throw new Win32Exception("Failed to impersonate SYSTEM identity");
+ }
- lpValueProc = Marshal.AllocHGlobal(IntPtr.Size);
- Marshal.WriteIntPtr(lpValueProc, parentHandle);
+ if (!InitializeProcThreadAttributeList(siEx.lpAttributeList, 1, 0, ref lpSize))
+ {
+ throw new Win32Exception(Marshal.GetLastWin32Error());
+ }
- if (!UpdateProcThreadAttribute(siEx.lpAttributeList, 0, (IntPtr)PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, lpValueProc, (IntPtr)IntPtr.Size, IntPtr.Zero, IntPtr.Zero))
- {
- throw new Win32Exception(Marshal.GetLastWin32Error());
- }
+ parentHandle = OpenProcess(ProcessAccessFlags.CreateProcess | ProcessAccessFlags.DuplicateHandle, false, parentProcessId);
+ if (parentHandle == IntPtr.Zero)
+ {
+ throw new Win32Exception(Marshal.GetLastWin32Error());
+ }
- SECURITY_ATTRIBUTES ps = new SECURITY_ATTRIBUTES();
- SECURITY_ATTRIBUTES ts = new SECURITY_ATTRIBUTES();
- ps.nLength = Marshal.SizeOf(ps);
- ts.nLength = Marshal.SizeOf(ts);
+ lpValueProc = Marshal.AllocHGlobal(IntPtr.Size);
+ Marshal.WriteIntPtr(lpValueProc, parentHandle);
- siEx.StartupInfo = new STARTUPINFO
- {
- cb = Marshal.SizeOf(typeof(STARTUPINFO)),
- dwFlags = 0x00000001,
- wShowWindow = showWindow ? (short)5 : (short)0
- };
+ if (!UpdateProcThreadAttribute(siEx.lpAttributeList, 0, (IntPtr)PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, lpValueProc, (IntPtr)IntPtr.Size, IntPtr.Zero, IntPtr.Zero))
+ {
+ throw new Win32Exception(Marshal.GetLastWin32Error());
+ }
- uint creationFlags = EXTENDED_STARTUPINFO_PRESENT;
+ SECURITY_ATTRIBUTES ps = new SECURITY_ATTRIBUTES();
+ SECURITY_ATTRIBUTES ts = new SECURITY_ATTRIBUTES();
+ ps.nLength = Marshal.SizeOf(ps);
+ ts.nLength = Marshal.SizeOf(ts);
- if (!CreateProcess(null, binaryPath, ref ps, ref ts, true, creationFlags, IntPtr.Zero, null, ref siEx, out PROCESS_INFORMATION pInfo))
- {
- throw new Win32Exception(Marshal.GetLastWin32Error());
- }
+ siEx.StartupInfo = new STARTUPINFO
+ {
+ cb = Marshal.SizeOf(typeof(STARTUPINFO)),
+ dwFlags = 0x00000001,
+ wShowWindow = showWindow ? (short)5 : (short)0
+ };
- CloseHandle(pInfo.hProcess);
- CloseHandle(pInfo.hThread);
- }
- finally
- {
- if (lpValueProc != IntPtr.Zero)
- {
- Marshal.FreeHGlobal(lpValueProc);
- }
+ if (!CreateProcess(null, binaryPath, ref ps, ref ts, true, EXTENDED_STARTUPINFO_PRESENT, IntPtr.Zero, null, ref siEx, out PROCESS_INFORMATION pInfo))
+ {
+ throw new Win32Exception(Marshal.GetLastWin32Error());
+ }
- if (parentHandle != IntPtr.Zero)
+ CloseHandle(pInfo.hProcess);
+ CloseHandle(pInfo.hThread);
+ return;
+ }
+ catch (Exception ex)
{
- CloseHandle(parentHandle);
+ lastException = ex;
}
-
- if (siEx.lpAttributeList != IntPtr.Zero)
+ finally
{
- DeleteProcThreadAttributeList(siEx.lpAttributeList);
- Marshal.FreeHGlobal(siEx.lpAttributeList);
+ if (lpValueProc != IntPtr.Zero)
+ {
+ Marshal.FreeHGlobal(lpValueProc);
+ }
+
+ if (parentHandle != IntPtr.Zero)
+ {
+ CloseHandle(parentHandle);
+ }
+
+ if (siEx.lpAttributeList != IntPtr.Zero)
+ {
+ DeleteProcThreadAttributeList(siEx.lpAttributeList);
+ Marshal.FreeHGlobal(siEx.lpAttributeList);
+ }
}
}
+
+ throw lastException ?? new Win32Exception("CreateProcessAsTrustedInstaller failed after 3 attempts");
}
}
}