尝试读取或写入受保护的内存前提是:其他的应用程序内存地址(0x012E6F54),如何读取它和改变它
unsafe
{
uint* ptr = (uint*)0x012E6F54;
MessageBox.Show((*ptr).ToString());
//这里出现错误:尝试读取或写入受保护的内存
}
------解决方案--------------------[DllImport( "kernel32.dll ")]
static extern bool ReadProcessMemory(uint hProcess, IntPtr lpBaseAddress,
IntPtr lpBuffer, uint nSize, ref uint lpNumberOfBytesRead);
[DllImport( "kernel32.dll ")]
static extern uint GetCurrentProcess();
private void button1_Click(object sender, EventArgs e)
{
int[] i = { 1234 };
IntPtr vBaseAddress = Marshal.UnsafeAddrOfPinnedArrayElement(i, 0);
byte[] vBuffer = new byte[4];
IntPtr vBytesAddress = Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0);
uint vNumberOfBytesRead = 0;
ReadProcessMemory(GetCurrentProcess(), vBaseAddress,
vBytesAddress, 4, ref vNumberOfBytesRead);
int vInt = Marshal.ReadInt32(vBytesAddress);
Text = vInt.ToString();
}
读取其它进程的内存需要使用如下Api函数
GetWindowThreadProcessId() // 根据窗体句柄得到进程ID
OpenProcess() // 打开进程句柄
ReadProcessMemory() // 通过进程句柄读取内存数据
WriteProcessMemory() // 通过进程句柄写入内存数据
如上代码示例读取本身进程的内存数据
更多代码你可以通过搜索引擎搜如上API函数