我对 C++ 的了解很模糊,但我在 C# 方面有一些丰富的经验,尽管在这种情况下它不是那么有用。我在 C# 中有一些代码,它工作得很好。我有我认为在 C++ 中非常相似的代码,我似乎无法让它工作或调试它。因此,这是我编写并经过彻底测试的 C# 代码:
[DllImport("kernel32.dll")]
public static extern Int32 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,
[In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesRead);
public byte[] ReadBytes(IntPtr Handle, Int64 Address, uint BytesToRead)
{
IntPtr ptrBytesRead;
byte[] buffer = new byte[BytesToRead];
ReadProcessMemory(Handle, new IntPtr(Address), buffer, BytesToRead, out ptrBytesRead);
return buffer;
}
public int ReadInt32(long Address, uint length = 4, IntPtr? Handle = null)
{
return BitConverter.ToInt32(ReadBytes(getIntPtr(Handle), Address, length), 0);
}
因此,该函数 ReadInt32 获取一个地址,将其添加到我在初始化 Util 类时存储的基地址中,并使用在初始化时再次获取的句柄读取最多 4 个字节的内存。如果我正确设置了值,我 100% 知道这是可行的。
这段代码有点长,请原谅我的无知,但我不想把这些留给想象,我不能开始暗示可能不正确的地方,因为我在 C++ 世界中没有受过很好的训练。
DWORD address = 0x3C02A8;
int value = 0;
DWORD pid;
HWND hwnd;
DWORD baseAddress;
DWORD toread;
SIZE_T bytesRead;
// Get a process handle
hwnd = FindWindow(NULL,L"Tibia - Knightski"); //Finds the Window called "Minesweeper"
if(!hwnd) //If none, display an error
{
cout <<"Window not found!\n";
cin.get();
}
else
{
cout << "Window found: " << hwnd << endl;
cin.get();
}
// Get a base address
GetWindowThreadProcessId(hwnd,&pid); //Get the process id and place it in pid
HANDLE phandle = OpenProcess(PROCESS_VM_READ,0,pid); //Get permission to read
if(!phandle) //Once again, if it fails, tell us
{
cout <<"Could not get handle!\n";
cin.get();
}
else
{
cout << "Handle obtained: " << phandle << endl;
cin.get();
baseAddress = (DWORD)phandle;
cout << "Base Address obtained: " << baseAddress << endl;
cin.get();
}
toread = baseAddress + address;
// Read memory at base + address
if (ReadProcessMemory(phandle, (void*)address, &value, 4, &bytesRead))
{
cout << value;
cin.get();
}
else
{
cout << "Failed to read memory" << GetLastError() << endl;
cout << "Bytes read: " << bytesRead << endl;
cin.get();
}
我试图读取的内存地址是 0x3BE1E0,但我已将基地址 (0x20C8) 添加到此以获得 0x3C02A8。我将假设本网站上的读者知道句柄以及不知道的内容...
感谢您的时间。请注意,我希望能解释我做错了什么而不是答案,所以如果你有空的话,请记住这一点。一个答案就可以了,因为无论如何我很可能会研究结果。
输出是这样的:
Window found: 00D5014C
Handle obtained: 00000044
Base Address obtained: 68
Failed to read memory299
Bytes read: 0