mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-01-18 14:41:21 +08:00
[Window Walker] Handle UWP process names (#1858)
* Fixed UWP app search * Use new methods * Updated comments
This commit is contained in:
parent
4f29f998c5
commit
cb454734b0
@ -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);
|
||||
|
||||
|
@ -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; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets returns the name of the process
|
||||
/// </summary>
|
||||
@ -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,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the process using the window handle
|
||||
/// </summary>
|
||||
/// <param name="hwnd">The handle to the window</param>
|
||||
/// <returns>A string representing the process name or an empty string if the function fails</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the process ID for the Window handle
|
||||
/// </summary>
|
||||
/// <param name="hwnd">The handle to the window</param>
|
||||
/// <returns>The process ID</returns>
|
||||
private uint GetProcessIDFromWindowHandle(IntPtr hwnd)
|
||||
{
|
||||
InteropAndHelpers.GetWindowThreadProcessId(hwnd, out uint processId);
|
||||
return processId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user