Add WindowsAPI/

Will add new functions as i need them.
This commit is contained in:
Mira 2023-04-18 13:00:23 +02:00
parent efe00f353f
commit c05c2fdd73
Signed by untrusted user who does not match committer: Xorog
GPG key ID: 983798ED9C3E7C36
3 changed files with 69 additions and 0 deletions

View file

@ -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);
}
}