MemorySharp设置偏移到一个不起作用的地址 [英] MemorySharp setting offset to an address not working

查看:91
本文介绍了MemorySharp设置偏移到一个不起作用的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Ok so I am using the MemorySharp library to read/write the memory of a game. My problem is when I try to add the offsets to the base pointer address Visual Studio throws an error during runtime. Here is the base code

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时,会引发此错误

But when I run this Visual Studios throws me this error

发生了类型为'System.ArgumentOutOfRangeException'的未处理异常 MemorySharp.dll附加信息:相对地址不能大于 主模块尺寸."

"An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in MemorySharp.dll Additional information: The relative address cannot be greater than the main module size."

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

And it points to this line of code healthPtr = m[healthPtr + offset].Read<IntPtr>(); Not quite sure what I'm doing wrong here.

编辑
这是我更新后的代码,仍然无法使用

EDIT
Here is my updated code that's still not working

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个指针时,我读取了第一个指针,然后在最后一个指针上将其读取为类似

EDIT ANSWER FOUND
I have to read the final pointer as the type I want. so with only 2 pointer I read pointer the 1st one then on the last one read it as the value like so

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());
}

推荐答案

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

First at all, using the indexer of the MemorySharp object already rebases the pointer to the base address of the main module of the process. So, you don't need to use your function GetBaseAddress and you can declare your pointer/offsets like this:

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

现在读取指针的值

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

使用偏移量浏览指针链

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

请注意第二个参数设置为false.它指出该地址一定不能基于该流程的主模块.

Note the second parameter set to false. It states that the address must not be rebased to the main module of the process.

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

Also, it's important to use the offsets after a first reading, else what is the purpose to declare an offset instead of directly add the value to the address. :)

我认为不需要设置var healthLocation的行,它将读取未知的内存地址.您确定要这样做吗?

I think the line setting the var healthLocation is not needed and will read unknown memory address. Are you sure you want to do that ?

此错误:

类型为'System.ArgumentOutOfRangeException'的未处理异常 发生在MemorySharp.dll中的附加信息:相对 地址不能大于主模块的大小."

"An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in MemorySharp.dll Additional information: The relative address cannot be greater than the main module size."

抛出

的原因是,如果您未将第二个参数设置为false(如上所述),则MemorySharp已将指针重新设置为基址.

is thrown because MemorySharp already rebases the the pointer if you do not set the second parameter to false (as explained above).

让我知道这是否适合您.

Let me know if it's all right for you.

这篇关于MemorySharp设置偏移到一个不起作用的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