Application of HOOK in VB.NET (1)

zhaozj2021-02-16  54

Application of HOOK in VB.NET (1)

In the VB.NET version, there is a single game, which has been thinking how to better help everyone understand the new concepts and programming methods of VB.NET. A small postback is difficult to say a lot of problems, so use the broad world of the documentation to carry out more discussion. I have limited level, there is nothing wrong with misfortune, please ask your friends to criticize.

The basic idea is to write a series of articles, explain the difference between VB.NET and VB, the new concept in VB.NET, the use of VB.NET controls, and the new ways of solving the problem. If time allows, I hope that I can work soon. More importantly, netizens' support and encouragement will be my continued source of power.

Today is the first time I wrote, I don't know where to start, weigh the third, decided to start from the API call problem. VB's enthusiasts often write to me or post, ask for changes in the API in VB.NET, then today we will discuss it. Although Microsoft launches VS.NET's purpose is to use it for cross-platform, the Windows API is not recommended, but from this, it can make us more easily understand the changes made by VB.NET, so:

Let's go ..................................................

This article is not an article that explains the principle of hook, just discuss how the hook function is called in VB.NET, as well as changes in the API used in VB.NET.

Due to many hook types, this paper analyzes the most commonly used keyboard hook as an example.

First, let's take a look at how it is implemented in VB. The cut is a space bar. The target is: an form, there is a textbox above, whether the focus falls in TextBox, press the space bar, not entering a space in TextBox, but becomes a sentence in TextBox: "Hook success! "

1. Write the following code in a module:

Application of the called API:

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

Function Description: This function is used to start the HOOK settings.

IDHOOK is the type of hook, that is, the type of message processing.

LPFN is the address pointer of the hook subsequence (function or process). If the DWTHREADID parameter is 0 or a thread that is created by another process, the LPFN must point to the Hook subtertaine in the DLL. In addition, LPFN can point to a HOOK subtertic code of the current process (which we use this nature).

HMOD is the handle of the application instance, identifies the DLL of the subtextone that contains LPFN. If the DWTHReadID identifies a thread created by the current process, and the subsystem is located in the current process, and the HMOD must be NULL.

DWTHREADID is an identifier of threads associated with the installation hook subroutine, if 0, the Hook subsequence is associated with all threads. Return Value: The function is successful, returns the handle of the hook child, and the failure returns NULL.

Declare Function UnHookWindowsHookex Lib "User32" (Byval HHOOK AS Long) As long

Function Description: This function is to release hook. HHOOK is the handle of the hook function.

Declare Function CallNexthookex Lib "User32" (Byval Ncode As Long, Byval WParam As Long, LPARM AS ANY) AS Long Function Description: This function is to pass the HOOK information in the current hook chain to the next hook .

HHOOK is the current Hook handle, an application receives this handle, as the result of the previous call setWindowsHookex function.

Ncode refers to the HOOK code passed to the current hook process. The next hook process uses this code to decide how to handle HOOK information.

WPARAM refers to the WPARAM value passed to the current hook process, which is determined by the type of HOOK in the current HOOK chain.

LPARAM refers to the LPARAM value passed to the current hook process, which is determined by the type of HOOK in the current hook chain.

2. The defined constant is:

Public HNEXTHOOKPROC AS Long

Public const wh_keyboard = 2 'This is the type of hook is the keyboard HOOK

Public const pm_key_space = & h20 'space bar

3. Code segment

Public Sub UnHookkBd () 'Default Hook Function

IF HNEXTHOOKPROC <> 0 THEN

UnHookWindowshookex HNEXTHOOKPROC

HNEXTHOOKPROC = 0

END IF

End Sub

Public function enablekbdhook () Settings Keyboard HOOK

IF HNEXTHOOKPROC <> 0 THEN

EXIT FUNCTION

END IF

HNEXTHOKPROC = SETWINDOWSHOKEX (Wh_Keyboard, Addressof _

Mykbhfunc, app.hinstance, 0)

IF HNEXTHOOKPROC <> 0 THEN

Enablekbdhook = HNEXTHOOKPROC

END IF

END FUNCTION

Public Function Mykbhfunc (Byval icode as long, _

BYVAL WPARAM As Long, Byval LParam as Long AS Long

Mykbhfunc = 0

Icode <0 THEN

Mykbhfunc = CallNexThookex (HNEXTHOKPROC, ICODE, WPARAM, LPARAM)

EXIT FUNCTION

END IF

IF wparam = pm_key_space the '' detection is there is a space button

Mykbhfunc = 1

'Join your code, used to indicate a response

Form1.text1.text = "Hook success!"

END IF

END FUNCTION

4. The code in Form is simple:

Private sub flow_load ()

Call enablekbdhook

End Sub

Private Sub Form_Unload (Cancel AS Integer)

Call unhookkbd

End Sub

After finishing the work! Now, in the Form form, you will knock the empty tier, you will respond to the code written in the Mykbhfunc function.

(Endlessly)

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

New Post(0)