【问题标题】:C# ReadProcessMemory - Accessing/Reading PointersC# ReadProcessMemory - 访问/读取指针
【发布时间】:2012-04-01 17:25:05
【问题描述】:

我有从内存中读取值的代码,当内存地址指向静态 4 字节值时该值有效,但我正在尝试访问位于动态位置的 4 字节值,因此需要搜索先指针再搜索,得到4字节的值。

下面是我的代码,它应该返回指针的地址,但它只输出 0...

bAddr = (IntPtr)0x0017C370; // Base address to find the Pointer (Currently: 0x00267A50)
ReadProcessMemory(hProc, bAddr, buffer, 4, out bytesRW);
output = BitConverter.ToInt32(buffer, 0);
txtOutput.Text = output.ToString();

我看到的伪代码如下:

bAddr = (IntPtr)0x0017C370; // Base address to find the Pointer (Currently: 0x00267A50)
ReadProcessMemory(hProc, bAddr, buffer, 4, out bytesRW);
output = BitConverter.ToInt32(buffer, 0);
bAddr = (IntPtr)output; // Should now contain the address 0x00267A50
ReadProcessMemory(hProc, bAddr, buffer, 4, out bytesRW);
output = BitConverter.ToInt32(buffer, 0);
txtOutput.Text = output.ToString();

谁能阐明我需要做什么才能找到地址然后搜索该地址以找到值?

【问题讨论】:

  • ReadProcessMemory 返回什么?它可能只是失败了,在这种情况下GetLastError 可能会有所启发。

标签: c# memory pointers


【解决方案1】:

当使用 pinvoke 执行 Win32 函数时,这是一个非常经典的错误,您没有进行任何错误检查。因此,任何故障都是无法诊断的。首先确保您正确声明它:

[DllImport("user32.dll", SetLastError = true)]
static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, 
    [In, Out] byte[] buffer, IntPtr size, out IntPtr lpNumberOfBytesRead);

然后这样执行:

bool ok = ReadProcessMemory(...);
if (!ok) throw new System.ComponentModel.Win32Exception();

现在您会知道为什么它不起作用了。在您至少以这种方式进行测试之前,我们无法帮助您找出问题所在。最基本的问题当然是地址猜错了。由于没有足够的权限,ReadProcessMemory 是一个高权限函数,原因很明显。

【讨论】:

  • 这个。始终始终检查 p/invoke 调用的 hresults - 如果您不想真正throw 出错,只需创建一个新的 Win32Exception(),所有错误详细信息都会为您填充。
猜你喜欢
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-30
  • 2018-11-02
  • 1970-01-01
相关资源
最近更新 更多