【问题标题】:C# ReadProcessMemory: How to read a 64 bit memory address?C# ReadProcessMemory:如何读取 64 位内存地址?
【发布时间】:2016-03-31 18:08:14
【问题描述】:

我开始阅读应用程序内存。我正在使用 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();
        }
    }
}

编辑:我已经通过进入 VS2012-->Project-->ApplicationName Properties-->Build-->Platform Target-->更改为“x64”将我的 C# 应用程序转换为 64 位应用程序,现在我只是需要知道如何更改我的代码以读取 64 位地址。

【问题讨论】:

  • 你在哪一行得到错误?如果它在 ReadProcessMemory 函数中,请尝试将 int 强制转换为 lons 强制转换......@user1928362
  • 错误确实出在 ReadProcessMemory 函数中,它的内容完全一样:“ConsoleApplication1.Program.ReadProcessMemory(int, int, byte[], int, ref int) 的最佳重载方法匹配”有一些无效的参数”,然后是“参数 2:无法从 'long' 转换为 'int'”。问题不在于第一个论点,而在于第二个论点。如果我将地址更改为 0x00AFBCD7 之类的地址,它可以正常工作,但它不会接受 CheatEngine 给我的地址。

标签: c# 64-bit readprocessmemory


【解决方案1】:

您可以将 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多