Add WindowsAPI/
Will add new functions as i need them.
This commit is contained in:
parent
efe00f353f
commit
c05c2fdd73
3 changed files with 69 additions and 0 deletions
35
WindowsAPI/WindowsUtils.cs
Normal file
35
WindowsAPI/WindowsUtils.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
25
WindowsAPI/kernel32.cs
Normal file
25
WindowsAPI/kernel32.cs
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
namespace Xorog.UniversalExtensions.WindowsAPI;
|
||||||
|
|
||||||
|
public class kernel32
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Opens an existing local process object.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="processAccess">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.</param>
|
||||||
|
/// <param name="bInheritHandle">If this value is TRUE, processes created by this process will inherit the handle. Otherwise, the processes do not inherit this handle.</param>
|
||||||
|
/// <param name="processId">The identifier of the local process to be opened.</param>
|
||||||
|
/// <returns>If the function succeeds, the return value is an open handle to the specified process. If the function fails, the return value is NULL.</returns>
|
||||||
|
[DllImport("kernel32.dll")]
|
||||||
|
public static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Closes an open object handle.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hObject">A valid handle to an open object.</param>
|
||||||
|
/// <returns>If the function succeeds, the return value is true.</returns>
|
||||||
|
[DllImport("kernel32.dll", SetLastError = true)]
|
||||||
|
[return: MarshalAs(UnmanagedType.Bool)]
|
||||||
|
public static extern bool CloseHandle(IntPtr hObject);
|
||||||
|
}
|
||||||
9
WindowsAPI/psapi.cs
Normal file
9
WindowsAPI/psapi.cs
Normal file
|
|
@ -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);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue