[英]C++ Read Memory Address / Pointer & Offset
So, I have injected a DLL into a process (a game) so that I can read from the process memory. 所以,我已经在一个进程(游戏)中注入了一个DLL,以便我可以从进程内存中读取。
I want to get the current game time, and I've found the static base address & offset of it using Cheat Engine : 我想获得当前的游戏时间,并且我已经使用Cheat Engine找到了它的静态基地址和偏移量:
"game.exe"+0158069C “的Game.exe” + 0158069C
Offset : 14 抵消:14
And this is the code I've tried 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 Cheat Engine), but after that everything is wrong, can you help me ? 基地址的cout是正确的(我可以用Cheat Engine检查),但在那之后一切都错了,你能帮助我吗? I'm stuck with this and I've tried many things ... :/ 我坚持这个,我尝试了很多东西......:/
Thank you in advance, 先感谢您,
I am assuming that you want to increment your pointer by OFFSET
and BASETIME
bytes. 我假设你想通过OFFSET
和BASETIME
字节递增指针。 If so, your code is not incrementing on a byte basis. 如果是这样,您的代码不会以字节为单位递增。 Instead it is incrementing by sizeof(DWORD) * OFFSET
bytes. 而是通过sizeof(DWORD) * OFFSET
字节递增。
The reason is that the base pointer type is DWORD*
, and incrementing pointers of this type by n
will get you to n * sizeof(DWORD)
away from the start. 原因是基本指针类型是DWORD*
,并且通过n
递增此类型的指针将使您从开始处获得n * sizeof(DWORD)
。 This will not do the job. 这不会起作用。
The easiest solution is to cast to a char *
when doing the pointer arithmetic, so that the increment is going by sizeof(char)
, not sizeof(DWORD)
: 最简单的解决方案是在执行指针运算时转换为char *
,以便增量按sizeof(char)
,而不是sizeof(DWORD)
:
DWORD *BaseTimeAddress = (DWORD*)((char *)BaseAddress + BASETIME);
time = (float*)((char *)BaseTimeAddress + OFFSET);
Now, whether where you end up is the data you want, that is something I can't answer. 现在,无论你最终到底是你想要的数据,这是我无法回答的。 However if your goal was to increment on a byte basis, then you should make the corrections as shown above. 但是,如果您的目标是以字节为单位递增,那么您应该进行如上所示的更正。
Thank you PaulMcKenzie I got it, 谢谢PaulMcKenzie我明白了
So for those who struggle like me, this is the final code who actually work : 所以对于像我这样挣扎的人来说,这是实际工作的最终代码:
//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 << "#> ";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.