Reading-the second day of Windows core programming in Delphi

Source: Internet
Author: User

Technical Exchange, DH explanation.

Go to the second chapter of this book today (Hook ).
What is a hook first?
Hooks are actually added with a filter layer in your environment. In special cases, the hook callback function is triggered.
For example, if we have installed a global keyboard hook, when we press the key on the keyboard, the hook callback function will be triggered.
We can also see that it was originally straight forward. As a result, a filtering layer is installed in the middle, and the system efficiency will definitely decrease.
To understand, we only need to know three functions:
1 hook function:

Function setwindowshookex (idhook: integer; // hook type lpfn: tfnhookproc; // callback function pointer hmod: hinst; // generally 0 or instancedwthreadid: DWORD // The Global hook is 0, otherwise it is the specified process ID): hhook; stdcall; // if the process is successful, a value is returned. if the process is unsuccessful, the value 0 is returned.

The hook types include:

{$ Externalsym wh_min} wh_min =-1; {$ externalsym wh_msgfilter} wh_msgfilter =-1; {$ externalsym timeout} timeout = 0; {$ externalsym wh_journalplayback} timeout = 1; {$ externalsym wh_keyboard} wh_keyboard = 2; {$ externalsym wh_getmessage} wh_getmessage = 3; {$ externalsym response} response = 4; {$ externalsym wh_cbt} wh_cbt = 5; {$ externalsym listener} wh_sysmsgfilter = 6; {$ externalsym wh_mouse} wh_mouse = 7; {$ externalsym wh_hardware} wh_hardware = 8; {$ externalsym wh_debug} wh_debug = 9; {$ externalsym wh_shell} wh_shell = 10; {$ externalsym release} release = 11; {$ externalsym release} release = 12; {$ externalsym wh_keyboard_ll} wh_keyboard_ll = 13; {$ externalsym hooks} links = 14; {$ externalsym wh_max} wh_max = 14; {$ externalsym wh_minhook} wh_minhook = wh_min; {$ externalsym wh_maxhook} wh_maxhook = wh_max;

The specific function is basically based on the name, and I don't know either of them. For details, refer to msdn. This is a sunflower collection.
2 unload hook function:

Function unhookwindowshookex (HHK: hhook // handle returned by the hook function): bool; stdcall;

3. Specific hook callback functions:

Tfnhookproc = function (Code: integer; wparam: wparam; lparam: lparam): lresult stdcall;

Let's take a look at the example:

VaR H: hhook; function mykeyboardhookproc (Code: integer; wparam: wparam; lparam: lparam): lresult stdcall; begin if code = hc_action then begin // wparam is Marshal code // lparam needs to split the byte showmessage (format ('click % d key % d', [wparam, lparam and $0000 FFFF]); end; // you must remember to call the next hook, because we need to know that this is not just something you want to handle, but someone else needs to handle it, // if you do so, you won't pass it down. I also have a solution... calculate your bt result: = callnexthookex (H, code, wparam, lparam); end; Procedure tform2.button1click (Sender: tobject); begin H: = setwindowshookex (wh_keyboard, success, 0, getcurrentthreadid); end; Procedure tform2.button2click (Sender: tobject); begin unhookwindowshookex (h) end;

The example of this hook is only valid for our own processes. If we want to be global, it is also valid for other processes, then we need to write the hook code into the DLL.

Next we will demonstrate global hooks.

VaR H: hhook; exehandle: Cardinal; // This handle must be mapped out with memory, because the global keyboard hook, // as longProgramWhen a key is pressed, the DLL will be loaded into the process, so the multi-process is formed. Here I will not get function mykeyboardproc (Code: integer; wparam: wparam; lparam: lparam): lresult stdcall; begin if code = hc_action then postmessage (exehandle, wm_hotkey, wparam, lparam); // The result is directly forwarded out of laziness: = callnexthookex (H, code, wparam, lparam) end; function hookon (ahandle: Cardinal): Boolean; export; // export it begin exehandle: = ahandle; H: = setwindowshookex (wh_keyboard, mykeyboardproc, hinstance, 0); // The last parameter must be 0 result: = H <> 0; // determine whether the end is successful; function hookoff (): Boolean; export; begin result: = unhookwindowshookex (h) end;

I will talk about other API hooks later. Here I will explain how to use them. I hope it will be helpful.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.