Reason your crashing: Detouring an instruction that is only 2 bytes long ( overwriting something thats 2 bytes long with a 5 byte instruction ). Meaning you're overwriting the instruction that comes next which completely changes everything.
Edit: why06 explained this part better than I did, sorry why. ):
Fix: NOP the last byte of the second instruction so that your jump doesn't cause any failure and just execute the instruction in your own function.
Code: ( I tested it, it works for me. My ammo goes up. )
[Highlight=VB]
#include <windows.h>
#include <detours.h>
unsigned long ret = 0x0045B75F+6;
__declspec(naked) void Inc_ammo()
{
__asm
{
inc [esi]
lea esi,dword ptr ss:[esp+0x24]
jmp [ret]
}
}
void MainThread()
{
unsigned long old;
VirtualProtect((void*)0x0045B75F,sizeof(unsigned long),PAGE_EXECUTE_READWRITE,&old);
DetourFunction((BYTE*)0x0045B75F,(BYTE*)Inc_ammo);
*(BYTE*)(0x0045B75F+5) = 0x90;
}
bool __stdcall DllMain(HINSTANCE hInst,unsigned long ulReason, void* lpUseless)
{
if(ulReason == DLL_PROCESS_ATTACH)
{
CreateThread(0,0,(LPTHREAD_START_ROUTINE)MainThrea d,0,0,0);
}
return true;
}
[/Highlight]
You're welcome.