读取进程C++的内存
Read Memory of Process C++
我正在尝试读取地址的值,但我似乎真的做不到。我正在尝试获取:客户端.dll + 0xA9C0DC + 0x00FC .我只是想从游戏中读取玩家的健康。这是我的代码:
#include <iostream>
#include <Windows.h>
#include <string>
DWORD pid;
DWORD Address = 0xA9C0DC;
int cHealth;
int main()
{
HWND hWnd = FindWindowA(0, ("Counter-Strike: Global Offensive"));
GetWindowThreadProcessId(hWnd, &pid);
HANDLE pHandle = OpenProcess(PROCESS_VM_READ, FALSE, pid);
while(true)
{
ReadProcessMemory(pHandle, (LPVOID)(Address + 0x00FC), &cHealth,
sizeof(cHealth), 0);
std::cout << cHealth <<std::endl;
Sleep(200);
}
return 0;
}
我没有尝试(Address + 0x00FC)
DWORD Address = 0xA9C0DC + 0x00FC;
或
DWORD Address1 = 0xA9C0DC;
DWORD offset = 0x00FC;
DWORD Address = Address1 + offset; //or DWORD Address = (DWORD)(Address1 + offset)
似乎什么都不起作用。我可以得到一些帮助吗?
必须首先获取客户端.dll模块的基址。为此,您可以使用 ToolHelp32Snapshot()
遍历模块列表,找到匹配的模块并读取 modBaseAddr
成员变量。
下面是执行此操作的示例代码:
uintptr_t GetModuleBaseAddress(DWORD dwProcID, char* szModuleName)
{
uintptr_t ModuleBaseAddress = 0;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, dwProcID);
if (hSnapshot != INVALID_HANDLE_VALUE)
{
MODULEENTRY32 ModuleEntry32;
ModuleEntry32.dwSize = sizeof(MODULEENTRY32);
if (Module32First(hSnapshot, &ModuleEntry32))
{
do
{
if (strcmp(ModuleEntry32.szModule, szModuleName) == 0)
{
ModuleBaseAddress = (uintptr_t)ModuleEntry32.modBaseAddr;
break;
}
} while (Module32Next(hSnapshot, &ModuleEntry32));
}
CloseHandle(hSnapshot);
}
return ModuleBaseAddress;
}
然后做:
//get base address
uintptr_t clientdllbaseaddr = GetModuleBaseAddress(dwProcId, "client.dll");
//add relative offset to get to pointer
uintptr_t playerPtr = clientdllbaseaddr + 0xA9C0DC;
//dereference the pointer using RPM, this gives you the dynamic address of the player object
uintptr_t playerObjectAddr;
ReadProcessMemory(pHandle, (LPVOID)playerPtr, &playerObjectAddr, sizeof(playerObjectAddr), NULL);
//add health offset
uintptr_t healthAddress = playerObjectAddr + 0xFC;
//Overwrite the value
int newValue = 1337;
WriteProcessMemory(pHandle, (LPVOID)healthAddress, &newvalue, sizeof(newValue), NULL);
请注意,我使用的是与架构无关的 typedef uintptr_t
:当用 x86 编译时,它将解析为 32 位变量,在 x64 中解析为 64 位值,因此您需要在游戏使用的任何架构中编译您的项目。现在开始执行此操作很有帮助,这样您将来迁移到 x64 游戏时就不必更改所有代码。
另请注意,我不使用 VirtualProtectEx()
来获取读/写权限,因为数据部分通常不需要它,但如果您弄乱了代码部分,则需要使用它。
DWORD Address = 0xA9C0DC;
long long Address = 0xA9C0DC;
我只是把它改成很长的长。如果这个剂量不起作用,那么地址就存在一些问题。
也可能是你使用了错误的位(我对位了解不多,32、64、84(,因为我认为你可能使用了错误的位
相关文章:
- 将字符串存储在c++中的稳定内存中
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- Win32编译器选项和内存分配
- 当vector是tje全局变量时,c++中vector的内存管理
- 带内存和隔离功能的SQLite
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 迭代时从向量和内存中删除对象
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 为什么示例代码访问IUnknown中已删除的内存
- 如何在C++类内存结构中创建"spacer"?
- 从构造函数抛出异常时如何克服内存泄漏
- malloc() 可能出现内存泄漏
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 如何针对特定情况调试和修复此双自由内存损坏问题
- 类型总是使用其大小存储在内存中吗
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存