C++ Pointer + Offset?

Posts 1–11 of 11 · Page 1 of 1
C++ Pointer + Offset?
Hey, i'm trying to learn myself some c++ and so forth. Not going into detail, anyways, i might need some help. I tried search here and came up with not much about what i was looking for.

I wrote a test code so i could modify the game 'Solitaire' in windows 7. Dude stop looking at me like that. it's a test

Anyways, i found the Pointer in cheat engine which points to the address know as the 'score' so i also know how to find the correct address using the pointer + offset in cheat engine.

So what's the problem? Well i don't exactly know how to find the correct memory address in c++. I know the pointer and i know the offset.

But how do i calculate the new address that the game appoints it to.
If i know this calculation alone it's already a big help

This is the code that i currently have.
I un-commented the action to patch the game so that it doesn't mess up anything while i was trying to figure out how to calculate the new address.

Code:
#include <windows.h>
#include <iostream>
using namespace std;

int main()
{   
    
    // This will find you the proper adress in the memory
    LONG pointer = 0x0020C020;
    LONG offset  = 14; // 14
    LONG address;
    
    if(pointer)
    {
         // If pointer is exists, calculate the proper adress and use that
         address = (pointer + offset);
    }
    else
    {
         // If pointer is empty then use this adress
         address = 0x005DA534;
    }
    
    cout << "The pointer points to the addres ";
    cout << address;
    cin.get();


	//value to send to game
    int newvalue = 99;

	// Handles
	/*
    HWND hwnd = FindWindow(NULL, "Solitaire"); //what window to find
    HANDLE phandle;
    DWORD pid;

    if (hwnd != 0)
	{
		//just a option
		cout << "Game is running. Press enter to hack it.";

		//name of program window
		SetWindowText(NULL,"Far Cry 2 Hack");

		GetWindowThreadProcessId(hwnd, &pid);
		phandle = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);

		cin.get();

    }
	else
	{
		//shows if game is not running
		cout << "Open the game";
		cin.get();
		return 0;
	}

	if (phandle != 0)
	{
		WriteProcessMemory(phandle, (LPVOID)address, (LPVOID) &newvalue, 4, 0);

		//says done if it worked
		cout << "Done";
		cin.get();
	}
	else
	{
		//if it didn't work it says failed
		cout << "Failed";
		cin.get();
	}
	*/
}
Thanks
First of all, offset must be 0xXXX not just XX (In your case, 0x14)
Why? Because you want to add a Hex number to the Address (Hex), If you had a decimal (Which is possible), The hex of it won't be the same. So, the offset will be incorrect.

Ok, the main problem. since you are using winapi. It's simple.
Use ReadProcessMemory, save the address that was inside, add the offset to it. And use that Stored Address on the next Read or Write process memory.
Just don't mess with Dec and Hex numbers.
Quote Originally Posted by 'Bruno View Post
First of all, offset must be 0xXXX not just XX (In your case, 0x14)
Why? Because you want to add a Hex number to the Address (Hex), If you had a decimal (Which is possible), The hex of it won't be the same. So, the offset will be incorrect.

Ok, the main problem. since you are using winapi. It's simple.
Use ReadProcessMemory, save the address that was inside, add the offset to it. And use that Stored Address on the next Read or Write process memory.
Just don't mess with Dec and Hex numbers.
Hey thanks man, this really helped. I found the decimal value of the memory, in cheat engine the pointer's hex value is '005DA520' which in decimals is 6137120.

By using this code (love google):
Code:
        ReadProcessMemory(phandle, (LPVOID)pointer, &value, 4, 0);

		//says done if it worked
		cout << "Done: ";
		cout << value;
		cin.get();
i managed to get that decimal result.

The code used by my currently opened solitaire game' hex value is '005DA534' so when i do 6137120 + 14 i get 6137134.

Now i think i have the address that i need to use. But i am not sure if i can use this the same way as i used the hexadecimal address.

Do i have to convert this to a Hexadecimal address in order to patch the game or can i use this as 0x6137134 ??
I think what you're trying to do is add 14 to what 0x5DA520 points to:
Code:
ReadProcessMemory(phandle, (LPVOID)0x5DA520, &value, 4, 0);
ReadProcessMemory(phandle, (LPVOID)(value+14), &value, 4, 0);
Which is what brinuz said.
Quote Originally Posted by Hell_Demon View Post
I think what you're trying to do is add 14 to what 0x5DA520 points to:
Code:
ReadProcessMemory(phandle, (LPVOID)0x5DA520, &value, 4, 0);
ReadProcessMemory(phandle, (LPVOID)(value+14), &value, 4, 0);
Which is what brinuz said.
Oh... yes i come from non-desktop programming (php and such) so i'm not used to thinking this way. Thanks though.

Uhm, however. It doesn't seem to work. The value after the + statement contains 0 which is either not working or its normal?

Here is my code now:
Code:
#include <windows.h>
#include <iostream>
using namespace std;

