Link to home
Start Free TrialLog in
Avatar of aptalaptal
aptalaptal

asked on

Install A hook to All threads

i want to install the hook all the threads not only the current thread.

how can i do that?

i would like to monitor all mouse messages.
i made a program seen below.
it uses Wh_Mouse hook but doesnot work when the mouse cursor is outside of my application. While the mouse cursor is somewhere on my form, it displays mouse x,y point.
i guess i forget something.

the code is below :

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
   FHookHndl : THandle;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

type
  MouseProc = function (nCode : Integer;
                    WParam: WPARAM;
                    LParam: LPARAM): UINT; stdcall;

{$R *.DFM}

var
   proc:MouseProc;

function MyMouseProc(nCode : Integer; WParam: WPARAM; LParam: LPARAM): UINT; stdcall;

function GetXY : TPoint;
begin
 Result := PMouseHookStruct(LParam)^.pt;
end;

begin

 if ncode<0 then
    begin
     Result := CallNextHookEx(Form1.FHookHndl, nCode, WParam, LParam);
     exit;
    end;

 case WParam of
    WM_MOUSEMOVE:
       begin
         Form1.Label1.Caption := IntToStr(GetXY.X)+'/'+IntToStr(GetXY.y);
       end;
 end;

 Result := CallNextHookEx(Form1.FHookHndl, nCode, WParam, LParam);
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
 Proc:=MyMouseProc;
 FHookHndl := SetWindowsHookEx(WH_MOUSE, @Proc, 0, GetCurrentThreadID);
 if FHookHndl>0 then
    MessageDlg('Hooked', mtInformation, [mbOK], 0);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 if UnhookWindowsHookEx(FHookHndl) then
    MessageDlg('UnHooked', mtInformation, [mbOK], 0);
end;

end.
ASKER CERTIFIED SOLUTION
Avatar of mhervais
mhervais

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mhervais
mhervais

oops, I wanted to make a comment not an answer. Sorry

ANSWER!

You need to put the hook in a DLL, like this: (credit to madshi!)

This is the code to disable the keyboard system wide.

// app

var dll, keybHook : cardinal;

procedure DisableKeyboard;
begin
  dll:=LoadLibrary('c:\yourAppPath\hook.dll');
  keybHook:=SetWindowsHookEx(WH_KEYBOARD,GetProcAddress(dll,'hookProc'),dll,0);
end;

procedure EnableKeyboard;
begin
  UnhookWindowsHookEx(keybHook);
  FreeLibrary(dll);
end;

// dll

library hook;

function hookProc(code, wParam, lParam: integer) : integer; stdcall;
begin
  result:=1;
end;

exports hookProc index 1 name 'hookProc';

end.

// Same method for the mouse, just change WH_KEYBOARD to WH_MOUSE...

Cheers,

Raymond.
I'm watching here :-)
Avatar of aptalaptal

ASKER

Okey this is what i guessed.
Then i should call next hook in chain.

in dll project

function hookProc(code, wParam, lParam: integer) : integer; stdcall;
begin
// codes here

   result:=CallNextHook(  ?  , code, WParam, LParam);
end;

i get hook handle in host application. i should get the same hook handle in the dll to call next hook in chain.

is there such an API to import variable values from host application or something else to get the current hook handle.
aptalaptal,

what I don't understand here is why you ask the same question again and deleted the old one which I already answered for you? I wrote alread what you have to do but you haven't even commented what I gave you!

Ciao, Mike

PS: The question was https://www.experts-exchange.com/Q.10225486-2175184
Is it only the mouse possitions that you are after? or it that you need to hook it?

If you can do it another way this piece of code works just fine......

var
  num: dword;
  hi,low: word;


num:=getmessagepos();
hi:=hiword(num);
low:=lowword(num);


ok i just had to search for that, l lost my code

That will return the x and y possitions in Hi and Low

Sorry Typo....


low:=loword(num); not low:=lowword(num);