C# 读取内存数据:示例代码与注意事项
要读取内存中的数据,可以使用C#中的指针操作。以下是一个示例代码:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
class Program
{
[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, int dwSize, out int lpNumberOfBytesRead);
public static void Main()
{
// 获取进程ID
int processId = Process.GetCurrentProcess().Id;
// 打开进程
IntPtr processHandle = OpenProcess(0x0010, false, processId);
// 读取内存数据
IntPtr baseAddress = new IntPtr(0x39ae8974);
int bufferSize = 4;
byte[] buffer = new byte[bufferSize];
int bytesRead = 0;
ReadProcessMemory(processHandle, baseAddress, buffer, bufferSize, out bytesRead);
// 将读取的数据转换为整数
int value = BitConverter.ToInt32(buffer, 0);
Console.WriteLine("读取的数据:'{0}'", value);
// 关闭进程句柄
CloseHandle(processHandle);
}
}
注意:这段代码使用了OpenProcess和ReadProcessMemory等Win32 API函数,需要使用System.Runtime.InteropServices命名空间,并且需要以管理员权限运行程序才能读取其他进程的内存数据。
原文地址: https://www.cveoy.top/t/topic/pdN3 著作权归作者所有。请勿转载和采集!