From c05c2fdd735fbe03489478e2b4a7c0c69642c93b Mon Sep 17 00:00:00 2001
From: Mira <56395159+TheXorog@users.noreply.github.com>
Date: Tue, 18 Apr 2023 13:00:23 +0200
Subject: [PATCH] Add WindowsAPI/
Will add new functions as i need them.
---
WindowsAPI/WindowsUtils.cs | 35 +++++++++++++++++++++++++++++++++++
WindowsAPI/kernel32.cs | 25 +++++++++++++++++++++++++
WindowsAPI/psapi.cs | 9 +++++++++
3 files changed, 69 insertions(+)
create mode 100644 WindowsAPI/WindowsUtils.cs
create mode 100644 WindowsAPI/kernel32.cs
create mode 100644 WindowsAPI/psapi.cs
diff --git a/WindowsAPI/WindowsUtils.cs b/WindowsAPI/WindowsUtils.cs
new file mode 100644
index 0000000..e53eedc
--- /dev/null
+++ b/WindowsAPI/WindowsUtils.cs
@@ -0,0 +1,35 @@
+using System.Runtime.CompilerServices;
+
+namespace Xorog.UniversalExtensions.WindowsAPI;
+
+public class WindowsUtils
+{
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static FileInfo? GetMainModuleFilepath(int pid)
+ {
+ var processHandle = kernel32.OpenProcess(0x0400 | 0x0010, false, pid);
+
+ if (processHandle == IntPtr.Zero)
+ {
+ return null;
+ }
+
+ const int lengthSb = 4000;
+
+ var sb = new StringBuilder(lengthSb);
+
+ string? result = null;
+
+ if (psapi.GetModuleFileNameEx(processHandle, IntPtr.Zero, sb, lengthSb) > 0)
+ {
+ result = Path.GetFullPath(sb.ToString());
+ }
+
+ kernel32.CloseHandle(processHandle);
+
+ if (result == null)
+ return null;
+
+ return new FileInfo(result);
+ }
+}
diff --git a/WindowsAPI/kernel32.cs b/WindowsAPI/kernel32.cs
new file mode 100644
index 0000000..27a32e3
--- /dev/null
+++ b/WindowsAPI/kernel32.cs
@@ -0,0 +1,25 @@
+using System.Runtime.InteropServices;
+
+namespace Xorog.UniversalExtensions.WindowsAPI;
+
+public class kernel32
+{
+ ///
+ /// Opens an existing local process object.
+ ///
+ /// The access to the process object. This access right is checked against the security descriptor for the process. This parameter can be one or more of the process access rights.
+ /// If this value is TRUE, processes created by this process will inherit the handle. Otherwise, the processes do not inherit this handle.
+ /// The identifier of the local process to be opened.
+ /// If the function succeeds, the return value is an open handle to the specified process. If the function fails, the return value is NULL.
+ [DllImport("kernel32.dll")]
+ public static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);
+
+ ///
+ /// Closes an open object handle.
+ ///
+ /// A valid handle to an open object.
+ /// If the function succeeds, the return value is true.
+ [DllImport("kernel32.dll", SetLastError = true)]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ public static extern bool CloseHandle(IntPtr hObject);
+}
diff --git a/WindowsAPI/psapi.cs b/WindowsAPI/psapi.cs
new file mode 100644
index 0000000..10b73fa
--- /dev/null
+++ b/WindowsAPI/psapi.cs
@@ -0,0 +1,9 @@
+using System.Runtime.InteropServices;
+
+namespace Xorog.UniversalExtensions.WindowsAPI;
+
+public class psapi
+{
+ [DllImport("psapi.dll")]
+ public static extern uint GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, [Out] StringBuilder lpBaseName, [In][MarshalAs(UnmanagedType.U4)] int nSize);
+}