c# - 从另一个进程内存地址读取字符串
问题描述
我有关于 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 字符串?你能告诉我并帮助我理解我的错误吗?
解决方案
要从内存中读取空终止的宽字符串,您可以使用此函数
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 进行编译。
推荐阅读
- android - 货币的 NumberFormat 在不同的 android 版本上为相同的语言环境返回不同的值
- flutter - 如何在代理后面的 Powershell 中使用 Flutter?
- c++ - 使用 msvc 15 在 Qt5.13 中编译 CUDA 代码
- wordpress - revslider 预加载密钥请求错误?
- schema - 架构未指定类型
- ajax - asp.net核心用ajax发送formfile,我不能在控制器中归档
- c# - 尽管我给出了一些偏移量,但我的游戏对象仍然紧挨着生成
- c# - 单独运行子进程时会影响原始dll
- java - 测试包含日期字段的对象
- c# - Azure Functions - C# - 运行外部应用程序?