Skip to content

Commit 3e54889

Browse files
committed
Add GetProcessNameFromHwnd
1 parent 4f4ef9a commit 3e54889

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/Win32Helper.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.Win32.SafeHandles;
22
using System;
3+
using System.IO;
34
using Windows.Win32;
45
using Windows.Win32.Foundation;
56
using Windows.Win32.System.Threading;
@@ -8,16 +9,21 @@ namespace Flow.Launcher.Plugin.DialogJump.Files;
89

910
public static class Win32Helper
1011
{
12+
internal static unsafe string GetProcessNameFromHwnd(HWND hWnd)
13+
{
14+
return Path.GetFileName(GetProcessPathFromHwnd(hWnd));
15+
}
16+
1117
internal static unsafe string GetProcessPathFromHwnd(HWND hWnd)
1218
{
1319
uint pid;
14-
uint threadId = PInvoke.GetWindowThreadProcessId(hWnd, &pid);
20+
var threadId = PInvoke.GetWindowThreadProcessId(hWnd, &pid);
1521
if (threadId == 0) return string.Empty;
1622

17-
HANDLE process = PInvoke.OpenProcess(PROCESS_ACCESS_RIGHTS.PROCESS_QUERY_LIMITED_INFORMATION, false, pid);
23+
var process = PInvoke.OpenProcess(PROCESS_ACCESS_RIGHTS.PROCESS_QUERY_LIMITED_INFORMATION, false, pid);
1824
if (process != HWND.Null)
1925
{
20-
using SafeProcessHandle safeHandle = new((nint)process.Value, true);
26+
using var safeHandle = new SafeProcessHandle((nint)process.Value, true);
2127
uint capacity = 2000;
2228
Span<char> buffer = new char[capacity];
2329
if (!PInvoke.QueryFullProcessImageName(safeHandle, PROCESS_NAME_FORMAT.PROCESS_NAME_WIN32, buffer, ref capacity))

0 commit comments

Comments
 (0)