A two-step:
First, establish the DLL, and implement the hook in the DLL settings, release and hook function;
Second, build another project call test.
The first step: do the DLL
First build a DLL project, the initial code is as follows (remove the annotation):
Library Project1
uses
Sysutils,
Classes;
{$R *.res}
Begin
End.
//Save the project as MYHOOK.DPR and implement the following:
Library Myhook
uses
Sysutils,
windows, {Hook functions from Windows Unit}
Messages, {message wm_lbuttondown defined in Messages cell}
classes;
{$R *.res}
var
hook:hhook; {Hook variable}
{hook function, mouse message too much (for example, mouse movement), must have a choice, here Select the left mouse button press}
function Mousehook (ncode:integer; wparam:wparam; Lparam:lpara M): lresult; stdcall;
Begin
If WParam = Wm_lbuttondown then
begin
MessageBeep (0),
end;
Result: = CallNextHookEx (Hook, ncode, WParam, LParam);
End;
{Create hook}
function Sethook:boolean stdcall
begin
Hook: = SetWindowsHookEx (Wh_mouse, @MouseHook, HIns tance, 0);
Result: = Hook <> 0;
End;
{release Hook}
function Delhook:boolean stdcall
Begin
Result: = UnhookWindowsHookEx (hook);
End;
br> {output function as required by DLL}
Exports
Sethook name 'Sethook ',
Delhook name ' Delhook ',
Mousehook name ' Mousehook ';
//sethook, Delhook, Mousehook; {If you do not need to change your name, you can exports it directly}
Begin
End.
Note: The first parameter of SetWindowsHookEx wh_mouse that this is a mouse hook; The fourth parameter 0 illustrates the global.
The mouse hook callback function is formatted here: www.cnblogs.com/del/archive/2008/02/25/1080724.html
Then press CTRL+F9 to compile, in the engineering directory will generate a file with the same name as the project, here is: MyHook.dll.