HOOK Introduction

zhaozj2021-02-17  52

HOOK Introduction

Hook This thing is sometimes loveful, hook is used to intercept the system some messages. For example, we want the system to perform NotePad, you will use Form. KeyPreview, set to True, but press CTL-B in other processs? Then, this is to have a keyboard hook to intercept all key IN keys; if you: Mousemove's Event is only valid on the form or Control, if you want to know the Mouse Move message outside of Form , Then use the mouse hook to the message of the mouse. Another example: You want to record all keyboard actions or MOSUE actions, so that you can use JournalRecordhook, if you want to stop all MOSUE keyboard actions, and put (execute) the gauge, then use Journalplayback Hook; 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 IDHOOK AS Long, _

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_journalrRecord = 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 is generated, this hook function has certain 叁Digital format

Private function hookfunc (byval ncode as long, _

Byval wparam as long, _

BYVAL LPARAM AS Long AS Long

What is the hook generated below the NCODE representative, with a different group of possible values ​​with the HOOK. WPARAM LPARAM transmission value is different from the value of HOOK and the value of 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 be given, not necessarily hookfunc

HMOD represents .dll's Hinstance, if it is Local Hook, this value can be NULL (VB can be transmitted in), and if it is Remote Hook, you can use the getModuleHandle (". DLL name") to pass.

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 sets a Remote's keyboard hook, then who is the keyboard of the keyboard to 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 message is intercepted by b, what? Just look at the Hook Function of B. If b wants A's Hook Function to get this message, then B will call CallNextHooKex () to give this message PASS to A, which generates a connection of HOOK. If you don't want PASS in B to give A, don't call callnexthooKex ().

DECLARE FUNCTION CALLNEXTHOKEX 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 directly intercept messages.

KEYBOARD HOOK Example

Hook Function three parameters

Ncode WPARAM LPARAM Passage Value

===================================== ================ = ================ HC_Action Table button Virtual Key and WM_KeyDown Similar messages to be processed 0

Or vibrant 1

HC_NOREMOVE

Public HHOOK As Long

Public Sub UnHookKBD ()

IF HNEXTHOOKPROC <> 0 THEN

UnHookWindowshookex HHOOK

hHOOK = 0

END IF

End Sub

Public function enablekbdhook ()

IF hHOOK <> 0 THEN

EXIT FUNCTION

END IF

HHOOK = SETWINDOWSHOKEX (Wh_Keyboard, Addressof _

Mykbhfunc, app.hinstance, app.threadid)

END FUNCTION

Public Function Mykbhfunc (Byval icode as long, _

BYVAL WPARAM As Long, Byval LParam as Long AS Long

Mykbhfunc = 0 'indicates that you want to handle this message

IF wparam = vbkeysnapshot life 'detection Do not press the PrintScreen button

Mykbhfunc = 1 'Eat this message in this hook

END IF

Call CallNexthookex (HHOOK, ICODE, WPARAM, LPARAM) is passed to the next HOOK

END FUNCTION

As for other hook details, NCODE, WPARAM, LPARM, please check Win32 Help or Windows 95: A Developer's Guide (Jeffrey Richter) (Chinese Translation: Yuefeng Li Shu Lily Translation Hou Junjie WINDOWS95 Code Design Guide)

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

New Post(0)