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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mhervaisCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.