int main()
{   
    int value;

	//value to send to game
    int newvalue = 99;

	// Handles
	
    HWND hwnd = FindWindow(NULL, "Solitaire"); //what window to find
    HANDLE phandle;
    DWORD pid;

    if (hwnd != 0)
	{
		//just a option
		cout << "Game is running. Press enter to hack it.";

		//name of program window
		SetWindowText(NULL,"Solitaire Hack");

		GetWindowThreadProcessId(hwnd, &pid);
		phandle = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);

		cin.get();

    }
	else
	{
		//shows if game is not running
		cout << "Open the game";
		cin.get();
		return 0;
	}

	if (phandle != 0)
	{
        ReadProcessMemory(phandle, (LPVOID)0x5DA520, &value, 4, 0);
        ReadProcessMemory(phandle, (LPVOID)(value+14), &value, 4, 0);
		WriteProcessMemory(phandle, (LPVOID)value, (LPVOID) &newvalue, 4, 0);

		//says done if it worked
		cout << "Done ";
		cout << value;
		cin.get();
	}
	else
	{
		//if it didn't work it says failed
		cout << "Failed";
		cin.get();
	}
}
replace
Code:
ReadProcessMemory(phandle, (LPVOID)(value+14), &value, 4, 0);
WriteProcessMemory(phandle, (LPVOID)value, (LPVOID) &newvalue, 4, 0);
with

Code:
WriteProcessMemory(phandle, (LPVOID)(value+14), (LPVOID) &newvalue, 4, 0);
Quote Originally Posted by Hell_Demon View Post
replace
Code:
ReadProcessMemory(phandle, (LPVOID)(value+14), &value, 4, 0);
WriteProcessMemory(phandle, (LPVOID)value, (LPVOID) &newvalue, 4, 0);
with

Code:
WriteProcessMemory(phandle, (LPVOID)(value+14), (LPVOID) &newvalue, 4, 0);
Does return something now, but still not changing the score, neither giving me a hex value.

Code:
#include <windows.h>
#include <iostream>
using namespace std;

int main()
{   
    LONG address;
    int value;

	//value to send to game
    int newvalue = 99;

	// Handles
	
    HWND hwnd = FindWindow(NULL, "Solitaire"); //what window to find
    HANDLE phandle;
    DWORD pid;

    if (hwnd != 0)
	{
		//just a option
		cout << "Game is running. Press enter to hack it.";

		//name of program window
		SetWindowText(NULL,"Solitaire Hack");

		GetWindowThreadProcessId(hwnd, &pid);
		phandle = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);

		cin.get();

    }
	else
	{
		//shows if game is not running
		cout << "Open the game";
		cin.get();
		return 0;
	}

	if (phandle != 0)
	{
        ReadProcessMemory(phandle, (LPVOID)0x5DA520, &value, 4, 0);
		WriteProcessMemory(phandle, (LPVOID)(value+14), (LPVOID) &newvalue, 4, 0);

		//says done if it worked
		cout << "Done ";
		cout << (value+14);
        cin.get();
	}
	else
	{
		//if it didn't work it says failed
		cout << "Failed";
		cin.get();
	}
}
returns to me: 'Done -689226' ??
edit: fail, using (LPVOID)(value+14) returns: 'Done 0xfff57bb6'

edit2: hmm, it should have worked. The value without +14 = FFF57BA8 which is the value of the address 005DA520 (pointer or whatever.) so if we added +14 then that should be the new address?

btw here is my text file which is the stuff i found in CE:
Code:
The value of the pointer needed to find this adress is probably
005DA520

[r11+14]

scan for pointer
address: 0020C020 < pointer + offset = adress needed to change (in ce)
value  : 005DA520

scan for pointer
address: 005DA520
value  : FFF57BA8
Good lord to much confusion in this thread. Okay first of all a number is a number hex, decimal, don't matter.

That being said where did you get the offset 14 from. If you got it from Olly or CE, it probably isn't 14, but 0x14. But you have to check yourself. 0x14 = 20 = 9+9+2. Why you wrote it like that I'll never know. Maybe you just got confused.
Quote Originally Posted by why06 View Post
Good lord to much confusion in this thread. Okay first of all a number is a number hex, decimal, don't matter.

That being said where did you get the offset 14 from. If you got it from Olly or CE, it probably isn't 14, but 0x14. But you have to check yourself. 0x14 = 20 = 9+9+2. Why you wrote it like that I'll never know. Maybe you just got confused.
Ahh crap. Your right, i should probably go and read some ASM first because this confuses me too much.

Btw, the offset is from Cheat Engine, normally in cheat egnine when you see this *something*+offset

and if you use the pointer location at the offset of 14 it gives you the correct address. But I am guessing that it doesn't work the same way as I thought to make that work in c++
These functions might help:

Code:
unsigned long ReadPointer(unsigned long ulBase, int iOffset)
{
   __try
   { 
	   return *(unsigned long*)(*(unsigned long*)ulBase + iOffset); }

   __except (EXCEPTION_EXECUTE_HANDLER) { return 0; }
}
int ReadNegativePointer(int ulBase, int iOffset)
{
   __try { return *(int*)(*(int*)ulBase + iOffset); }
   __except (EXCEPTION_EXECUTE_HANDLER) { return 0; }
}

bool WritePointer(unsigned long ulBase, int iOffset, int iValue)
{
   __try { *(int*)(*(unsigned long*)ulBase + iOffset) = iValue; return true; }
   __except (EXCEPTION_EXECUTE_HANDLER) { return false; }
}
Posts 1–11 of 11 · Page 1 of 1
This thread is closed for replies.

Tags for this Thread

None

Talk with us