This is the code I'm using:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <detours.h>
#include <fstream>
#define real_sendto 0x152942C6
void(__cdecl* originalFunction)();
void tompa_sendto() {
printf("Hellooo");
return originalFunction();
}
void hook() {
originalFunction = (void(__cdecl*)())DetourFunction((PBYTE)real_sendto, (PBYTE)tompa_sendto);
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH: hook(); break;
case DLL_PROCESS_DETACH: return FALSE; break;
}
return TRUE;
}
This code has no problems, it places the hook at 0x152942C6 and I can see it's jumping to my tompa_sendto() and back again.
When I use this code instead as the hook it doesn't set the hook correctly,
in the memory I can see at 0x152942C6: jmp 00000000
void tompa_sendto() {
char buffer[] = {'x', 'y', 'z'};
return originalFunction();
}
Whatever code I place in tompa_sendto() crashes the program (becuase of the bad jmp), the only 2 codes that I've managed to put there is printf and messageBoxA.