Using the hook function [5]

Source: Internet
Author: User

Bo friend "Peng" letter to discuss the problem of hooks, the core difficulty is: The data in the DLL how to pass out. This question is discussed in the next two examples.

This example establishes a global mouse hook and then passes the information about the mouse through a custom GetInfo function to the calling hook program.

To facilitate testing, provide a source download bar: Http://files.cnblogs.com/del/MouseHook_1.rar

This example effect chart:

DLL file:

Library Myhook

uses
Sysutils,
Windows,
Messages,
Classes;

{$R *.res}

var
hook:hhook;
Info:string;

function Getinfo:pchar;
Begin
Result: = Pchar (info),
end;

function Mousehook (ncode:integer; Wparam:wparam lparam:lparam): lresult; stdcall;
Begin
Case WParam of
Wm_mousemove:info: = ' mouse position ';
Wm_lbuttondown:info: = ' press ';
Wm_lbuttonup:info: = ' let go ';
End;
Info: = Format ('%s:%d,%d ', [info, pmousehookstruct (lParam) ^.pt. X, Pmousehookstruct (LParam) ^.pt. Y]);
{This information can be extracted externally from the GetInfo function}

Result: = CallNextHookEx (hooks, ncode, WParam, LParam);
End;

function Sethook:boolean; stdcall; 
Const
WH_MOUSE_LL = 14; {Delphi gave two constants less: WH_KEYBOARD_LL = 13; Wh_mouse_ll = 14; Define yourself when needed
begin
Hook: = SetWindowsHookEx (Wh_mouse_ll, @MouseHook, hinstance, 0); {Wh_mouse is only a thread-level}
Result: = Hook <> 0;
End; 

Function Delhook:boolean;stdcall;
Begin
Result: = UnhookWindowsHookEx (Hook),
end;

Exports Sethook, Delhook, Mousehook, GetInfo;
Begin
End.

Test code:

unit Unit1;

Interface

uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, S Tdctrls, Extctrls;

Type
TForm1 = Class (Tform)
Button1:tbutton;
Button2:tbutton;
Timer1:ttimer;
Procedure formcreate (sender:tobject);
Procedure Button1Click (sender:tobject);
Procedure Button2click (sender:tobject);
Procedure Timer1timer (sender:tobject);
Procedure Formdestroy (sender:tobject);
End;

function Sethook:boolean; stdcall
function Delhook:boolean; stdcall;
function Getinfo:pchar; stdcall ;

var
form1:tform1;

Implementation

{$R *.DFM}

function sethook; external ' MyHook.dll ';
function delhook; external ' MyHook.dll ';
function GetInfo; external ' MyHook.dll ';

Procedure Tform1.button1click (sender:tobject);
begin
Sethook;
timer1.enabled: = True;
End;

Procedure Tform1.button2click (SendeR:tobject);
Begin
Timer1.enabled: = False;
Delhook;
End;

Procedure tform1.formcreate (sender:tobject);
Begin
Button1.caption: = ' install hook ';
Button2.caption: = ' load unload hook ';
Timer1.interval: = 100;
Timer1.enabled: = False; 
Formstyle: = Fsstayontop; {To test, keep the window in front}
End;

Procedure Tform1.formdestroy (sender:tobject);
Begin
Delhook
End;

Procedure Tform1.timer1timer (sender:tobject);
Begin
Text: = GetInfo
End;

End.

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.