I'm trying to hook the MessageBoxA function with MS Detours 3.0 but when I try it my program crashes. I'm not sure what is causing the program to crash. When i run the test program and hit shift the message box appears, but when I inject the dll and hit shift my program crashes.
TEST PROGRAM
#include <Windows.h>
int main()
{
for(;;)
{
if(GetAsyncKeyState(VK_SHIFT))
{
MessageBoxA(0,"NOT HOOKED","HOOK STATUS",0);
}
}
}
HOOK DLL
#include <Windows.h>
#include <detours.h>
#pragma comment(lib,"detours.lib")
BOOL (WINAPI* oMessageBoxA)(HWND,LPCTSTR,LPCTSTR,UINT);
BOOL WINAPI hMessageBoxA( HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption,UINT uType)
{
return oMessageBoxA(hWnd,"HOOKED",lpCaption,uType);
}
void patch()
{
HMODULE user32 = GetModuleHandle("user32.dll");
if(user32 != NULL)
{
DWORD MessageBoxAddress = (DWORD)GetProcAddress(user32,"MessageBoxA");
oMessageBoxA = (BOOL (WINAPI*)(HWND, LPCTSTR, LPCTSTR, UINT))MessageBoxAddress;
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)oMessageBoxA, hMessageBoxA);
DetourTransactionCommit();
}
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
{
if(fdwReason==DLL_PROCESS_ATTACH)
{
patch();
}
}