HOOK Introduction
2000-03-18 · - · CWW
Hook This thing is sometimes loveful, hook is used to intercept some of the information of the system, for example, we want the system to perform NotePad regardless of the CTL-B, perhaps you will use Form KeyPreview, set to True, but press CTL-B 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. 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 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 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 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 it is not set to do it, then 0 (so in general, Remote Hook passed into 0), while VB's local hook generally passed 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 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 intercept information directly.
KEYBOARD HOOK Example
Hook Function three parameters
Ncode WPARAM LPARAM Passage Value
HC_Action or hc_noremove table button Virtual Key with wm_keydown is the same if information is to be processed 0 Conversion 1
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
HHOOK = setWindowshookex (wh_keyboard, addressof mykbhfunc, app.hinstance, app.threadid)
END FUNCTION
Public Function Mykbhfunc (Byval Icode As Long, Byval LParam as long) As long
Mykbhfunc = 0 'indicates that you want to handle this information.
IF wparam = vbkeysnapshot life 'detection Do not press the PrintScreen button
Mykbhfunc = 1 'Eat this information in this hook
END IF
Call CallNexthookex (HHOOK, ICODE, WPARAM, LPARAM) is passed to the next HOOK
END FUNCTION
As for other hook details and NCODE, WPARAM, LPARAM, please check Win32 Help