Hook mechanism

xiaoxiao2021-03-06  19

Transfer: Use of 9CBS hook (HOOK) Mechanism: E Meng Yuan SetwindowsHookEx function provides 15 different types of message monitoring, i.e. 15 different hooks. A message used to capture a particular type or a range of a range (such as a keyboard message, mouse message, etc.). We only use the mouse hook as an example, discuss how to write a DLL program under Delphi and how to install the mouse hook function in its own program. Windows provides API functions SetwindowsHookEx to establish a Hook, may be added by the function of a program to Hook chain monitoring Windows message function syntax is: SetWindowsHookEx (idHook: Integer; lpfn: TFNHookProc; hmod: HINST; dwThreadId: DWORD) wherein: Parameter IDHOOK Specifies the established monitoring function type. The parameter LPFN specifies the message function. After the corresponding message is generated, the system calls the function and passes the message value to the function. The general form of functions is: hookProc (Code: integer; lparam: wparam; lparam: lparam): LRESULT stdcall; where Code is a system indicator mark, WPARAM, and LPARAM are additional parameters, depending on different messaging types. As long as you create such a function in the program, you can add a message to the message monitoring chain through the SetWindowsHookex function. Since the hook filtering function must be in a separate module, that is, we must first generate a DLL frame, then add hook function code and other related function code. First, the hook writing Instructions 1. Mr. is a DLL frame 2, writing its own hook filtering function hook filter function must be a callback function, its function form: function KeyhookProc (icode: integer; wparam: wparam; lparam: lparam): LRESULT stdcall; export; 3, add your own hook processing function in the generated DLL frame.

4, mounting the SetWindowsHookEx function (Usage supra) HOOK 5, with UnHookWindowsHookEx unloading hook} library mousehook; uses SysUtils, Classes, Windows, messages, shellapi; type Tcallbackfun = procedure (info: pchar); Tmousehook = record isrun: boolean; hook: hhook; callbackfun: Tcallbackfun; end; var mymousehook: Tmousehook; {$ R * .res} function gethookinfo (code: integer; wp: WPARAM; lp: LPARAM): LResult; stdcall; var info: string; begin if code <0 then begin result: = CallNextHookEx (mymousehook.hook, code, wp, lp); exit; end; info: = ''; Case wp of WM_LBUTTONDOWN: begin info: = 'WM_LBUTTONDOWN '; end; WM_LBUTTONUP: begin info: = 'WM_LBUTTONUP '; end; WM_LBUTTONDBLCLK: begin info: = 'WM_LBUTTONDBLCLK '; end; WM_RBUTTONDOWN: begin info: = 'WM_RBUTTONDOWN '; end; WM_RBUTTONUP: begin info: = 'WM_RBUTTONUP '; end; WM_RBUTTONDBLCLK: begin info: = ' WM_RBUTTONDBLCLK '; end; WM_MBUTTONDOWN: begin info: =' WM_MBUTTONDOWN '; end; WM_MBUTTONUP: begin info: =' WM_MBUTTONUP '; end; WM_MBUTTONDBLCLK: begin info: =' WM_MBUTTONDBLCLK '; end; WM_NCMouseMove, WM_MOUSEMOVE: begin info: =' WM_MO Usemove '; end; end; info: = INFO ' POS (' INTOSTR (PMousehookTRuct (LP) ^. Pt.x) ', ' INTOSTR (PMousehookTruct (LP) ^. Pt.x) ') '; mymousehook.callbackfun (pchar (info)); result: = CallNextHookEx (mymousehook.hook, code, wp, lp); end; procedure installmousehook (callbackF: Tcallbackfun); stdcall; begin if not mymousehook.isrun then begin mymousehook.hook : = SETWINDOWSHOKEX (wh_mouse, @ getHookInfo, hinstance, getcurrentthreadid ()); mymousehook.callbackfun: = callbackf; mymousehook.ismrun: =

转载请注明原文地址:https://www.9cbs.com/read-42672.html

New Post(0)