// Set up the hook
VERIFY ( CFuncInAPIHook::Instance().HookFunction ( _T("user32.dll"), _T("MessageBoxW"), AfxGetInstanceHandle(), ( PROC )&MyMessageBox ) );
// Replacement procedure
int WINAPI MyMessageBox( HWND hWnd,
LPCTSTR lpText,
LPCTSTR lpCaption,
UINT uType
)
{
// Function pointer must have same calling convention as PROC (WINAPI)! Therefore, prepend WINAPI (__stdcall)!
typedef int (WINAPI *MESSAGEBOX_FUNC)(HWND, LPCTSTR, LPCTSTR, UINT);
// Only retrieve the original function's address once!
static MESSAGEBOX_FUNC pFunc = ( MESSAGEBOX_FUNC ) CFuncInAPIHook::Instance().RetrieveOriginalAPIFunctionAddress ( _T("user32.dll"), _T("MessageBoxW") );
ASSERT ( pFunc );
return pFunc ? ( *pFunc )( hWnd, _T("I have hooked this call!"), lpCaption, uType ) : 0;
}
// This call will be hooked
::MessageBox ( NULL, _T(""), _T("Title"), MB_OK );
|