Skip to content

Commit ca01b25

Browse files
committed
Replace DllImport & flags with CSWin32
1 parent a395936 commit ca01b25

File tree

3 files changed

+31
-30
lines changed

3 files changed

+31
-30
lines changed

Plugins/Flow.Launcher.Plugin.ProcessKiller/Flow.Launcher.Plugin.ProcessKiller.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@
5050
</Content>
5151
</ItemGroup>
5252

53+
<ItemGroup>
54+
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106">
55+
<PrivateAssets>all</PrivateAssets>
56+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
57+
</PackageReference>
58+
</ItemGroup>
59+
5360
<ItemGroup>
5461
<ProjectReference Include="..\..\Flow.Launcher.Infrastructure\Flow.Launcher.Infrastructure.csproj" />
5562
<ProjectReference Include="..\..\Flow.Launcher.Plugin\Flow.Launcher.Plugin.csproj" />
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
QueryFullProcessImageName
2+
OpenProcess

Plugins/Flow.Launcher.Plugin.ProcessKiller/ProcessHelper.cs

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
using Flow.Launcher.Infrastructure;
1+
using Flow.Launcher.Infrastructure;
22
using Flow.Launcher.Infrastructure.Logger;
3+
using Microsoft.Win32.SafeHandles;
34
using System;
45
using System.Collections.Generic;
56
using System.Diagnostics;
67
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;
911

1012
namespace Flow.Launcher.Plugin.ProcessKiller
1113
{
@@ -84,43 +86,33 @@ public void TryKill(Process p)
8486
}
8587
}
8688

87-
public string TryGetProcessFilename(Process p)
89+
public unsafe string TryGetProcessFilename(Process p)
8890
{
8991
try
9092
{
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)
9595
{
96-
return String.Empty;
96+
return string.Empty;
9797
}
9898

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);
100111
}
101112
catch
102113
{
103-
return "";
114+
return string.Empty;
104115
}
105116
}
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);
125117
}
126118
}

0 commit comments

Comments
 (0)