[Help] reading memory c++

03/04/2015 18:37 NutellaJunkie#1
Hello Epvpers!

I have a little problem with my simple and stupid farm bot for a game.
The bot need to read current Hp and the map ID.

These are my functions for doing so.
Code:
DWORD getCurrentHp()
{
	return *(DWORD*)(*(DWORD*)hpBaseAdress + hpOffset);
}

DWORD getCurrentMapID()
{
	return *(DWORD*)(*(DWORD*)mapBaseAddress);
}
And I use them like so:
Code:
//HP

DWORD currHp2 = getCurrentHp();
if (currHp2 != currHp)
{
	char hp[10];
	sprintf_s(Hp, "%i", currHp2);
	SetWindowText(GetDlgItem(HackInterface, hpEdit), hp);
	currHp = currHp2;
}
if (currHp < hpLimit)
{
        //restore hp.
}
And this works like a charm, my problem comes when I try to read the map ID.
Code:
//Map ID

DWORD currMapID = getCurrentMapID();
char mapID[10];
sprintf_s(mapID, "%i", currMapID);
SetWindowText(GetDlgItem(HackInterface, mapEdit), mapID);
This is code from my dll which I inject into the game.
I know that the HP is from an INT and technically the mapID is an byte, but it doesn't matter if I make the address a byte/2 byte or 4 byte in cheat engine, the value stays the same.

My game crashes when I inject the dll and tries to get the mapID.
So, any hints on what I do wrong? :P
03/04/2015 19:37 warfley#2
Well I dont know that much about how CheatEngine works, but why dont you just use a Byte instead of a Double Word?
Lets take this example, this might be your memory:

...0011 0111 1001 0110 1011 0111 0011 1001 1101 1000 10...
|A1| |A2| |A3| |A4| |A5|

And A1..5 are the Adresses. If you take a a DWord from A1 than you got
0011 1001 1011 0111 1001 0110 0011 0111 Which is equal to 968332855
or you take a Byte than you got 0011 0111 which is equal to 55

i mean you read more Memory than the value has allocated, so you read anything that is near that pointer also in the Dword. This cant go well
03/04/2015 19:53 NutellaJunkie#3
Quote:
Originally Posted by warfley View Post
Well I dont know that much about how CheatEngine works, but why dont you just use a Byte instead of a Double Word?
Lets take this example, this might be your memory:

...0011 0111 1001 0110 1011 0111 0011 1001 1101 1000 10...
|A1| |A2| |A3| |A4| |A5|

And A1..5 are the Adresses. If you take a a DWord from A1 than you got
0011 1001 1011 0111 1001 0110 0011 0111 Which is equal to 968332855
or you take a Byte than you got 0011 0111 which is equal to 55

i mean you read more Memory than the value has allocated, so you read anything that is near that pointer also in the Dword. This cant go well
I know I do, my code is far from optimized :P

But I have tried to use:
Code:
BYTEgetCurrentMapID()
{
	return *(BYTE*)(*(BYTE*)mapBaseAddress);
}
But the game still crashes :/
03/04/2015 20:10 Omdi#4
You should verify whether mapBaseAddress is valid or not.

Regardless of the validity check:
Code:
return *(BYTE*)mapBaseAddress;
03/04/2015 22:17 Daifoku#5
since you are injecting a DLL, you are already in the context of the game.
This said, you can just grab any valid address. You just have to typecast it.

*(BYTE*)mapBaseAddress

if you want to access a structure which leads to another address (which hold a byte, you would cast the base address to DWORD (since addresses are stored as DWORDS) and then cast the result to BYTE.

like

*(BYTE*)(*(DWORD*)mapBaseAddress + someoffset)


I'm not sure how to properly check an address, but this is my way

Code:
DWORD address = *(DWORD*)(0x00C5FCC4);
if (!address) {return NULL;}
03/04/2015 22:37 NutellaJunkie#6
Quote:
Originally Posted by Omdihar View Post
You should verify whether mapBaseAddress is valid or not.

Regardless of the validity check:
Code:
return *(BYTE*)mapBaseAddress;
Validation is for Über programmers, which I'm not :P
But the code worked like a charm, thanks :D

Quote:
Originally Posted by Daifoku View Post
since you are injecting a DLL, you are already in the context of the game.
This said, you can just grab any valid address. You just have to typecast it.

*(BYTE*)mapBaseAddress

if you want to access a structure which leads to another address (which hold a byte, you would cast the base address to DWORD (since addresses are stored as DWORDS) and then cast the result to BYTE.

like

*(BYTE*)(*(DWORD*)mapBaseAddress + someoffset)


I'm not sure how to properly check an address, but this is my way

Code:
DWORD address = *(DWORD*)(0x00C5FCC4);
if (!address) {return NULL;}
Thanks for clarifying, I learned something :)