C# ReadProcessMemory:如何读取64位内存地址?

6
我正在研究应用程序内存。我使用CheatEngine获取内存地址,然后尝试返回其值。然而,CheatEngine似乎返回64位内存地址,所以每当我输入地址时,我的ReadProcessMemory函数就告诉我无法将'long'转换为'int'。我找到的所有教程都似乎基于像00AB5678这样的内存地址,但我得到的内存地址更像是D3569227FC。
那么我的问题是,如何使用ReadProcessMemory处理更大的内存地址?
以下是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace ConsoleApplication1
{
    class Program
    {
        const int PROCESS_WM_READ = 0x0010;

        [DllImport("kernel32.dll")]
        public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);

        [DllImport("kernel32.dll")]
        public static extern bool ReadProcessMemory(int hProcess,
        int lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead);

        static void Main(string[] args)
        {
            Process process = Process.GetProcessesByName("MyProgram")[0]; 
            IntPtr processHandle = OpenProcess(PROCESS_WM_READ, false, process.Id); 

            int bytesRead = 0;
            byte[] buffer = new byte[24]; //To read a 24 byte unicode string

            ReadProcessMemory((int)processHandle, 0xD5369227FC, buffer, buffer.Length, ref bytesRead);

            Console.WriteLine(Encoding.Unicode.GetString(buffer) + 
                  " (" + bytesRead.ToString() + "bytes)");
            Console.ReadLine();
        }
    }
}

编辑:我已将我的 C# 应用程序转换为 64 位应用程序,方法是进入 VS2012-->项目-->应用程序名称属性-->生成-->平台目标-->更改为 "x64",现在我只需要知道如何修改代码来读取 64 位地址。


1
你在哪一行遇到错误?如果是在ReadProcessMemory函数中,请尝试将int转换为long... @user1928362 - khaled4vokalz
1
错误确实出现在ReadProcessMemory函数中,它的读取内容如下:“ConsoleApplication1.Program.ReadProcessMemory(int, int, byte[], int, ref int)的最佳重载方法匹配存在一些无效参数”,然后跟着是“第二个参数:无法将‘long’转换为‘int’”。问题不在第一个参数上,而是在第二个参数上。如果我将地址更改为类似于0x00AFBCD7这样的内容,它就可以正常工作,但它不接受CheatEngine给我的地址。 - KillerKode
1个回答

18
你可以将lpBaseAddress作为Int64类型进行传递。尝试替换你的。
[DllImport("kernel32.dll")]
    public static extern bool ReadProcessMemory(int hProcess,
    int lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead);

[DllImport("kernel32.dll")] 
public static extern bool ReadProcessMemory(int hProcess,
    Int64 lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead); 

但最正确的实现:

[DllImport("kernel32.dll")]
static extern bool ReadProcessMemory(IntPtr hProcess,
    IntPtr lpBaseAddress, [Out] byte[] lpBuffer, int dwSize, out IntPtr lpNumberOfBytesRead);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接