AfxCodeHook Example - Simple Api Hooking

Post date: Oct 17, 2010 10:39:22 PM

This code example details how to use afxcodehook (written by Aphex) to manipulate calls to windows apis.

program MsgBox;
uses
  Windows,
  afxCodeHook;
var
  MessageBoxANextHook: function(hWnd: HWND; lpText, lpCaption: PAnsiChar; uType: UINT): Integer; stdcall;
function MessageBoxAHookProc(hWnd: HWND; lpText, lpCaption: PAnsiChar; uType: UINT): Integer; stdcall;
begin
  Result := MessageBoxANextHook(0, '', 'bye', 0);
end;
begin
  //test the target API
  MessageBox(0, '', 'hi', 0);
  //hook the API
  HookCode('user32', 'MessageBoxA', @MessageBoxAHookProc, @MessageBoxANextHook);
  //the message should be changed
  MessageBox(0, '', 'hi', 0);
  //unhoook the API
  UnhookCode(@MessageBoxANextHook);
  //test the target API
  MessageBox(0, '', 'hi', 0);
end.