c# - 使用带偏移量的基址指针读取进程内存

标签 c# process

我已经在网上搜索了几个小时,但我终究无法理解为什么下面的代码不起作用。

为进程提取的基址似乎有误。如果我将结束地址直接硬编码到 ReadMemory 中,我会得到所需的值(所以我知道我有正确的过程和所有)。

我没有发布 MemoryHandler 类,因为它正在正常工作

这可能与我在 64 位 Windows 上的事实有关吗?该游戏是 32 位的(安装在“Program Files (x86)”文件夹中)。

public partial class MainForm : Form
{

    Process myProcess = Process.GetProcessesByName("ffxiv").FirstOrDefault();

    public MainForm()
    {
        InitializeComponent();
    }

    private void startButton_Click(object sender, EventArgs e)
    {
        IntPtr baseAddress = myProcess.MainModule.BaseAddress;
        Console.WriteLine("Base Address: " + baseAddress.ToString("X"));

        IntPtr newAddr = IntPtr.Add(baseAddress, 0xF8BEFC);
        IntPtr finalAddr = IntPtr.Add(newAddr, 0x1690);

        int bytesRead;
        byte[] memoryOutput = MemoryHandler.ReadMemory(myProcess, finalAddr, 4, out bytesRead);

        int value = BitConverter.ToInt32(memoryOutput, 0);
        Console.WriteLine("Read Value: " + value);
    }
}

编辑:基地址是正确的,我围绕指针的代码逻辑是错误的,请参阅下面的完整答案。

最佳答案

因此,David 关于基地址不能是奇数的评论让我想到,也许我从 Cheat Engine 获得的数字实际上不是基地址。事实证明这是正确的。我的代码实际上是在提取正确的基地址。我之前发布的数字 (9460301) 实际上是存储在内存中基址位置的数字(不是地址本身)。

无论如何,上面的代码获取基地址并添加第一个偏移量,然后添加下一个偏移量,然后读取该地址上的内存。那是错误的,这不是多级指针的工作方式。对于每个级别,您都必须读取内存并查看地址中存储的内容。您找到的值将是您的下一个地址,您将向其应用下一个偏移量,依此类推。

正确的代码是:

public partial class MainForm : Form
{

    Process myProcess = Process.GetProcessesByName("ffxiv").FirstOrDefault();

    public MainForm()
    {
        InitializeComponent();
    }

    private void startButton_Click(object sender, EventArgs e)
    {
        int bytesRead;

        IntPtr baseAddress = myProcess.MainModule.BaseAddress;
        Console.WriteLine("Base Address: " + baseAddress);

        IntPtr firstAddress = IntPtr.Add(baseAddress, 0xF8BEFC);
        IntPtr firstAddressValue = (IntPtr)BitConverter.ToInt32(MemoryHandler.ReadMemory(myProcess, firstAddress, 4, out bytesRead), 0);
        IntPtr finalAddr = IntPtr.Add(firstAddressValue, 0x1690);
        Console.WriteLine("Final Address: " + finalAddr.ToString("X"));

        byte[] memoryOutput = MemoryHandler.ReadMemory(myProcess, finalAddr, 4, out bytesRead);

        int value = BitConverter.ToInt32(memoryOutput, 0);
        Console.WriteLine("Read Value: " + value);
    }
}

不过我想知道,如何使它不特定于 32 位 Windows?我不太喜欢 Int32() 函数和后续的 IntPtr 转换,所以想知道是否有任何方法可以直接从 byte[] 转到 IntPtr。我尝试了 marshal-copy 等,但无法正常工作。

再次感谢!

关于c# - 使用带偏移量的基址指针读取进程内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19847107/

相关文章:

linux - 如何在shell中加入多个进程?

c - 父进程跟踪子进程的终止状态

linux - "Max open files"工作进程

C# 等效于 PHP 动态方法调用

c# - 排序列表而不创建新变量

linux - Linux 进程空闲时间

objective-c - 从 PID 获取名称?

用于编程 WPF GUI 的 C# 与 F#

c# - RemoteEndPoint 提供错误的 IP 地址

C# 枚举数组接受错误值