Search code examples
c++pointersreadprocessmemorycheat-engine

C++ ReadProcessMemory, base addresses


I tried to readprocessmemory of a game called wesnoth. But when i debug it the value is set to 0 for both bytes_read and gold_value.

But When i go into cheatengine and paste the pointer for the gold value(0x017EED8) it gives me the value (252187896).

So i guess there is something wrong which makes the program not read the gold value.

I tried to restart the program several times but it gave the value 0 for both gold_value and bytes_read every time.

code:

int main(int argc, char** argv) {
    HWND wesnoth_window = FindWindow(0, L"The Battle for Wesnoth - 1.14.9");

    DWORD process_id = 0;
    GetWindowThreadProcessId(wesnoth_window, &process_id);

    HANDLE wesnoth_process = OpenProcess(PROCESS_ALL_ACCESS, true, process_id);

    DWORD gold_value = 0;
    DWORD bytes_read = 0;
    ReadProcessMemory(wesnoth_process, (void*)0x017EED8, &gold_value, 4, &bytes_read);  
    return 0;
}

Solution

  • The address you want to read from is an offset that is relative to the base address of the target process. When calling ReadProcessMemory(), you have to give it an absolute address that is within the process. So, you need to retrieve the base address of the target process and add the desired offset before you can then read from that address. See Get base address of process for an example.