More on Accessing Memory [Solved]
Ok, so thanks to Jason and Hassan I understand how to access pointers and stuff in memory. But the problem is I don't know how far to go in some situations. For example I have found the offset of my player's health in assault cube, it is an integer but i tried to access it by this and it didn't work:
Code:
*(int*)(0x4E4DBC(start of player struct) + 0xF4(player health offset)) = 200;
I didn't affect the health and it didn't do anything to the gameplay. Then i got the base address of ac_client and then added to that the start of the player struct then the offset like this:
Code:
DWORD base = (DWORD) GetModuleHandleA("ac_client.exe");
*(int*) (base + 0x4E4DBC + 0xF4) = 200;
All that did was crash the game.
I don't understand why it's not working so it's hard to fix the problem. Any help is appreciated. Thanks.
Thanks for the quick reply Jason. Thx for the code also but if you don't mind can you explain why for the player struct *(DWORD*) and not *(DWORD**) or something like that? I understand what they mean but my problem that I am having is that how do I know how many levels down a pointer is.
To verify if a pointer is pointing to a valid location in memory before using it you can do:
IsBadReadPtr( Your_pointer ) and IsBadWritePtr() respectively. Also you can wrap your code in an exception handler to test for an invalid pointer.
As for your question, it's very simple, *(DWORD*) = pointer to a DWORD pointer. But is dereferenced in player struct, so all that remains is simply a pointer to your health or name.
To clarify my answer about why it's a double level pointer, as I was a bit hazy in the first post:
The address itself needs to be interpreted as a pointer for C++ to read a specific type from it. I.e say there was a value of 100 starting at 0xDEADBEEF (4 byte value btw)
You can't just go:
DWORD value = (DWORD)0xDEADBEEF
because that will just convert the hex "0xDEADBEEF" to an integer.
But yeah, hopefully you get it now.
Thank you guys so much as usual. It makes a lot more sense to me now then it did yestarday, and that's a huge step for me. Thanks a bunch.
I doubt this is the end of my pointer problems so i will keep you guys updated but i really feel like i get it now. Thanks guys.
Oh, I just realized that it was also crashing because i wasn't even accessing the right offset. The struct is located at 0x4E4DBC, but when i add the base + offset of struct it is something else. So I should have written 0xE4DBC since the base address is 0x400000 and 0x400000 + 0xE4DBC = 0x4E4DBC, the address i wanted from the beginning.