|
1 |
| -using Flow.Launcher.Infrastructure; |
| 1 | +using Flow.Launcher.Infrastructure; |
2 | 2 | using Flow.Launcher.Infrastructure.Logger;
|
| 3 | +using Microsoft.Win32.SafeHandles; |
3 | 4 | using System;
|
4 | 5 | using System.Collections.Generic;
|
5 | 6 | using System.Diagnostics;
|
6 | 7 | using System.Linq;
|
7 |
| -using System.Runtime.InteropServices; |
8 |
| -using System.Text; |
| 8 | +using Windows.Win32; |
| 9 | +using Windows.Win32.Foundation; |
| 10 | +using Windows.Win32.System.Threading; |
9 | 11 |
|
10 | 12 | namespace Flow.Launcher.Plugin.ProcessKiller
|
11 | 13 | {
|
@@ -84,43 +86,33 @@ public void TryKill(Process p)
|
84 | 86 | }
|
85 | 87 | }
|
86 | 88 |
|
87 |
| - public string TryGetProcessFilename(Process p) |
| 89 | + public unsafe string TryGetProcessFilename(Process p) |
88 | 90 | {
|
89 | 91 | try
|
90 | 92 | {
|
91 |
| - int capacity = 2000; |
92 |
| - StringBuilder builder = new StringBuilder(capacity); |
93 |
| - IntPtr ptr = OpenProcess(ProcessAccessFlags.QueryLimitedInformation, false, p.Id); |
94 |
| - if (!QueryFullProcessImageName(ptr, 0, builder, ref capacity)) |
| 93 | + var handle = PInvoke.OpenProcess(PROCESS_ACCESS_RIGHTS.PROCESS_QUERY_LIMITED_INFORMATION, false, (uint)p.Id); |
| 94 | + if (handle.Value == IntPtr.Zero) |
95 | 95 | {
|
96 |
| - return String.Empty; |
| 96 | + return string.Empty; |
97 | 97 | }
|
98 | 98 |
|
99 |
| - return builder.ToString(); |
| 99 | + using var safeHandle = new SafeProcessHandle(handle.Value, true); |
| 100 | + uint capacity = 2000; |
| 101 | + char[] buffer = new char[capacity]; |
| 102 | + fixed (char* pBuffer = buffer) |
| 103 | + { |
| 104 | + if (!PInvoke.QueryFullProcessImageName(safeHandle, PROCESS_NAME_FORMAT.PROCESS_NAME_WIN32, (PWSTR)pBuffer, ref capacity)) |
| 105 | + { |
| 106 | + return string.Empty; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return new string(buffer, 0, (int)capacity); |
100 | 111 | }
|
101 | 112 | catch
|
102 | 113 | {
|
103 |
| - return ""; |
| 114 | + return string.Empty; |
104 | 115 | }
|
105 | 116 | }
|
106 |
| - |
107 |
| - [Flags] |
108 |
| - private enum ProcessAccessFlags : uint |
109 |
| - { |
110 |
| - QueryLimitedInformation = 0x00001000 |
111 |
| - } |
112 |
| - |
113 |
| - [DllImport("kernel32.dll", SetLastError = true)] |
114 |
| - private static extern bool QueryFullProcessImageName( |
115 |
| - [In] IntPtr hProcess, |
116 |
| - [In] int dwFlags, |
117 |
| - [Out] StringBuilder lpExeName, |
118 |
| - ref int lpdwSize); |
119 |
| - |
120 |
| - [DllImport("kernel32.dll", SetLastError = true)] |
121 |
| - private static extern IntPtr OpenProcess( |
122 |
| - ProcessAccessFlags processAccess, |
123 |
| - bool bInheritHandle, |
124 |
| - int processId); |
125 | 117 | }
|
126 | 118 | }
|
0 commit comments