Search code examples
c++pointersreverse-engineering

pointer not returning the value correctly


I have a problem to return the value of the pointer in my program, the value of the pointer is not being saved, and when it reads it is returning null.

Header Code:

class PlayerHK : public Player {
public:
    PlayerHK();

    ULONG player_hp();
    ULONG player_power();
    ULONG player_hp2();
    ULONG player_power2();

private:

    struct CPlayer
    {
        BYTE padding[0x20];
        ULONG hp;
        ULONG power;
    };

    CPlayer *player;
};

Main Code:

PlayerHK::PlayerHK() {

        player = reinterpret_cast<CPlayer*>(*reinterpret_cast<DWORD*>(0x00B1C4E5));

    }

    ULONG PlayerHK::player_hp() {
        return player->hp; //does not return the value
    }

    ULONG PlayerHK::player_power() {
        return player->power; //does not return the value
    }

    ULONG PlayerHK::player_hp2() {
        player = reinterpret_cast<CPlayer*>(*reinterpret_cast<DWORD*>(0x00B1C4E5));
        return player->hp; //returns the value
    }

    ULONG PlayerHK::player_power2() {
        player = reinterpret_cast<CPlayer*>(*reinterpret_cast<DWORD*>(0x00B1C4E5));
        return player->power; //returns the value
    }

when the program I run will read the PlayerHK, the value should not get saved? Did I forget to do something?


Solution

  • If I understand the question correctly you are asking why

    player = reinterpret_cast<CPlayer*>(*reinterpret_cast<DWORD*>(0x00B1C4E5));
    

    gives sets player to NULL when run in the constructor, but not when run in player_hp2 or player_power2.

    The obvious answer would be that this memory location (0x00B1C4E5) holds the value NULL when you are constructing the object, and holds a different value when you're calling player_hp2 or player_power2. Perhaps the player hasn't been created yet when the constructor runs, so the pointer to the player (which you are reading) is NULL.