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.
aptalaptalAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
mhervaisConnect With a Mentor Commented:
following but though I don't play with these things as well as you seem to do, I thing that windows will not report you the mousemoves outside the window you refer.

if you want to get informations of what happens outside, you vill have to manage the position of the mouse over all the widows handles starting at root for the screen.

will see if some other comments can be more precise.

by the way ... this is not an easy question

regards, marc  
0
 
mhervaisCommented:
oops, I wanted to make a comment not an answer. Sorry

0
 
rwilson032697Commented:
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.
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
GwenaCommented:
I'm watching here :-)
0
 
aptalaptalAuthor Commented:
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.
0
 
LischkeCommented:
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 http://www.experts-exchange.com/Q.10225486-2175184
0
 
craig_capelCommented:
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

0
 
craig_capelCommented:
Sorry Typo....


low:=loword(num); not low:=lowword(num);
0
All Courses

From novice to tech pro — start learning today.