Basic memory editing problem c++ GTA

Posts 15 of 5 · Page 1 of 1
Basic memory editing problem c++ GTA
I have recently got involved in game hacking and wanted to try making a small trainer for GTA SA:MP using tutorials.

It's a basic tool to change the money, I found out the memory address which is correct
But when I try t run the hack it just doesn't change the money.

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

int newdata = 99999999;

int main() 
{
	HWND hWnd = FindWindow(0, "GTA:SA:MP");
  	if(hWnd == 0)
	{
    		printf("Error cannot find window.");
  	} 
	else
	{
		DWORD ProcessID;
		GetWindowThreadProcessId(hWnd, &ProcessID);
		HANDLE handleprocess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessID);
		if(!handleprocess)
			printf("Error opening process");
		else{
			WriteProcessMemory(handleprocess, (LPVOID)0xB7CE50, &newdata, (DWORD)sizeof(newdata), NULL);
		}
		CloseHandle(handleprocess);

	}

	getchar();
  	return 0;
}
It can't seem to open the process correctly so I tried using PROCESS_QUERY_INFORMATION instead of PROCESS_ALL_ACCESS which seems to work but it still won't change the money value. It's getting frustrating and I wonder if anyone can help me out.
WriteProcessMemory function (Windows)

To be able to use WriteProcessMemory you'll need to have both PROCESS_VM_WRITE and PROCESS_VM_OPERATION access on the handle, or simply PROCESS_ALL_ACCESS which includes them both.

0xB7CE50 is your address? Did you find it in Cheat Engine? In Cheat Engine the module's offset isn't showing as a part of the address, so if it's shown like this: GTA.exe+B7CE50 then GTA.exe -> is the offset which you will need to add to B7CE50, look at the address list where it doesn't show it in this form.

And if OpenProcess fails when you try to open the process with PROCESS_ALL_ACCESS, you can see what is the problem with this function:

Code:
void ShowError(DWORD dwErrorCode)
{
	TCHAR* lpMessageBuffer;

	FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (TCHAR*)&lpMessageBuffer, 0, NULL);

	MessageBox(HWND_DESKTOP, lpMessageBuffer, NULL, MB_OK | MB_ICONINFORMATION);

	LocalFree(lpMessageBuffer);
}
Add it above main() function. And instead of printf("Error opening process"); type: ShowError(GetLastError());
Quote Originally Posted by base187 View Post
.
So you can't get OpenProcess() to return a valid handle? ..try running the program "As Administrator" ?

Also, WriteProcessMemory() returns a BOOL: you should check it for success/fail.
And you do output messages when there are errors, but it might also be helpful to give output for successes, just for debugging purposes.

ex.
#include <iostream>
#include <windows.h>

int newdata = 99999999;

int main()
{
HWND hWnd = FindWindow(0, "GTA:SA:MP");
if(hWnd == 0)
{
printf("Error cannot find window.");
}
else
{
std::cout <<"SA:MP Window Found, Handle: " << hWnd << std::endl;
DWORD ProcessID;
GetWindowThreadProcessId(hWnd, &ProcessID);
std::cout <<"SA:MP Process Id: " << ProcessID << std::endl;
HANDLE handleprocess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessID);
if(!handleprocess)
printf("Error opening process");
else{
std::cout <<"OpenProcess() Success! _handle returned was: " << handleprocess << std::endl;
if (WriteProcessMemory(handleprocess, (LPVOID)0xB7CE50, &newdata, (DWORD)sizeof(newdata), NULL) == TRUE)
std::cout <<"WriteProcessMemory Ssccess. Value was changed to " << newdata << std::endl;
else
std::cout <<"WriteProcessMemory Failed!" << std::endl;

CloseHandle(handleprocess);

}


}

getchar();
return 0;
}

//not important yet, but also moved CloseHandle() --> it was getting called even if the handle wasn't valid.


Switch it back to PROCESS_ALL_ACCESS and try running your IDE (if you have one) "as Administrator" and|or running the program "as Administrator".
Try
- setting debug privileges
- unhiding the process
- using PROCESS_VM_WRITE | PROCESS_VM_OPERATION
Thanks everyone for posting! Sorry it took me a few days to reply.

Jabberwo0ck: Yes the memory address is the right one, I used an online list of memory addresses. I used your error function, the error it gave was "access denied" which was what I thought it probably was.

abuckau907: Running as admin seemed to fix it but I really wanted to be able to run the hack with a limited account, I could edit the values using cheat engine fine but not with my hack. I prefer using c style strings instead out cout, I find it cleaner and easier to organize data.

[MPGH]Biesi: I have seen some people using debug privileges but I could never get it to work. You were right about adjusting the access rights.

I got it working, I changed the access rights to: PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_VM_OPERATION
and now it seems to work great so thanks everyone!

Here's the code:
Code:
#include <cstdlib>
#include <iostream>
#include <windows.h>

int newdata = 1000;



void ShowError(DWORD dwErrorCode)
{
	TCHAR* lpMessageBuffer;

	FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (TCHAR*)&lpMessageBuffer, 0, NULL);

	MessageBox(HWND_DESKTOP, lpMessageBuffer, NULL, MB_OK | MB_ICONINFORMATION);

	LocalFree(lpMessageBuffer);
}

int main() 
{
	HWND hWnd = FindWindow(0, "GTA:SA:MP");
  	if(hWnd == 0)
	{
			ShowError(GetLastError());
  	} 
	else
	{
		DWORD ProcessID;
		GetWindowThreadProcessId(hWnd, &ProcessID);
		HANDLE handleprocess = OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_VM_OPERATION, FALSE, ProcessID);
		if(!handleprocess)
		{
			ShowError(GetLastError());
		}
		else{
			int hack = WriteProcessMemory(handleprocess, (LPVOID)0xB7CE50, &newdata, (DWORD)sizeof(newdata), NULL);
			if(hack > 0){
				printf("success!");
			}
			else{
				ShowError(GetLastError());
			}
		}
		CloseHandle(handleprocess);

	}

	getchar();
  	return 0;
}
Posts 15 of 5 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?