From ca01b25a39b6c35d58b021fd153d796def9d7e05 Mon Sep 17 00:00:00 2001
From: Jack251970 <1160210343@qq.com>
Date: Tue, 10 Dec 2024 13:29:21 +0800
Subject: [PATCH] Replace DllImport & flags with CSWin32
---
.../Flow.Launcher.Plugin.ProcessKiller.csproj | 7 +++
.../NativeMethods.txt | 2 +
.../ProcessHelper.cs | 52 ++++++++-----------
3 files changed, 31 insertions(+), 30 deletions(-)
create mode 100644 Plugins/Flow.Launcher.Plugin.ProcessKiller/NativeMethods.txt
diff --git a/Plugins/Flow.Launcher.Plugin.ProcessKiller/Flow.Launcher.Plugin.ProcessKiller.csproj b/Plugins/Flow.Launcher.Plugin.ProcessKiller/Flow.Launcher.Plugin.ProcessKiller.csproj
index 876bac1e7..4e216b7b2 100644
--- a/Plugins/Flow.Launcher.Plugin.ProcessKiller/Flow.Launcher.Plugin.ProcessKiller.csproj
+++ b/Plugins/Flow.Launcher.Plugin.ProcessKiller/Flow.Launcher.Plugin.ProcessKiller.csproj
@@ -50,6 +50,13 @@
+
+
+ all
+ runtime; build; native; contentfiles; analyzers; buildtransitive
+
+
+
diff --git a/Plugins/Flow.Launcher.Plugin.ProcessKiller/NativeMethods.txt b/Plugins/Flow.Launcher.Plugin.ProcessKiller/NativeMethods.txt
new file mode 100644
index 000000000..7fa794755
--- /dev/null
+++ b/Plugins/Flow.Launcher.Plugin.ProcessKiller/NativeMethods.txt
@@ -0,0 +1,2 @@
+QueryFullProcessImageName
+OpenProcess
\ No newline at end of file
diff --git a/Plugins/Flow.Launcher.Plugin.ProcessKiller/ProcessHelper.cs b/Plugins/Flow.Launcher.Plugin.ProcessKiller/ProcessHelper.cs
index 0acc39fbb..d8e5f4fb7 100644
--- a/Plugins/Flow.Launcher.Plugin.ProcessKiller/ProcessHelper.cs
+++ b/Plugins/Flow.Launcher.Plugin.ProcessKiller/ProcessHelper.cs
@@ -1,11 +1,13 @@
-using Flow.Launcher.Infrastructure;
+using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Logger;
+using Microsoft.Win32.SafeHandles;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
-using System.Runtime.InteropServices;
-using System.Text;
+using Windows.Win32;
+using Windows.Win32.Foundation;
+using Windows.Win32.System.Threading;
namespace Flow.Launcher.Plugin.ProcessKiller
{
@@ -84,43 +86,33 @@ namespace Flow.Launcher.Plugin.ProcessKiller
}
}
- public string TryGetProcessFilename(Process p)
+ public unsafe string TryGetProcessFilename(Process p)
{
try
{
- int capacity = 2000;
- StringBuilder builder = new StringBuilder(capacity);
- IntPtr ptr = OpenProcess(ProcessAccessFlags.QueryLimitedInformation, false, p.Id);
- if (!QueryFullProcessImageName(ptr, 0, builder, ref capacity))
+ var handle = PInvoke.OpenProcess(PROCESS_ACCESS_RIGHTS.PROCESS_QUERY_LIMITED_INFORMATION, false, (uint)p.Id);
+ if (handle.Value == IntPtr.Zero)
{
- return String.Empty;
+ return string.Empty;
}
- return builder.ToString();
+ using var safeHandle = new SafeProcessHandle(handle.Value, true);
+ uint capacity = 2000;
+ char[] buffer = new char[capacity];
+ fixed (char* pBuffer = buffer)
+ {
+ if (!PInvoke.QueryFullProcessImageName(safeHandle, PROCESS_NAME_FORMAT.PROCESS_NAME_WIN32, (PWSTR)pBuffer, ref capacity))
+ {
+ return string.Empty;
+ }
+ }
+
+ return new string(buffer, 0, (int)capacity);
}
catch
{
- return "";
+ return string.Empty;
}
}
-
- [Flags]
- private enum ProcessAccessFlags : uint
- {
- QueryLimitedInformation = 0x00001000
- }
-
- [DllImport("kernel32.dll", SetLastError = true)]
- private static extern bool QueryFullProcessImageName(
- [In] IntPtr hProcess,
- [In] int dwFlags,
- [Out] StringBuilder lpExeName,
- ref int lpdwSize);
-
- [DllImport("kernel32.dll", SetLastError = true)]
- private static extern IntPtr OpenProcess(
- ProcessAccessFlags processAccess,
- bool bInheritHandle,
- int processId);
}
}