[Help] code crashing game

07/31/2015 13:28 ogpayne#1
My very small hack I've made is crashing my game and I don't know why, all I wanted to do was keep the ammo at 42 (favorite number) and not let it decrease. but whenever I start the exe the game crashes... should I make it as a dll and inject it instead? this is the error I get upon starting the exe "win32 exception 0x0000005 [0x36]"
and here is my code

#include "stdafx.h"
#include <iostream>
#include <string>
#include <Windows.h>


using namespace std;

int main(void)
{

int nVal = 42;

HWND hWnd = FindWindowA(0, "AssaultCube");

if (hWnd == 0)
{
cerr << "Could not find window" << endl;
}
else
{
BYTE data[] = { 0x0, 0x14, 0x384, 0x0 };
DWORD pId;
GetWindowThreadProcessId(hWnd, &pId);
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, false, pId);

if (!hProc)
{
cerr << "Process can't open.." << endl;
}
else
{
int stat = WriteProcessMemory(hProc, (LPVOID)0x005100F8, &nVal, (DWORD)sizeof(nVal), NULL);

if (stat < 42)
{
clog << "Activated" << endl;
}
else
{
cerr << "Not applied" << endl;
}

CloseHandle(hProc);
}
}
system("pause");
return 0;
}
08/23/2015 03:15 ~kau~#2
WriteProcessMemory is rtn your value? :O how?
Is the adress static?
remove the red line...

int stat = WriteProcessMemory(hProc, (LPVOID)0x005100F8, &nVal, (DWORD)sizeof(nVal), NULL);

if (stat < 42)
{
clog << "Activated" << endl;
}
else
{
cerr << "Not applied" << endl;
}
10/22/2015 20:24 Cyrex'#3
wpm returns a bool, so it can only be 0 or 1, hence your check is crap.
check nVal.