Board index » delphi » Mouse Hook

Mouse Hook


2003-10-31 01:45:14 PM
delphi162
I have a DLL that implements a windows hook (WH_MOUSE)
However this hook works only for the application that is calling the dll but
not all other process.
Here my DLL functions:
unit UnitLib;
interface
uses Windows, Messages;
const WM_MyMouse = WM_USER + $100;
function CallBackHook(nCode: integer; in_wParam: WPARAM; in_lParam:
LPARAM): LRESULT; stdcall;
procedure SetHandle(inHandle: THandle); stdcall;
procedure HookOn(inHandle: THandle); stdcall;
procedure HookOff; stdcall;
var HCaller: THandle;
HookHandle: HHOOK;
implementation
procedure SetHandle(inHandle: THandle);
begin
HCaller := inHandle;
end;
function CallBackHook(nCode: integer; in_wParam: WPARAM;
in_lParam: LPARAM): LRESULT;
begin
SendMessage(HCaller, WM_MyMouse, in_WPARAM, in_Lparam);
if nCode < 0 then
Result := CallNextHookEx(HookHandle, nCode, in_wParam, in_lParam)
else Result := 0;
end;
procedure HookOn(inHandle: THandle);
begin
SetHandle(inHandle);
HookHandle := SetWindowsHookEx(WH_MOUSE, @CallBackHook, HInstance, 0);
end;
procedure HookOff;
begin
UnhookWindowsHookEx(HookHandle);
end;
------
Where am I wron and what should I change to make it a global hook (for all
running processes)?
In help is written that if I want to make a hook for all the processes but
not the current one I am to call
SetWindowsHookEx(WH_MOUSE, @CallBackHook, HInstance, 0); with 0 as a last
parameter.
Thank you
 
 

Re:Mouse Hook

That is correct!
you must store the Window Handle of your all some where globally.
DLL's in Win32 do not share the same heap memory..
so each app in the system gets a different copy of your variable heap space.
if you were to have something like VC++ tools you could make ur self a
kernal chuck of memory that would be global how ever, borland for some
reason didn't think it was a required item. this means you need some form of
shared memory..
look into CreateFileMapping.
and create a Memory file. you can then switch this memory into the DLL
to get the window handle that you stored that when you initiated the DLL from
your app...
ect...
Georgi Sandev writes:
Quote
I have a DLL that implements a windows hook (WH_MOUSE)
However this hook works only for the application that is calling the dll but
not all other process.
Here my DLL functions:
unit UnitLib;

interface
uses Windows, Messages;

const WM_MyMouse = WM_USER + $100;

function CallBackHook(nCode: integer; in_wParam: WPARAM; in_lParam:
LPARAM): LRESULT; stdcall;
procedure SetHandle(inHandle: THandle); stdcall;
procedure HookOn(inHandle: THandle); stdcall;
procedure HookOff; stdcall;
var HCaller: THandle;
HookHandle: HHOOK;

implementation

procedure SetHandle(inHandle: THandle);
begin
HCaller := inHandle;
end;

function CallBackHook(nCode: integer; in_wParam: WPARAM;
in_lParam: LPARAM): LRESULT;
begin
SendMessage(HCaller, WM_MyMouse, in_WPARAM, in_Lparam);
if nCode < 0 then
Result := CallNextHookEx(HookHandle, nCode, in_wParam, in_lParam)
else Result := 0;
end;

procedure HookOn(inHandle: THandle);
begin
SetHandle(inHandle);
HookHandle := SetWindowsHookEx(WH_MOUSE, @CallBackHook, HInstance, 0);
end;

procedure HookOff;
begin
UnhookWindowsHookEx(HookHandle);
end;
------
Where am I wron and what should I change to make it a global hook (for all
running processes)?
In help is written that if I want to make a hook for all the processes but
not the current one I am to call
SetWindowsHookEx(WH_MOUSE, @CallBackHook, HInstance, 0); with 0 as a last
parameter.
Thank you