Portable Tool for an Ideal Windows Setup
C# activatorbloatware-removalcsharpdebloatdebloaterdisable-servicesmonitoring-systemportableprivacy-toolsregistry-tweakssystem-tuningsystem-utilitytelemetrytweakerwindows-10windows-11windows-debloatwindows-defenderwindows-optimizationwindows-tweaks
refactor clean up code & fix resource leaks
Greedeks8c685f46bb83
diff --git a/.Source/GTweak/Utilities/Behaviors/ScrollFadeBehavior.cs b/.Source/GTweak/Utilities/Behaviors/ScrollFadeBehavior.csindex 5b3e692d..370fbdab 100644--- a/.Source/GTweak/Utilities/Behaviors/ScrollFadeBehavior.cs+++ b/.Source/GTweak/Utilities/Behaviors/ScrollFadeBehavior.cs@@ -7,7 +7,8 @@ namespace GTweak.Utilities.Behaviors{internal static class ScrollFadeBehavior{- public static readonly DependencyProperty EnableFadeProperty = DependencyProperty.RegisterAttached("EnableFade", typeof(bool), typeof(ScrollFadeBehavior), new PropertyMetadata(false, OnEnableFadeChanged));+ public static readonly DependencyProperty EnableFadeProperty =+ DependencyProperty.RegisterAttached("EnableFade", typeof(bool), typeof(ScrollFadeBehavior), new PropertyMetadata(false, OnEnableFadeChanged));public static bool GetEnableFade(DependencyObject obj) => (bool)obj.GetValue(EnableFadeProperty);public static void SetEnableFade(DependencyObject obj, bool value) => obj.SetValue(EnableFadeProperty, value);diff --git a/.Source/GTweak/Utilities/Behaviors/TextBoxClearBehavior.cs b/.Source/GTweak/Utilities/Behaviors/TextBoxClearBehavior.csindex 14fe84de..d06ac189 100644--- a/.Source/GTweak/Utilities/Behaviors/TextBoxClearBehavior.cs+++ b/.Source/GTweak/Utilities/Behaviors/TextBoxClearBehavior.cs@@ -8,8 +8,8 @@ namespace GTweak.Utilities.Behaviorspublic static readonly DependencyProperty ClearButtonProperty =DependencyProperty.RegisterAttached("ClearButton", typeof(bool), typeof(TextBoxClearBehavior), new PropertyMetadata(false, OnClearButtonChanged));- public static void SetClearButton(UIElement element, bool value) => element.SetValue(ClearButtonProperty, value);public static bool GetClearButton(UIElement element) => (bool)element.GetValue(ClearButtonProperty);+ public static void SetClearButton(UIElement element, bool value) => element.SetValue(ClearButtonProperty, value);private static void OnClearButtonChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){diff --git a/.Source/GTweak/Utilities/Helpers/TrustedInstaller.cs b/.Source/GTweak/Utilities/Helpers/TrustedInstaller.csindex 57937bc7..8b0fa42f 100644--- a/.Source/GTweak/Utilities/Helpers/TrustedInstaller.cs+++ b/.Source/GTweak/Utilities/Helpers/TrustedInstaller.cs@@ -50,6 +50,9 @@ namespace GTweak.Utilities.Helpers[return: MarshalAs(UnmanagedType.Bool)]private static extern bool InitializeProcThreadAttributeList(IntPtr lpAttributeList, int dwAttributeCount, int dwFlags, ref IntPtr lpSize);+ [DllImport("kernel32.dll", SetLastError = true)]+ private static extern void DeleteProcThreadAttributeList(IntPtr lpAttributeList);+private const uint MAXIMUM_ALLOWED = 0x02000000;private const uint SC_MANAGER_CONNECT = 0x0001;@@ -153,29 +156,41 @@ namespace GTweak.Utilities.Helpersprivate static bool ImpersonateSystem(){- Process[] processlist = Process.GetProcesses();IntPtr tokenHandle = IntPtr.Zero;+ Process[] winlogonList = null;- foreach (Process theProcess in processlist)+ try{- if (theProcess.ProcessName == "winlogon")+ winlogonList = Process.GetProcessesByName("winlogon");+ if (winlogonList.Length == 0){- bool token = OpenProcessToken(theProcess.Handle, MAXIMUM_ALLOWED, out tokenHandle);- if (!token)- {- return false;- }- else+ return false;+ }++ Process winlogon = winlogonList[0];++ if (!OpenProcessToken(winlogon.Handle, MAXIMUM_ALLOWED, out tokenHandle))+ {+ return false;+ }++ return ImpersonateLoggedOnUser(tokenHandle);+ }+ finally+ {+ if (winlogonList != null)+ {+ foreach (Process p in winlogonList){- token = ImpersonateLoggedOnUser(tokenHandle);- CloseHandle(theProcess.Handle);- CloseHandle(tokenHandle);- return true;+ p.Dispose();}}++ if (tokenHandle != IntPtr.Zero)+ {+ CloseHandle(tokenHandle);+ }}- CloseHandle(tokenHandle);- return false;}internal static void StartTrustedInstallerService()@@ -228,37 +243,80 @@ namespace GTweak.Utilities.Helpersinternal static void CreateProcessAsTrustedInstaller(int parentProcessId, string binaryPath, bool showWindow = false){ImpersonateSystem();- _ = new PROCESS_INFORMATION();- var siEx = new STARTUPINFOEX();- var lpSize = IntPtr.Zero;++ 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(siEx.lpAttributeList, 1, 0, ref lpSize);- IntPtr parentHandle = OpenProcess(ProcessAccessFlags.CreateProcess | ProcessAccessFlags.DuplicateHandle, false, parentProcessId);+ try+ {+ if (!InitializeProcThreadAttributeList(siEx.lpAttributeList, 1, 0, ref lpSize))+ {+ throw new Win32Exception(Marshal.GetLastWin32Error());+ }- IntPtr lpValueProc = Marshal.AllocHGlobal(IntPtr.Size);- Marshal.WriteIntPtr(lpValueProc, parentHandle);+ parentHandle = OpenProcess(ProcessAccessFlags.CreateProcess | ProcessAccessFlags.DuplicateHandle, false, parentProcessId);+ if (parentHandle == IntPtr.Zero)+ {+ throw new Win32Exception(Marshal.GetLastWin32Error());+ }++ lpValueProc = Marshal.AllocHGlobal(IntPtr.Size);+ Marshal.WriteIntPtr(lpValueProc, parentHandle);++ if (!UpdateProcThreadAttribute(siEx.lpAttributeList, 0, (IntPtr)PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, lpValueProc, (IntPtr)IntPtr.Size, IntPtr.Zero, IntPtr.Zero))+ {+ throw new Win32Exception(Marshal.GetLastWin32Error());+ }- UpdateProcThreadAttribute(siEx.lpAttributeList, 0, (IntPtr)PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, lpValueProc, (IntPtr)IntPtr.Size, IntPtr.Zero, IntPtr.Zero);+ SECURITY_ATTRIBUTES ps = new SECURITY_ATTRIBUTES();+ SECURITY_ATTRIBUTES ts = new SECURITY_ATTRIBUTES();+ ps.nLength = Marshal.SizeOf(ps);+ ts.nLength = Marshal.SizeOf(ts);++ siEx.StartupInfo = new STARTUPINFO+ {+ cb = Marshal.SizeOf(typeof(STARTUPINFO)),+ dwFlags = 0x00000001,+ wShowWindow = showWindow ? (short)5 : (short)0+ };++ uint creationFlags = EXTENDED_STARTUPINFO_PRESENT;+ if (!showWindow)+ {+ creationFlags |= CREATE_NO_WINDOW;+ }- var ps = new SECURITY_ATTRIBUTES();- var 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());+ }- STARTUPINFO startInfo = new STARTUPINFO+ CloseHandle(pInfo.hProcess);+ CloseHandle(pInfo.hThread);+ }+ finally{- cb = Marshal.SizeOf(typeof(STARTUPINFO)),- dwFlags = 0x00000001,- wShowWindow = showWindow ? (short)5 : (short)0- };+ if (lpValueProc != IntPtr.Zero)+ {+ Marshal.FreeHGlobal(lpValueProc);+ }- siEx.StartupInfo = startInfo;+ if (parentHandle != IntPtr.Zero)+ {+ CloseHandle(parentHandle);+ }- _ = CreateProcess(null, binaryPath, ref ps, ref ts, true, EXTENDED_STARTUPINFO_PRESENT, IntPtr.Zero, null, ref siEx, out PROCESS_INFORMATION pInfo);- _ = pInfo.dwProcessId.ToString();+ if (siEx.lpAttributeList != IntPtr.Zero)+ {+ DeleteProcThreadAttributeList(siEx.lpAttributeList);+ Marshal.FreeHGlobal(siEx.lpAttributeList);+ }+ }}}}diff --git a/.Source/GTweak/Utilities/Tweaks/ServicesTweaks.cs b/.Source/GTweak/Utilities/Tweaks/ServicesTweaks.csindex e375408f..c02cf61e 100644--- a/.Source/GTweak/Utilities/Tweaks/ServicesTweaks.cs+++ b/.Source/GTweak/Utilities/Tweaks/ServicesTweaks.cs@@ -264,10 +264,8 @@ namespace GTweak.Utilities.Tweaks}break;case "TglButton4":- string[] services = {"WalletService","VacSvc", "spectrum", "SharedRealitySvc","perceptionsimulation", "MixedRealityOpenXRSvc",- "MapsBroker", "EntAppSvc", "embeddedmode","wlidsvc", "WEPHOSTSVC", "StorSvc", "ClipSVC", "InstallService"};- string command = $@"/c {string.Join(" & ", services.Select(s => $@"reg add HKLM\SYSTEM\CurrentControlSet\Services\{s} /t REG_DWORD /v Start /d {(isDisabled ? "4" : "3")} /f"))}";- CommandExecutor.RunCommandAsTrustedInstaller(command);+ CommandExecutor.RunCommandAsTrustedInstaller($@"/c {string.Join(" & ", new[] { "WalletService", "VacSvc", "spectrum", "SharedRealitySvc", "perceptionsimulation", "MixedRealityOpenXRSvc", "MapsBroker", "EntAppSvc",+ "embeddedmode", "wlidsvc", "WEPHOSTSVC", "StorSvc", "ClipSVC", "InstallService" }.Select(s => $@"reg add HKLM\SYSTEM\CurrentControlSet\Services\{s} /v Start /t REG_DWORD /d {(isDisabled ? "4" : "3")} /f"))}");break;case "TglButton5":RegistryHelp.Write(Registry.LocalMachine, @"SYSTEM\CurrentControlSet\Services\wmiApSrv", "Start", isDisabled ? 4 : 3, RegistryValueKind.DWord);