Using the hook function [3]

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.