Search code examples
c++cheat-engine

Adding 20 Byes Offset To Pointer Address


I'm trying to read a value from a pointer address + offset and not getting the correct result.

I have the following (relevant pieces) of code:

uintptr_t moduleBase = GetModuleBaseAddress(procId, L"ProgramImReading.exe");
uintptr_t pObjectManager = moduleBase + 0x237CB28;
std::vector<unsigned int> countOffset = { 0x20 };

uintptr_t totalObjects = FindDMAAddy(hProcess, pObjectManager, countOffset);
std::cout << "Current objects = " << totalObjects << std::endl;

FindDMAAddy:

    uintptr_t FindDMAAddy(HANDLE hProc, uintptr_t ptr, std::vector<unsigned int> offsets)
{
    uintptr_t   addr = ptr;
    for (unsigned int i = 0; i < offsets.size(); ++i)
    {
        ReadProcessMemory(hProc, (BYTE*)addr, &addr, sizeof(addr), 0);
        addr += offsets[i];
    }
    return addr;
}

For some reason this will not work. I can confirm the addresses are correct by opening cheat engine and entering GameAdress + Pointer, then setting the offset + 20 and getting the correct value.

When i run the code above I get a long random value "2596411228208"

I can also find the current dynamic address the pointer is pointing to and read directly from that address, but when I try to read from the pointer + offset It does not work right.


Solution

  • FindDMAAddy returns the final address of the pointer chain. Your code expects it to contain the value in that address, which it does not. You need to use ReadProcessMemory to read it.

    The correct code is:

    uintptr_t moduleBase = GetModuleBaseAddress(procId, L"ProgramImReading.exe");
    uintptr_t pObjectManager = moduleBase + 0x237CB28;
    std::vector<unsigned int> countOffset = { 0x20 };
    
    uintptr_t addr = FindDMAAddy(hProcess, pObjectManager, countOffset);
    
    int totalObjects = 0;
    
    ReadProcessMemory(hProc, (BYTE*)addr, &totalObjects, sizeof(totalObjects), 0);
    
    std::cout << "Current objects = " << totalObjects << std::endl;