Use the hook function [2]

Source: Internet
Author: User
Although there are not many hook functions, their parameters are complex. You should start with the parameters before proceeding.

Unhookwindowshookex only needs the hook handle returned by setwindowshookex as the parameter, which is simple;

Let's take a look at the setwindowshookex statement:

 
Setwindowshookex (idhook: integer; {hook type} lpfn: tfnhookproc; {function pointer} hmod: hinst; {handle of the module containing hook functions (exe and DLL)} dwthreadid: DWORD {associated thread}): hhook;

The first parameter is very troublesome:

Parameter 4 dwthreadid: when setting a global hook, this parameter is generally 0, indicating that all threads are associated. In this example, it is a thread-level hook, so yes
Getcurrentthreadid.

Parameter 3 hmod: it is the handle of the module instance. You can use hinstance in both EXE and DLL to obtain the handle of the current instance. You can also use the API directly:
Getmodulehandle (nil ).

Parameter 2 lpfn: the pointer of the hook function. You can obtain the function pointer using both the @ and ADDR functions. The key here is the hook function:
First, different hook types correspond to different hook function structures. Win32 has 14 hook types, which are detailed annotations;
In this example, a keyboard hook is used. The parameter structure of the callback function of the keyboard hook is here. The function name we define does not matter. The parameters must follow Windows's rules.
Also, the call convention of this callback function must be: stdcall; In the above example, we declare it in the interface area first. If we do not declare it for direct implementation, we cannot forget this stdcall.

According to the preceding instructions, make the following changes:
The parameters of setwindowshookex are flexible;
In addition, the declaration of the hook function in the interface area is canceled, which is directly implemented;
The interception condition is canceled. All keyboard messages are intercepted now.

 
Unit unit1; interfaceuses 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; {define a hook handle} {this hook function is not declared in the interface area now. The parameter call method must be specified here: stdcall} function keyhook (ncode: integer; wparam: wparam; lparam: lparam): lresult; stdcall; begin beep; Result: = callnexthookex (Hook, ncode, wparam, lparam); end; {set keyboard hook} procedure tform1.formcreate (Sender: tobject); begin HOOK: = setwindowshookex (wh_keyboard, ADDR (keyhook), hinstance, getcurrentthreadid); end; {release keyboard hook} procedure tform1.formdestroy (Sender: tobject); begin unhookwindowshookex (Hook); end.

  

Why do hook functions have to use the stdcall call mechanism? Because the hook function is not appliedProgramCalled 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.