Using the hook function [2]

Source: Internet
Author: User
Tags integer

Although the hook function is not many, but its parameter is complex, should start from the parameter to go into deeply.

UnhookWindowsHookEx only need to setwindowshookex the return of the hook handle as a parameter, this simple;

First look at SetWindowsHookEx's statement:

SetWindowsHookEx(
idHook: Integer;  {钩子类型}
lpfn: TFNHookProc; {函数指针}
hmod: HINST;    {包含钩子函数的模块(EXE、DLL)的句柄}
dwThreadId: DWORD {关联的线程}
): HHOOK;

The first parameter was very troublesome, from the back said:

Parameter four dwthreadid: This parameter is typically 0 when setting the global hook, indicating that all threads are associated; This example is a thread-level hook, so it is

GetCurrentThreadID.

Parameter three hmod: is the handle of the module instance, can get the handle of the current instance with hinstance in EXE and DLL; You can also use the API directly:

GetModuleHandle (nil).

Parameter two LPFN: is the hook function pointer, uses the @ and the ADDR function all may obtain the function pointer; The key here is the hook function:

First, different hook types correspond to different hook function structure, WIN32 has 14 kinds of hook type, this is detailed annotation;

This example uses the keyboard hook, the keyboard hook callback function parameter structure here, we define the function name does not matter, the parameter must follow the Windows stipulation.

Also, the calling convention for this callback function must be: stdcall; In the example above, we declare the interface area first, and if you do not declare a direct implementation, you should not forget this stdcall.

Based on the above instructions, make the following modifications:

The parameters of SetWindowsHookEx are flexible;

And the cancellation of the hook function in the interface area of the declaration, is directly implemented;

The interception condition was canceled and now all keyboard messages are blocked.

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

var
hook: HHOOK; {定义一个钩子句柄}

{现在这个钩子函数没有在接口区声明, 这里必须指定参数调用方式: stdcall}
function KeyHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
Beep;
Result := CallNextHookEx(hook, nCode, wParam, lParam);
end;

{设置键盘钩子}
procedure TForm1.FormCreate(Sender: TObject);
begin
hook := SetWindowsHookEx(WH_KEYBOARD, Addr(KeyHook), HInstance, GetCurrentThreadId);
end;

{释放键盘钩子}
procedure TForm1.FormDestroy(Sender: TObject);
begin
UnhookWindowsHookEx(hook);
end;

end.

Why does the hook function have to use the stdcall invocation mechanism? Because the hook function is not invoked by the application, it is called by the system.

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.