diff --git a/src/modules/windowwalker/app/Window Walker/Components/InteropAndHelpers.cs b/src/modules/windowwalker/app/Window Walker/Components/InteropAndHelpers.cs index 9d9dcb8aa3..1f90405c0b 100644 --- a/src/modules/windowwalker/app/Window Walker/Components/InteropAndHelpers.cs +++ b/src/modules/windowwalker/app/Window Walker/Components/InteropAndHelpers.cs @@ -775,6 +775,9 @@ namespace WindowWalker.Components [DllImport("user32.dll", SetLastError = true)] public static extern int GetWindowLong(IntPtr hWnd, int nIndex); + [DllImport("user32.dll", CharSet = CharSet.Unicode)] + public static extern int EnumChildWindows(IntPtr hWnd, CallBackPtr callPtr, int lPar); + [DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount); diff --git a/src/modules/windowwalker/app/Window Walker/Components/Window.cs b/src/modules/windowwalker/app/Window Walker/Components/Window.cs index 85a062c7ad..0aa84eafdb 100644 --- a/src/modules/windowwalker/app/Window Walker/Components/Window.cs +++ b/src/modules/windowwalker/app/Window Walker/Components/Window.cs @@ -8,6 +8,7 @@ using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; +using System.Threading.Tasks; using System.Windows; using System.Windows.Interop; using System.Windows.Media; @@ -71,6 +72,8 @@ namespace WindowWalker.Components get { return hwnd; } } + public uint ProcessID { get; set; } + /// /// Gets returns the name of the process /// @@ -88,11 +91,9 @@ namespace WindowWalker.Components if (!_handlesToProcessCache.ContainsKey(Hwnd)) { - InteropAndHelpers.GetWindowThreadProcessId(Hwnd, out uint processId); - IntPtr processHandle = InteropAndHelpers.OpenProcess(InteropAndHelpers.ProcessAccessFlags.AllAccess, true, (int)processId); - StringBuilder processName = new StringBuilder(MaximumFileNameLength); + var processName = GetProcessNameFromWindowHandle(Hwnd); - if (InteropAndHelpers.GetProcessImageFileName(processHandle, processName, MaximumFileNameLength) != 0) + if (processName.Length != 0) { _handlesToProcessCache.Add( Hwnd, @@ -104,6 +105,27 @@ namespace WindowWalker.Components } } + if (_handlesToProcessCache[hwnd].ToLower() == "applicationframehost.exe") + { + new Task(() => + { + InteropAndHelpers.CallBackPtr callbackptr = new InteropAndHelpers.CallBackPtr((IntPtr hwnd, IntPtr lParam) => + { + var childProcessId = GetProcessIDFromWindowHandle(hwnd); + if (childProcessId != this.ProcessID) + { + _handlesToProcessCache[Hwnd] = GetProcessNameFromWindowHandle(hwnd); + return false; + } + else + { + return true; + } + }); + InteropAndHelpers.EnumChildWindows(Hwnd, callbackptr, 0); + }).Start(); + } + return _handlesToProcessCache[hwnd]; } } @@ -342,5 +364,38 @@ namespace WindowWalker.Components Maximized, Unknown, } + + /// + /// Gets the name of the process using the window handle + /// + /// The handle to the window + /// A string representing the process name or an empty string if the function fails + private string GetProcessNameFromWindowHandle(IntPtr hwnd) + { + uint processId = GetProcessIDFromWindowHandle(hwnd); + ProcessID = processId; + IntPtr processHandle = InteropAndHelpers.OpenProcess(InteropAndHelpers.ProcessAccessFlags.AllAccess, true, (int)processId); + StringBuilder processName = new StringBuilder(MaximumFileNameLength); + + if (InteropAndHelpers.GetProcessImageFileName(processHandle, processName, MaximumFileNameLength) != 0) + { + return processName.ToString().Split('\\').Reverse().ToArray()[0]; + } + else + { + return string.Empty; + } + } + + /// + /// Gets the process ID for the Window handle + /// + /// The handle to the window + /// The process ID + private uint GetProcessIDFromWindowHandle(IntPtr hwnd) + { + InteropAndHelpers.GetWindowThreadProcessId(hwnd, out uint processId); + return processId; + } } }