HOOK and its application

zhaozj2021-02-16  173

HOOK and its application

Hook is really a great guy, it sometimes loves! So what is HOOK? It turns out, it is used to intercept the system some information, for example, we want the system to perform Notepad regardless of the CTL-N, maybe you will use Form's KeyPreview, set to true, but Press CTL-N in other processs? Then, you have to set up a keyboard hook to intercept all key in the key; if you: Mousemove is valid only on the form or Control, if you want to know the Mouse Move information outside the FORM , Then use the mouse hook to the information of the mouse. Hook, can be the REMOTE HOOK, that is, the action of other processs can also intercept, or Localhook, its interception range is only Process itself. Remote Hook Hook Function To be in .dll, local hook is in .bas.

How to set HOOK in VB? Use setWindowshookex ()

Declare function setwindowshookex lib "user32" alias "setwindowshookexa" (Byval LPFN As Long, Byval HMOD AS Long, BYVAL DWTHREADID AS Long) AS Long

What kind of hook represents the IDHOOK representative, with the following

Public Const WH_CALLWNDPROC = 4 Public Const WH_CALLWNDPROCRET = 12 Public Const WH_CBT = 5 Public Const WH_DEBUG = 9 Public Const WH_FOREGROUNDIDLE = 11 Public Const WH_GETMESSAGE = 3 Public Const WH_HARDWARE = ​​8 Public Const WH_JOURNALPLAYBACK = 1 Public Const WH_JOURNALRECORD = 0 Public Const WH_KEYBOARD = 2 Public const wh_mouse = 7 public const wh_msgfilter = (-1) public const wh_shell = 10 public const wh_sysmsgfilter = 6

LPFN represents the address of Hook Function, this is a Callback FuCnction, when a HOOK is hooked, we have to define a function as a message to handle it, this hook function has certain parameters. format

Private function hookfunc (Byval Ncode As Long, Byval LParam as long) As long

Ncode represents the hook produced under the condition, and the possible value of different groups of possible wparam lparam transmission values ​​vary with HOOK, different from the value of hook and NCODE.

Because this parameter is a Function address, we fix the Hook Function in .bas and incorporated with Addressof Hookfunc. As for the name of Hook Function, we can give any given, not necessarily hookfunchmod representative .dll's Hinstance, if it is Local Hook, this value can be NULL (VB can be transmitted 0), and if it is Remote Hook, you can use getModuleHandle (".dll Name") is incoming.

DWTHREADID represents this hook's ThreadID, if not set is that thread is being made, then zero 0 (so in general, the Remote Hook passed into the 0), while the VB's local hook generally passes App.ThreadID.

Valueback value If setWindowshookex () is successful, it will pass a value, representing the current Hook Handle, this value is to be recorded.

Because the A program can have a System Hook, such as the Keyboard Hook, and the B program also has a Remote's Keyboard Hook, then who is the keyboard information Who is intercepted? The answer is that the last one is intercepted, that is, a first to make a keyboard hook, and then B is doing, the information is intercepted by b, what? Just look at the Hook Function of B. If b wants A's Hook Function to get this information, then b To call callNexthooKex () to give this information PASS to A, then a connection of HOOK. If you don't want PASS this information to A, don't call callnexthooKex ().

Declare function callnexthookex lib "user32" alias "callnexthookex" _ (byval hhook as long, _ byval ncode as long, _ byval wparam as long, _ lparam as any) As long

The hHOOK value is the return value of SetWindowsHooKex (), Ncode, WPARAM, LPARAM is three parameters in hook procedure.

Finally, remove this hook, please call unHookWindowHookex ()

Declare Function UnHookWindowshookex lib "user32" alias "unhockwindowshookex" _

(BYVAL HHOOK AS Long) As Long

HHOOK is the return value of SetWindowsHooKex (). At this time, in the above example, the B program ends hook, then change A can intercept information directly.

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

New Post(0)