Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global mouse hook

Status
Not open for further replies.

Msilver2

Programmer
Apr 12, 2003
9
RO
Global mouse hook.

Please take a look at my code.
I try to implement a global mouse hook, but it works like a local hook.
What is wrong with it ? I have no idea. Any suggest is welcome !


----------------------------------------------------
unit MouseHookU;

interface

uses
Windows, Messages, SysUtils, Variants, Classes,
Graphics, Controls, Forms,Dialogs, StdCtrls;

const
WM_XMSG = WM_USER+333;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
procedure xMsg (var msg: TMessage); message WM_XMSG;
public
{ Public declarations }
end;

TIDll = procedure(c_handle: integer); stdcall;
TSetHookHandle = procedure(HookHandle: HHook); stdcall;

var
Form1: TForm1;
CurrentHook : hHook;
LibLoaded: boolean;
LibHandle: HInst;
GHookInstalled: boolean;
IDll: TIDll;
HookProcAdd: pointer;
SetHookHandle: TSetHookHandle;

function LoadHookProc: boolean;

implementation

{$R *.dfm}

function LoadHookProc: boolean;
begin
LibHandle:=LoadLibrary('MouseHookDll.dll');
if LibHandle=0 then begin
LoadHookProc:=false;
exit;
end;
@IDll:=GetProcAddress(LibHandle,'IDll');
HookProcAdd:=GetProcAddress(LibHandle,'myHookProc');
@SetHookHandle :=
GetProcAddressLibHandle,'SetHookHandle');

if (@IDll=nil)or(HookProcAdd=nil)or
(@setHookHandle=nil) then begin
FreeLibrary(LibHandle);
LoadHookProc:=false;
exit;
end;
LoadHookProc:=true;
end;

procedure TForm1.xMsg(var msg: TMessage);
begin
Label1.Caption := IntToStr(msg.WParam)+':'+
IntToStr(msg.LParam);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if LibLoaded=false then LibLoaded:=LoadHookProc;
if LibLoaded=false then exit;
CurrentHook := SetWindowsHookEx(WH_MOUSE, HookProcAdd,
LibHandle, 0);
if CurrentHook=0 then
ShowMessage('Hook not installed !')
else begin
SetHookHandle(CurrentHook);
IDll(Form1.Handle);
end;
end;

procedure TForm1.Button2Click(Sender : TObject);
begin
UnhookWindowsHookEx(CurrentHook);
end;

end.

-----------------------------------------------
library MouseHookDll;

uses
SysUtils,
Classes,
Windows, Messages;

const
WM_XMSG = WM_USER+333;

var CurrentHook: HHook;
xhandle : integer;

procedure IDll (c_handle : integer); stdcall;
begin
xhandle := c_handle;
end;

function myHookProc (code : Integer;
wParam, lParam : LongInt) : LongInt;
stdcall;
var
X, Y : Integer;

begin
X := PMouseHookStruct(lParam).pt.X;
Y := PMouseHookStruct(lParam).pt.Y;

PostMessage(xhandle,WM_XMSG,x,y);

result := CallNextHookEx(CurrentHook, Code, wParam, lParam);

end;

procedure SetHookHandle(HookHandle: HHook); stdcall;
begin
CurrentHook:=HookHandle;
end;

exports
IDll index 1,
myHookProc index 2,
SetHookHandle index 3;

begin
end.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top