我在返回程序中的指针值时遇到问题,该指针的值未保存,并且在读取时返回空值。
标题代码:
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;
};
主要代码:
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
}
我运行的程序将读取PlayerHK时,该值不应保存吗?我忘了做某事吗?
答案 0 :(得分:2)
如果我对问题的理解正确,那么您在问为什么
player = reinterpret_cast<CPlayer*>(*reinterpret_cast<DWORD*>(0x00B1C4E5));
在构造函数中运行时,将player
设置为NULL,而在player_hp2或player_power2中运行时,则不设置为{1>}。
一个明显的答案是,在构造对象时,此内存位置(0x00B1C4E5)保留NULL值,而在调用player_hp2或player_power2时保留不同的值。构造函数运行时,也许尚未创建播放器,所以指向播放器的指针(您正在读取)为NULL。