I have hooked a sound function that work within the game menu after, when the battle start, the game will crash.
The function at the first parameter get correctly the sound file name, before the battle begin.
If the function 'HookSum' is empty the game work without any crash.
The line:
//
std::cout << "your program has been hacked! " << std::endl;
//
don't cause the crash, but If I insert any other line of code for example:
//
std::string StrFileName;
StrFileName = "test";
//
the game crash. So at this point I can't hook nothing, becouse I can't insert any code inside the hooked function.
Someone can please explain why the game crash ?
Follow the full code.
#include <Windows.h>
#include <iostream>
#include "detours.h"
#include "sigscan.h"
DWORD AddressOfSum = 0;
typedef void(*sum) (char *a1, int a2, int a3, int a4);
void HookSum(char *a1, int a2, int a3, int a4)
{
std::cout << "your program has been hacked! " << std::endl;
std::string StrFileName;
StrFileName = "test";
sum originalSum = (sum)AddressOfSum;
return originalSum(a1,a2,a3,a4);
// MessageBoxA(0, a1, "MsgTitle", MB_OK | MB_ICONQUESTION);
// return;
}
BOOL WINAPI DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved)
{
// store the address of sum() in testprogram.exe here.
if (dwReason == DLL_PROCESS_ATTACH)
{
// We will use signature scanning to find the function that we want to hook
// we will find the function in IDA pro and create a signature from it:
SigScan Scanner;
AddressOfSum = Scanner.FindPattern("bzone.exe", "\x55\x8B\xEC\x51\x83\x3D\x94\x55\x91", "xxxxxxxxx"); // BattleZoneRedux 43AA30
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
// this will hook the function
DetourAttach(&(LPVOID&)AddressOfSum, &HookSum);
DetourTransactionCommit();
}
else if (dwReason == DLL_PROCESS_DETACH)
{
// unhook
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
// this will hook the function
DetourDetach(&(LPVOID&)AddressOfSum, &HookSum);
DetourTransactionCommit();
}
return TRUE;
}