Using the hook function [6]

Source: Internet
Author: User

The last example is to accept data from a DLL, so how do you pass data to a DLL? Also, it's not a good idea to have a Timer scan all the time when it comes to accepting data. This example solves these problems (but does not know solves the Bo friend "Peng" the question not?).

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

This example effect is the same as the previous example:

DLL file:

Library Myhook

uses
Sysutils,
Windows,
Messages,
Classes;
 
{$R *.res}

Const Wm_mymessage = wm_user + 1; {Custom Message} 

var
hook:hhook
info:string;
H:hwnd; {As Handle to external window}

{Gets the handle of the external window}
function Sethwnd (hwnd:hwnd): Boolean; stdcall;
begin
H: = hwnd;
Result: = True;
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]);

{Passes the data through the message to the specified window}
PostMessage (H, wm_mymessage, 0, Integer (Pchar (info));

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

function Sethook:boolean; stdcall
Const
WH_MOUSE_LL =14;
Begin
Hook: = SetWindowsHookEx (Wh_mou Se_ll, @MouseHOok, hinstance, 0);
Result: = Hook <> 0;
End;

function Delhook:boolean; stdcall
Begin
Result: = UnhookWindowsHookEx (hook);

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

Test code:

unit Unit1;

Interface

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

Const Wm_mymessage = wm_user + 1;

Type
TForm1 = Class (Tform)
Button1:tbutton;
Button2:tbutton;
Procedure formcreate (sender:tobject);
Procedure Button1Click (sender:tobject);
Procedure Button2click (sender:tobject);
Procedure Formdestroy (sender:tobject); 
Procedure Mymessage (var msg:tmessage); message wm_mymessage; {Define a message method accept message}
End;

function Sethook:boolean; stdcall
function Delhook:boolean; stdcall
Function Sethwnd (hwnd:hwnd): Bo Olean; stdcall;

var
form1:tform1;

Implementation

{$R *.DFM}

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

Procedure Tform1.button1click (sender:tobject);
begin
Sethook;
SetHWnd (Handle);
End;

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

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

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

{Displays the accepted content in the form}
procedure Tform1.mymessage (var msg:tmessage);
Begin
Text: = Pchar (Msg. LParam);
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.