C ++ Reading memory address / pointer and offset

So, I injected the DLL into the process (game) so that I can read from the process memory.

I want to get the current game time and I found the static base address and its offset using Cheat Engine:

"game.exe" + 0158069C

Displacement: 14

And this is the code I was trying to get the float value (current game timer):

//Offsets
#define BASETIME 0x158069C
#define OFFSET 0x14

void CurrentTime() {

    float *time;
    DWORD *BaseAddress = (DWORD*)GetModuleHandle(NULL);
    DWORD *BaseTimeAddress = (DWORD*)(BaseAddress + BASETIME);
    time = (float*)(BaseTimeAddress + OFFSET);

    if (BaseTimeAddress && time) //Check the addresses, not values.
    {
        std::cout << "Base Address : " << BaseAddress << endl; // Correct
        std::cout << "Base Time Address &: " << &BaseTimeAddress << endl; // Not correct
        std::cout << "Base Time Address : " << BaseTimeAddress << endl; // Not correct
        std::cout << "Time Value : " << *time << endl; // Not correct
    }
}

      

The cout of the base address is correct (I can check it with the Cheat Engine), but after that everything is wrong, can you help me? I'm stuck with this and I've tried a lot of things ...: /

Thank you in advance,

+3


source to share


2 answers


I assume you want to increment your pointer to OFFSET

and BASETIME

bytes. If so, your code is not incremented by a byte. Instead, it is incremented by sizeof(DWORD) * OFFSET

bytes.

The reason is that the type of the underlying pointer DWORD*

, and pointers of this type n

do not take you to n * sizeof(DWORD)

the beginning. It won't work.

The simplest solution is to cast to char *

when doing pointer arithmetic, so the increment is done with sizeof(char)

, not sizeof(DWORD)

:



 DWORD *BaseTimeAddress = (DWORD*)((char *)BaseAddress + BASETIME);
 time = (float*)((char *)BaseTimeAddress + OFFSET);

      

Now where do you end up finding the data you want, I cannot answer. However, if your goal was to increase the byte, then you should make the corrections as shown above.

+4


source


Thanks PaulMcKenzie, I got it,

So, for those struggling like me, this is the final code that actually works:



//Offsets
#define BASETIME 0x0158069C
#define OFFSET 0x14

void CurrentTime() {

    DWORD* BaseAddress = (DWORD*)GetModuleHandle(NULL);
    DWORD* address = (DWORD*)((char*)BaseAddress + BASETIME);
    address = (DWORD*)((char*)*address + OFFSET);
    float currentTime = *(float*)address;

    if (address && currentTime)
    {
        std::cout << endl <<"----------------" << endl;
        std::cout << "Base Address : " << BaseAddress << endl;
        std::cout << "----------------" << endl;
        std::cout << "Time Address : " << address << endl;
        std::cout << "----------------" << endl;
        std::cout << "Time Value : " << currentTime << endl;
        std::cout << "----------------" << endl << endl << "#> ";
    }

}

      

+2


source







All Articles