[C/C++] Internal hack only works inside a new thread? Full code example

Posts 1–2 of 2 · Page 1 of 1
[C/C++] Internal hack only works inside a new thread? Full code example
Hi, so I have a pointer that points to the game's location data:
Code:
DWORD *pLocationData = (DWORD*)((*(DWORD*)(g_dwBaseAddress + 0x14459E4)) + 0x18);
And I have another pointer that points to the local player's X axis:
Code:
DWORD *pPlayerX = (DWORD*)(*(DWORD*)(pLocationData)+0x1C);
I'm not sure why, but the first pointer always points to the address 0x0000000018 unless I put the code in a new thread.

For example,
Code:
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)FlyHack, 0, 0, 0);
Code:
void FlyHack() //main.cpp
{
	g_dwBaseAddress = GetModuleHandle(0);
	while(true)
	{
		DWORD *pLocationData = (DWORD*)((*(DWORD*)(g_dwBaseAddress + 0x14459E4)) + 0x18);
		

		cheat_state->player.flyHack ^= 1; // I'm assuming this crashes my program because I'm accessing a struct value from a new thread.
        }
}
If I use the code above it will always point to the right address and not 0x00000018. However, if I code it like this:

Code:
HRESULT IDirect3DDevice9Proxy::Present(CONST RECT* pSourceRect, CONST RECT* pDestRect, HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion) //IDirect3DDevice9Proxy.cpp
{
	renderHandler();
}

void renderHandler() //IDirect3DDevice9Proxy.cpp
{
	// Draw some overlay stuff and call our cheat_hook function..

	cheat_hook(pPresentParam.hDeviceWindow);
}
Code:
void cheat_hook(HWND wnd) //cheat.cpp
{
	keyhook_maybe_install(wnd);
	keyhook_run();
	

	// Here is where we call our flyhack function
	if(g_dwBaseAddress != NULL)
	   cheat_flyhack();
}
Code:
void cheat_flyhack() // flyhack.cpp
{
	DWORD *pLocationData = (DWORD*)((*(DWORD*)(g_dwBaseAddress + 0x14459E4)) + 0x18);
	if (pLocationData && (DWORD)pLocationData != 0x00000018)
	{
		// Code never makes it this far because pLocationData always points to 0x00000018
	}
}
Take a look at the cheat_flyhack function. How come the pointer pLocationData always points to 0x00000018? If I put that same code in void Flyhack (a new thread) it works just fine. It will point to the local player's x coordinate like I programmed it to do. Could this be happening because the game I am playing has 2 processes with the same name? If so, how do I fix my code so it points to the right address? Both game processes have the same base address..

Edit: Yeah, my program is trying to access the pointer in the wrong process. If I attach cheat engine to the first game process it will point to the right address.


But if I attach to the second process (the one that has exactly the same name and baseaddress of the first) it will point to the wrong address.


So how do I get my code to point to the right address? I've been doing a lot of research on google and I don't think anyone has encountered this problem before.
Steps I took to solve the problem:
1. I am using my own function to get a handle to the correct game window.
2. Instead of changing the memory directly with pointers, I am using ReadProcessMemory and WriteProcessMemory.
Now my program is fully functional.. But I'd still like to know why my last code wasn't working because Read/WriteProcessMemory seems to be a lot slower (and more expensive) than directly modifying the game's memory.
Posts 1–2 of 2 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us