Чтение в памяти указателя на другой адрес - C#
Формулировка задачи:
В общем у меня получается считывать только значение в памяти, а вот указатель что-то не выходит.
Вот как считываю значение:
Спасибо.
using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace Project1
{
class MyMemory
{
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 ReadMemory()
{
Process process = Process.GetProcessesByName("gta_sa").FirstOrDefault();
IntPtr processHandle = OpenProcess(PROCESS_WM_READ, false, process.Id);
int bytesRead = 0;
byte[] buffer = new byte[100];
ReadProcessMemory((int)processHandle, 0xF6C8898, buffer, buffer.Length, ref bytesRead);
MessageBox.Show(Encoding.ASCII.GetString(buffer));
}
}
}Решение задачи: «Чтение в памяти указателя на другой адрес»
textual
Листинг программы
using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms; // Тут нужно только для MessageBox.Show
namespace Project1
{
class MyMemory
{
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 ReadMemory()
{
IntPtr dllBase = IntPtr.Zero;
int dllLength = 0;
string processName = "имя процесса"; // Без ".exe"
Process process = Process.GetProcessesByName(processName ).FirstOrDefault();
IntPtr processHandle = OpenProcess(PROCESS_WM_READ, false, process.Id);
int bytesRead = 0;
byte[] buffer = new byte[15]; // Массив байт для выходного значения
byte[] addr = new byte[255]; // Массив байт для смещения
// Найти смещение dll, если надо
foreach (ProcessModule item in process.Modules)
{
if (item.ModuleName == "имя длл.dll")
{
dllBase = item.BaseAddress;
dllLength = item.ModuleMemorySize;
break;
}
}
ReadProcessMemory((int)processHandle, (int)dllBase + 0x21A190, addr, addr.Length, ref bytesRead);
int intAddr = BitConverter.ToInt32(addr, 0);
ReadProcessMemory((int)processHandle, intAddr + 0x40, buffer, buffer.Length, ref bytesRead);
MessageBox.Show(Encoding.ASCII.GetString(buffer).ToString()); // Вывод в MsgBox
}
}
}