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.FHook Hndl, nCode, WParam, LParam);
exit;
end;
case WParam of
WM_MOUSEMOVE:
begin
Form1.Label1.Caption := IntToStr(GetXY.X)+'/'+IntT oStr(GetXY .y);
end;
end;
Result := CallNextHookEx(Form1.FHook Hndl, 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(FHookH ndl) then
MessageDlg('UnHooked', mtInformation, [mbOK], 0);
end;
end.
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)^.
end;
begin
if ncode<0 then
begin
Result := CallNextHookEx(Form1.FHook
exit;
end;
case WParam of
WM_MOUSEMOVE:
begin
Form1.Label1.Caption := IntToStr(GetXY.X)+'/'+IntT
end;
end;
Result := CallNextHookEx(Form1.FHook
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Proc:=MyMouseProc;
FHookHndl := SetWindowsHookEx(WH_MOUSE,
if FHookHndl>0 then
MessageDlg('Hooked', mtInformation, [mbOK], 0);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if UnhookWindowsHookEx(FHookH
MessageDlg('UnHooked', mtInformation, [mbOK], 0);
end;
end.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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:\yourA ppPath\hoo k.dll');
keybHook:=SetWindowsHookEx (WH_KEYBOA RD,GetProc Address(dl l,'hookPro c'),dll,0) ;
end;
procedure EnableKeyboard;
begin
UnhookWindowsHookEx(keybHo ok);
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.
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:\yourA
keybHook:=SetWindowsHookEx
end;
procedure EnableKeyboard;
begin
UnhookWindowsHookEx(keybHo
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 :-)
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.
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
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
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);
low:=loword(num); not low:=lowword(num);