I have been trying to write to a process's memory with this code (to create a cheat code):
#include
int main()
{
HWND hWnd = FindWindow(0, "xyz");
if(hWnd == 0)
{
MessageBox(0, "Error cannot find window.", "Error", MB_OK|MB_ICONERROR);
}
else
{
DWORD proccess_ID;
GetWindowThreadProcessId(hWnd, &proccess_ID);
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, proccess_ID);
if(!hProcess)
{
MessageBox(0, "Could not open the process!", "Error!", MB_OK|MB_ICONERROR);
}
else
{
int newdata = 500;
DWORD newdatasize = sizeof(newdata);
if(WriteProcessMemory(hProcess, (LPVOID)0x57C2A4, &newdata, newdatasize, NULL))
{
MessageBox(NULL, "WriteProcessMemory worked.", "Success", MB_OK + MB_ICONINFORMATION);
}
else
{
MessageBox(NULL, "Error cannot WriteProcessMemory!", "Error", MB_OK + MB_ICONERROR);
}
CloseHandle(hProcess);
}
}
return 0;
}
When I overwrite for example a jnz with jz it works fine, because both have the same size. But when I try to overwrite for example a pop with jmp I get an error because these commands have different size.
I read here that WriteProcessMemory performs a verification to check the available size at the specified address.
What I want to do is to write to a memory address without size check, so the program simply overwrite as many bytes is needed to the code.
With Cheat Engine I was able to do this, because it offered me to overwrite the necessary bytes.
So my question is how to do in C++ the same as with Cheat Engine?