Realization of VB hooks

xiaoxiao2021-03-30  213

Api start talking about: SetWindowsEx: a hook statement: Private Declare Function SetWindowsHookEx Lib "user32.dll" Alias ​​"SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long Parameter description: IDHOOK, the hook type to be installed, simple point is the type of message you want to intercept, is it a keyboard? Still mouse ... If the keyboard hook is: wh_keyboard. LPFN, the address of the hook subroutine, you can use the AdRess Of hook subroutine name in VB. HMOD, this parameter can be set to 0, which is used for global hooks. DWTHREADID, thread identifier, in VB, if set to 0, the hook you install is associated with all processes. This function has a pass value, the return value is the handle of the current hook. The use of hooks will increase the burden of system processing, so they will try to remove hooks without hooks, release hook resources, when they are used. So how do we release hooks? Look at the function unhookwindowshookex: Release a hook declaration: private declare function unhatlesswindowshookex lib "user32.dll" Description: hhook, the hook name to release is released, the system resources are not necessarily After successful release, this time is using the set hook = Nothing to completely release system resources. After installing the hook, you should pay attention to a problem, that is, after you install the hook, other programs may not receive the message, because when you intercept the message, there is no message to pass individual hooks, just handled internally. So you have to use a function: CallNexthookex Call the next hook, deliver a message to the next hook program: Public Declare Function Callnexthookex lib "user32.dll" (Byval HHOOK As Long, Byval Ncode As Long, Byval WParam As Long, LPARAM As Any) AS Long Parameter Description: HHOOK, SETWINDOWSHOKEX's return value Ncode, WPARAM, LPARAM is just its three parameters, and the specific role is not obvious. You can fill in this three ginseng directly when you use it. The specific installation is released, the call has been finished. The following rounds to the specific hook treatment process, this process is completely written by yourself, how to see your mood, but still some places need to talk.

When writing processing procedures, subroutine framework must be written this: private function hookname (byval ncode as long, byval wparam as long, byval Long) AS long processing process ... End Function In this subroutine, hookname is The name of the hook, you can write a name as you wish (but you must match the VB naming specification). Several parameters I come to explain to everyone: ncode: This parameter produces different values ​​in different hooks. WPARAM, LPARAM contains interception messages, and it is also different from the value of hooks and NCODE. For example, in the Keyboard, WPARAM is a button return code. Yes, this subroutine has a return value, this return value determines how you handle this message. Let's write a concrete hook to implement the interception of the message, this program will pop up the dialog when you press Shift S to remind you ~! If you don't understand, you will come back to find an answer, you can say that the following code is coming here (I also added a comment ~~ !!!). 'The following code is placed in a standard module, must be a standard module ~! (VB specified, no way ...) Public const wh_keyboard = 2 'wh_keyboard, this is a must, because the next step we want to use it to install the keyboard hook, wh_keyboard belongs to constants.

Public Const VK_SHIFT = & H10 'VK_SHIFT, following use it determines SHIFTDeclare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As LongDeclare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As IntegerDeclare Function SetWindowsHookEx Lib "user32" Alias ​​"SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As LongDeclare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As long 'above the API declaration, the API declaration, the API declaration of the PUBLIC HHOOK AS long' defines a global variable hHOOK for long plastic surgery, we use this variable to store the hook's handle PUBLIC FUNCTION Keyhook (Byval IDHook As Long, Byval WParam As Long , Byval LParam as long) AS long 'hook processing, Keyhook is a hook name, you can define if Idhook <0 Then' if the value of IDHOOK is less than 0, that is, no keyboard message KeyboardProc = CallNexthookex (HHOOK, IDHOOK WPARAM, BYVAL LPARAM 'So, then call the next hook else if (getKeyState (vk_shift) and & hf0000000) and wparam = ASC ("s") THEN' When you press SHIFT S, you will proceed as the following process Msgbox "you press SHIFT S ~! "'Use the MSGBOX pop-up prompt box, prompt" You press SHIFT S ~! "END IF 'End If Statement Keyhook = CallNexthookex (HHOOK, IDHOK, WPARAM, BYVAL LPARAM)' Call Next Hook End IFEND FUNCTION 'End Process' The following code is placed in the form PRIVATE SUB FORM_LOAD ()' When the form is read generated when taking hHook = SetWindowsHookEx (WH_KEYBOARD, AddressOf Keyhook, App.hInstance, App.ThreadID) 'for mounting a keyboard hook hook SetWindowsHookEx End SubPrivate Sub Form_Unload (Cancel as Integer) UnhookWindowsHookEx hHook' UnhookWindowsHookEx unloaded using a hook, hHook of hooks Handle end subhaha, the simplest Hook is used, can be used in the shortcut key, but have you found it when you run? At least you will pop up each time you press Shift S.

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

New Post(0)