AfxCodeHook Example - Simple Api Hooking

posted 17 Oct 2010, 15:39 by Delphi Basics
This code example details how to use afxcodehook (written by Aphex) to manipulate calls to windows apis. 

program MsgBox;

uses
  Windows,

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.
Comments