From f743a7676119f18228b1a2c4c1bc3c59d6f158e0 Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Sun, 20 Apr 2025 22:31:47 -0500 Subject: [PATCH] Fix process handle leak (#136) --- src/ManagedShell.Common/Helpers/ShellHelper.cs | 3 ++- src/ManagedShell.Interop/NativeMethods.Kernel32.cs | 3 +++ src/ManagedShell.WindowsTray/ExplorerTrayService.cs | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ManagedShell.Common/Helpers/ShellHelper.cs b/src/ManagedShell.Common/Helpers/ShellHelper.cs index 05f85a1b..d3d972ef 100644 --- a/src/ManagedShell.Common/Helpers/ShellHelper.cs +++ b/src/ManagedShell.Common/Helpers/ShellHelper.cs @@ -340,6 +340,7 @@ public static string GetPathForWindowHandle(IntPtr hWnd) // get path int len = outFileName.Capacity; QueryFullProcessImageName(hProc, 0, outFileName, ref len); + CloseHandle(hProc); } return outFileName.ToString(); @@ -403,7 +404,7 @@ public static string GetAppUserModelIdForHandle(IntPtr hWnd) StringBuilder outAumid = new StringBuilder((int)len); GetApplicationUserModelId(hProcess, ref len, outAumid); - CloseHandle((int)hProcess); + CloseHandle(hProcess); if (outAumid.Length > 0) { diff --git a/src/ManagedShell.Interop/NativeMethods.Kernel32.cs b/src/ManagedShell.Interop/NativeMethods.Kernel32.cs index 4bbc3e32..7f01c46c 100644 --- a/src/ManagedShell.Interop/NativeMethods.Kernel32.cs +++ b/src/ManagedShell.Interop/NativeMethods.Kernel32.cs @@ -71,6 +71,9 @@ public static extern SafeFileHandle CreateFile([MarshalAs(UnmanagedType.LPTStr)] [DllImport(Kernel32_DllName)] public static extern Int32 CloseHandle(Int32 Handle); + [DllImport(Kernel32_DllName)] + public static extern bool CloseHandle(IntPtr Handle); + [DllImport(Kernel32_DllName, SetLastError = true, CallingConvention = CallingConvention.Winapi)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool IsWow64Process( diff --git a/src/ManagedShell.WindowsTray/ExplorerTrayService.cs b/src/ManagedShell.WindowsTray/ExplorerTrayService.cs index 45cab2f5..45bf6eea 100644 --- a/src/ManagedShell.WindowsTray/ExplorerTrayService.cs +++ b/src/ManagedShell.WindowsTray/ExplorerTrayService.cs @@ -106,7 +106,7 @@ private void GetTrayItems() VirtualFreeEx(hProcess, hBuffer, 0, AllocationType.Release); - CloseHandle((int)hProcess); + CloseHandle(hProcess); } private IntPtr FindExplorerTrayToolbarHwnd()