Post Reply  Post Thread 
Hook example
Author Message
ricnogueira
Junior Member
**


Posts: 2
Group: Registered
Joined: Feb 2008
Status: Offline
Reputation: 0
Post: #1
Hook example

I am trying to build a simple program that hooks the Windows function MessageBox

I ve analysed the sample that comes with codehook, but I didnt understand how I can do that

Could you supply an exemple for hooking MessageBox?

Tks

Ricardo Nogueira

02-22-2008 06:47 AM
Find all posts by this user Quote this message in a reply
Qi
Administrator
*******


Posts: 46
Group: Administrators
Joined: Oct 2007
Status: Offline
Reputation: 0
Post: #2
RE: Hook example

Hello,

I have no Delphi in hand now. I will post complete code hooking MessageBox when I back to home.

The document tutorial.html in "help" directry may give you some hints.

Now here is a brief instructions to make a hook.

1, Get the interfaces.
InitCodeHookDLL('..\..\..\CHook.dll');
GetCodeHook(GCodeHook);
//if you don't need advanced hook, no need following line
GCodeHook.GetCodeHookHelper(GCodeHookHelper);

2, Get the address of MessageBoxA or MessageBoxW (unicode version). There is no API names MessageBox.
TheAddress := GetProcAddress(GetModuleHandle('user32.dll'), 'MessageBoxA');

3, Write your hook function such like,

function NewMessageBoxA(hWnd: HWND; lpText, lpCaption: PAnsiChar; uType: UINT): Integer; stdcall;
begin
Do something here
end;

4, Now do the hook,
GCodeHook.Hook(TheAddress, @NewMessageBoxA);

Finished.

Here is the raw mode hook. The sample code in the package is the advanced mode which I must admit is not a good start. Smile

This post was last modified: 02-22-2008 11:00 AM by Qi.

02-22-2008 11:00 AM
Visit this users website Find all posts by this user Quote this message in a reply
ricnogueira
Junior Member
**


Posts: 2
Group: Registered
Joined: Feb 2008
Status: Offline
Reputation: 0
Post: #3
RE: Hook example

Thanks

I will try it

Qi Wrote:
Hello,

I have no Delphi in hand now. I will post complete code hooking MessageBox when I back to home.

The document tutorial.html in "help" directry may give you some hints.

Now here is a brief instructions to make a hook.

1, Get the interfaces.
InitCodeHookDLL('..\..\..\CHook.dll');
GetCodeHook(GCodeHook);
//if you don't need advanced hook, no need following line
GCodeHook.GetCodeHookHelper(GCodeHookHelper);

2, Get the address of MessageBoxA or MessageBoxW (unicode version). There is no API names MessageBox.
TheAddress := GetProcAddress(GetModuleHandle('user32.dll'), 'MessageBoxA');

3, Write your hook function such like,

function NewMessageBoxA(hWnd: HWND; lpText, lpCaption: PAnsiChar; uType: UINT): Integer; stdcall;
begin
Do something here
end;

4, Now do the hook,
GCodeHook.Hook(TheAddress, @NewMessageBoxA);

Finished.

Here is the raw mode hook. The sample code in the package is the advanced mode which I must admit is not a good start. Smile

02-22-2008 10:43 PM
Find all posts by this user Quote this message in a reply
Qi
Administrator
*******


Posts: 46
Group: Administrators
Joined: Oct 2007
Status: Offline
Reputation: 0
Post: #4
RE: Hook example

Following is a complete source code of a demo .dpr file.
You can copy and save it as a .dpr to try it.

More detailed simple demo code will come soon.

Note the line InitCodeHookDLL('..\..\..\CHook.dll');
You need to change the path to where CHook.dll is in.

Code:
program CodeHookDemo1;

uses
  SysUtils, Windows,
  CodeHookIntf;

var
  GCodeHook: ICodeHook;
  OldMessageBoxA: function(hWnd: HWND; lpText, lpCaption: PAnsiChar; uType: UINT): Integer; stdcall;

function NewMessageBoxA(hWnd: HWND; lpText, lpCaption: PAnsiChar; uType: UINT): Integer; stdcall;
var
  lMsg: string;
begin
  lMsg := 'The text passed to MessageBox is:'#13#10
    + '"' + StrPas(lpText) + '"'#13#10
    + #13#10
    + 'Now here is some other text appended by the hook function.';

  Result := OldMessageBoxA(hWnd, PChar(lMsg), lpCaption, uType);
end;

procedure ShowMsg(const AMsg: string);
begin
  MessageBoxA(0, PChar(AMsg + #13#10 + 'This is the demo message'), 'Demo title', MB_OK);
end;

procedure ExecuteDemo;
var
  lHookInfo: TCodeHookInfo;
  lHandle: TCodeHookHandle;
  lMessageBoxA: Pointer;
begin
  InitCodeHookDLL('..\..\..\CHook.dll');
  GetCodeHook(GCodeHook);

  ShowMsg('Before hooked');
  lMessageBoxA := GetProcAddress(GetModuleHandle('user32.dll'), 'MessageBoxA');
  lHandle := GCodeHook.Hook(lMessageBoxA, @NewMessageBoxA);
  GCodeHook.GetHookInfo(lHandle, @lHookInfo);
  @OldMessageBoxA := lHookInfo.PreviousMethod;

  ShowMsg('After hooked');
  GCodeHook.Unhook(lHandle);
end;

begin
  ExecuteDemo;
end.

02-22-2008 11:37 PM
Visit this users website Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites

Forum Jump: