MemorySharp设置偏移到不起作用的地址

好的,我正在使用MemorySharp库来读取/写入游戏的内存。 我的问题是当我尝试将偏移量添加到基指针地址时Visual Studio在运行时抛出错误。 这是基本代码

 using (var m = new MemorySharp(ApplicationFinder.FromProcessName("Cube").First())) { IntPtr healthPtr = GetBaseAddress("Cube") + 0x0036B1C8; int[] offsets = {0x39c, 0x16c}; foreach(var offset in offsets) { healthPtr = m[healthPtr + offset].Read(); //I'm getting the error on this line } var healthLocation = m[m[healthPtr].Read()]; float health = healthLocation.Read(); MessageBox.Show(health.ToString()); } 

这是我的GetBaseAddress方法

 internal static IntPtr GetBaseAddress(string ProcessName) { try { Process[] L2Process = Process.GetProcessesByName(ProcessName); return L2Process[0].MainModule.BaseAddress; } catch { return IntPtr.Zero; } } 

但是当我运行这个Visual Studios时,我发现了这个错误

“MemorySharp.dll中发生了’System.ArgumentOutOfRangeException’类型的未处理exception附加信息:相对地址不能大于主模块大小。”

它指向这行代码healthPtr = m[healthPtr + offset].Read(); 不太确定我在这里做错了什么。

编辑
这是我的更新代码仍然无法正常工作

 using (var m = new MemorySharp(ApplicationFinder.FromProcessName("Cube").First())) { var healthPtr = new IntPtr(0x0036B1C8); int[] offsets = { 0x39c, 0x16c }; healthPtr = m[healthPtr].Read(); foreach (var offset in offsets) { healthPtr = m[healthPtr + offset, false].Read(); // false is here to avoid rebasing } float Health = m[healthPtr, false].Read(); // false is here to avoid rebasing MessageBox.Show(Health.ToString()); } 

编辑答案发现
我必须将最终指针读作我想要的类型。 因此,只有2个指针,我读取指针,第一个指针,然后在最后一个读取它作为值,如此

 using (var m = new MemorySharp(ApplicationFinder.FromProcessName("Cube").First())) { var healthPtr = new IntPtr(0x0036B1C8); int[] offsets = { 0x39C, 0x16C }; healthPtr = m[healthPtr].Read(); healthPtr = m[healthPtr + offsets[0], false].Read(); // false is here to avoid rebasing float Health = m[healthPtr + offsets[1],false].Read(); MessageBox.Show(Health.ToString()); } 

首先,使用MemorySharp对象的索引器已经将指针重新指向进程主模块的基址。 因此,您不需要使用函数GetBaseAddress ,您可以像这样声明指针/偏移量:

 var healthPtr = new IntPtr(0x0036B1C8); int[] offsets = { 0x39c, 0x16c }; 

现在读取指针的值

 healthPtr = m[healthPtr].Read(); // The pointer is automatically rebased 

使用偏移量浏览指针链

 foreach (var offset in offsets) { healthPtr = m[healthPtr + offset, false].Read(); // false is here to avoid rebasing } 

请注意第二个参数设置为false 。 它声明地址不得重新绑定到进程的主模块。

此外, 在第一次读取后使用偏移量很重要,否则声明偏移量的目的是什么,而不是直接将值添加到地址。 🙂

我认为不需要设置var healthLocation的行,并且将读取未知的内存地址。 你确定要这么做吗?

这个错误:

“MemorySharp.dll中发生了’System.ArgumentOutOfRangeException’类型的未处理exception附加信息:相对地址不能大于主模块大小。”

抛出是因为如果没有将第二个参数设置为false MemorySharp已经重新指定了指针(如上所述)。

如果它适合你,请告诉我。