41 lines
1.1 KiB
C#
41 lines
1.1 KiB
C#
using System;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace LightlessSync.Utils;
|
|
|
|
internal static class MemoryProcessProbe
|
|
{
|
|
[DllImport("kernel32.dll")]
|
|
private static extern nint GetCurrentProcess();
|
|
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
private static extern bool ReadProcessMemory(
|
|
nint hProcess,
|
|
nint lpBaseAddress,
|
|
byte[] lpBuffer,
|
|
int dwSize,
|
|
out nint lpNumberOfBytesRead);
|
|
|
|
private static readonly nint _proc = GetCurrentProcess();
|
|
|
|
public static bool TryReadIntPtr(nint address, out nint value)
|
|
{
|
|
value = nint.Zero;
|
|
|
|
if (address == nint.Zero)
|
|
return false;
|
|
|
|
if ((ulong)address < 0x10000UL)
|
|
return false;
|
|
|
|
var buf = new byte[IntPtr.Size];
|
|
if (!ReadProcessMemory(_proc, address, buf, buf.Length, out var read) || read != (nint)buf.Length)
|
|
return false;
|
|
|
|
value = IntPtr.Size == 8
|
|
? (nint)BitConverter.ToInt64(buf, 0)
|
|
: (nint)BitConverter.ToInt32(buf, 0);
|
|
|
|
return true;
|
|
}
|
|
} |