好的,所以我正在使用该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<IntPtr>(); //I'm getting the error on this line
}
var healthLocation = m[m[healthPtr].Read<IntPtr>()];
float health = healthLocation.Read<float>();
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’类型的未处理异常附加信息:相对地址不能大于主模块大小。”
它指向这行代码healthPtr = m[healthPtr + offset].Read<IntPtr>();
不太确定我在这里做错了什么。
编辑
这是我的更新代码,但仍然无法正常工作
using (var m = new MemorySharp(ApplicationFinder.FromProcessName("Cube").First()))
{
var healthPtr = new IntPtr(0x0036B1C8);
int[] offsets = { 0x39c, 0x16c };
healthPtr = m[healthPtr].Read<IntPtr>();
foreach (var offset in offsets)
{
healthPtr = m[healthPtr + offset, false].Read<IntPtr>(); // false is here to avoid rebasing
}
float Health = m[healthPtr, false].Read<float>(); // 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<IntPtr>();
healthPtr = m[healthPtr + offsets[0], false].Read<IntPtr>(); // false is here to avoid rebasing
float Health = m[healthPtr + offsets[1],false].Read<float>();
MessageBox.Show(Health.ToString());
}