首页 > 解决方案 > 从另一个进程内存地址读取字符串

问题描述

我有关于 pc 寻址的错误信息,而且我认为我的代码有问题,所以它无法从地址中找到我需要的某些信息。

我使用谷歌并找到了一种从内存地址读取字符串的方法。

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
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);

        public static void Main(string[] args)
        {
            Process process = Process.GetProcessById(10828);
            IntPtr processHandle = OpenProcess(PROCESS_WM_READ, false, process.Id);

            int bytesRead = 0;
            byte[] buffer = new byte[24]; //'Hello World!' takes 12*2 bytes because of Unicode 

            // 0x0046A3B8 is the address where I found the string, replace it with what you found
            ReadProcessMemory((int)processHandle, 0x00C716EC, buffer, buffer.Length, ref bytesRead);
            Console.WriteLine(Encoding.Unicode.GetString(buffer) + " (" + bytesRead.ToString() + "bytes)");

            Console.ReadLine();
        }
    }

我现在向你解释我想要什么,我的问题是什么有一个运行名称 gta-sa.exe 的应用程序,它的在线游戏,它有 textdraw 在游戏屏幕上显示一些字符串我使用作弊引擎找到那个字符串的地址显示作弊引擎信息是吹进程 000036F4-gta_sa.exe 地址值 00C716EC 昵称

如你看到的

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

我用作0x00C716EC地址,但它显示奇怪的信息,如 ???? 在安慰!我怎样才能得到那个 NickName 字符串?你能告诉我并帮助我理解我的错误吗?

标签: c#.netmemory-address

解决方案


要从内存中读取空终止的宽字符串,您可以使用此函数

public static string ReadNullTerminatedWString(IntPtr handle, IntPtr addr, int maxlength)
{
    var bytearray = new byte[maxlength*2];

    IntPtr bytesread = IntPtr.Zero;

    ReadProcessMemory(handle, addr, bytearray, maxlength*2, out bytesread);

    int nullterm = 0;
    while (nullterm < bytesread.ToInt64() && bytearray[nullterm] != 0)
    {
        nullterm = nullterm + 2;
    }

    string s = Encoding.Unicode.GetString(bytearray, 0, nullterm);

    return s;
}

您必须以管理员身份运行,您必须编译为与目标进程相同的位数。如果目标是 x86,则针对 x86 进行编译。


推荐阅读