C++ address and offsets issues

Posts 1–5 of 5 · Page 1 of 1
C++ address and offsets issues
I'm trying to learn and understand how readproccessmemory work. When I'm using cheat engine to find the static values, it's an success. I can close the game, use my address and offset, and get the correct value every time. However, when I'm trying to execute this in C++, it never returns the correct value?

This is what my code looks like,

Code:
#include <iostream>
#include <windows.h>


using namespace std;

int main()
{

    // Base Address
    DWORD BaseAddress = 0x50F4F4;

    // Health
    DWORD healthOfset = 0xf8;
    DWORD healthAddress = (DWORD)(BaseAddress+healthOfset);
    int health = 0;

    HWND hwnd;
    DWORD pid;
    HANDLE phandle;
    hwnd = FindWindow(NULL,"AssaultCube");

    if(hwnd)
    {

        cout << "Game was found.\n" << endl;
        GetWindowThreadProcessId(hwnd,&pid);
        phandle = OpenProcess(PROCESS_ALL_ACCESS,0,pid);

        if(phandle)
        {


            // Read Health
            ReadProcessMemory(phandle,(PCVOID)healthAddress, &health, sizeof(health),0);
            cout << "Health: " << health << endl;

        }

    } else {
        cout << "Game not found." << endl;
    }

    return 0;

}
The above code returns:

Game was found.
Health: -1
This is what cheat engine is giving me,

http:/ / image.prntscr.com/image/b3660e76b0174f9794c4322108cb9eab . png (not allowed to post links)

Am i doing this completely wrong?
Your link doesn't work so it's hard to say. I don't see too much wrong with the code at first glance. Try using PBYTE* instead of PCVOID in your readprocmem.

Also you don't have to cast to dword if your assigning a dword to another dword. just do
Code:
DWORD healthAddress = (BaseAddress+healthOfset);
instead of
DWORD healthAddress = (DWORD)(BaseAddress+healthOfset);
If you're still having trouble you could try watching the video i posted on reading and writing memory.
http://www.mpgh.net/forum/showthread.php?t=1155218
Not sure why the link wasn't working. Using http instead of www seems to work, sorry.

http:/ / image.prntscr.com/image/b3660e76b0174f9794c4322108cb9eab . png

I'll watch your video, thanks!
Oh wow i'm blind. Your problem is really simple. Your base address is a pointer. Which means you have to read it to see what address it's pointing too. Try it like this. (idk if it works i haven't tested it, but this is DEF your problem.)

Code:
#include <iostream>
#include <windows.h>


using namespace std;

int main()
{

    // Base Address
    DWORD BaseAddress = 0x50F4F4;
	DWORD cBase = 0;

    // Health
    DWORD healthOfset = 0xf8;
    int health = 0;

    HWND hwnd;
    DWORD pid;
    HANDLE phandle;
    hwnd = FindWindow(NULL,"AssaultCube");

    if(hwnd)
    {

        cout << "Game was found.\n" << endl;
        GetWindowThreadProcessId(hwnd,&pid);
        phandle = OpenProcess(PROCESS_ALL_ACCESS,0,pid);

        if(phandle)
        {
		//read base
		ReadProcessMemory(phandle,(PBYTE*)BaseAddress, &cBase, sizeof(DWORD),0);//Assigns the value of base address pointer to cbase
			
            // Read Health
            ReadProcessMemory(phandle,(PBYTE*)cBase + healthOfset, &health, sizeof(health),0);
            cout << "Health: " << health << endl;

        }

    } else {
        cout << "Game not found." << endl;
    }

    return 0;

}
Quote Originally Posted by HexMurder View Post
Oh wow i'm blind. Your problem is really simple. Your base address is a pointer. Which means you have to read it to see what address it's pointing too. Try it like this. (idk if it works i haven't tested it, but this is DEF your problem.)

Code:
#include <iostream>
#include <windows.h>


using namespace std;

int main()
{

    // Base Address
    DWORD BaseAddress = 0x50F4F4;
	DWORD cBase = 0;

    // Health
    DWORD healthOfset = 0xf8;
    int health = 0;

    HWND hwnd;
    DWORD pid;
    HANDLE phandle;
    hwnd = FindWindow(NULL,"AssaultCube");

    if(hwnd)
    {

        cout << "Game was found.\n" << endl;
        GetWindowThreadProcessId(hwnd,&pid);
        phandle = OpenProcess(PROCESS_ALL_ACCESS,0,pid);

        if(phandle)
        {
		//read base
		ReadProcessMemory(phandle,(PBYTE*)BaseAddress, &cBase, sizeof(DWORD),0);//Assigns the value of base address pointer to cbase
			
            // Read Health
            ReadProcessMemory(phandle,(PBYTE*)cBase + healthOfset, &health, sizeof(health),0);
            cout << "Health: " << health << endl;

        }

    } else {
        cout << "Game not found." << endl;
    }

    return 0;

}
wow, thank you so much! nice to see that i almost made it on the first try, thanks again!
Posts 1–5 of 5 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us