VB omnipotent 3: hook for VB to intercept Windows messages

Source: Internet
Author: User
Tags what callback what callback function

Address: http://blog.csdn.net/useway "Java programmers, work that thing"

-- Author: bellVB has always been regarded as a language that can be used to complete Windows interface applications in a simple way. For programming on Windows systems, it seems that the first thought is definitely not VB, and most programmers must think of VC. Today, we will introduce a method to intercept windows global messages using VB. 1. Windows global message IntroductionI don't like to introduce a simple thing in a very obscure language. Therefore, I want to explain this concept in plain language in this article. What is Windows global message? For example, if you move the cursor over a custom window, you can use the related methods in this form. However, if the mouse does not move in its own form, how can we get its position? For example, How can I obtain the key value of a keyboard when I hit the keyboard in another place without hitting the keyboard in my own form? Anyone who knows about single-chip microcomputer knows that the mouse and keyboard operations are completed by the "interrupt" trigger event. When the system is "interrupted, messages are sent to the operating system, and these messages are windows global messages. Ii. Hook IntroductionHooks are translated by English hooks. The so-called hooks are intercepted before Windows global messages are transmitted to the operating system. After processing, the hooks are passed to the operating system or other hook programs. Think about what programs do hooks sound like? Yes, hackers and Trojans use hooks to obtain the operation information of the mouse and keyboard, as well as passwords or other useful information. However, the hook itself is not a virus, but a method provided to our programmers to obtain operating system actions. This method is sometimes very useful, you can easily develop high-quality programs for the operating system. Iii. Message Types1. wh_callwndproc and wh_callwndprocret are messages sent to the window process. The system calls wh_callwndproc before the message is sent to the Receiving Window Process, and calls wh_callwndpro after the message is processed in the window process. 2. wh_cbt refers to window events such as activation, creation, destruction, minimization, maximization, movement, and size change; complete system commands; move the mouse and keyboard events from the system message queue; set input focus events and synchronize system message queue events. 3. wh_keyboard wm_keydown monitoring wm_keyup 4. wh_keyboard_ll monitoring keyboard messages that are input to the thread Message Queue 5. wh_mouse monitoring mouse messages that are input to the Message Queue 6. wh_mouse_ll monitoring mouse input to the thread Message Queue message Iv. Hook type1) thread HOOK: monitors the event messages of a specified thread.
2) system HOOK: monitors the event messages of all threads in the system. VB can only set thread hooks. Because system hooks affect all applications in the system, hook functions must be placed in an independent dynamic link library (DLL. V. steps for establishing a hook using VB  Step 1: hookHook is the first step. The purpose is 1. Tell the operating system what messages I want to listen to, and 2. Tell the operating system what callback function is called after the message is received. Hook method: for example: Private const wh_mouse_ll as long = 14 private hhook as long ...... hhook = setwindowshookex (wh_mouse_ll, addressof hookproc, app. hinstance, 0) first parameter: Tell the operating system that I want to hook up with a mouse listener. The second parameter: Tell the operating system that my callback function is hookproc, and addressof is used to obtain the address of the hookproc function. Step 2: Compile the callback functionPublic Function hookproc (byval ncode as long, byval wparam as long, byval lparam as long) as long
Dim typmhs as msllhookstruct, Pt as pointapi if wparam = wm_mousemove then
Call copymemory (typmhs, byval lparam, lenb (typmhs ))
PT = typmhs.pt
'If ptinrect (RTL, Pt. X, Pt. Y) <> 0 then
'Hookproc = 1'
'Else
Form1.caption = "mouse cursor at" + CSTR (Pt. X) + "," + CSTR (Pt. Y)
Hookproc = 0' to complete the pending operations
'End if
End if

If wparam = wm_lbuttondown then
Debug. Print "L"
End if

If wparam = wm_rbuttondown then
Debug. Print "R"
End if

'If ncode <0 then
Hookproc = callnexthookex (hhook, ncode, wparam, lparam)
'Exit Function
'End if

End function NOTE 1: The callback function format is public function hookproc (byval ncode as long, byval wparam as long, byval lparam as long) As long ncode: hook handle
Wparam, lparam: contains the intercepted message content. It is also different from the hook type and ncode value. For example, in the keyboard, wparam is the return code of the key. If it is a mouse event, it contains the mouse location information and key information. NOTE 2: at the end of the callback function, you need to add callnexthookex to pass the message to the next hook or operating system. This is also easy to understand. After you intercept a message, you do not really want to intercept the message, but want to process it accordingly and then pass the message to the next contact. Step 3: Release the hookAfter the program ends, you need to remove the hook. Otherwise, the system will always listen to messages. Call unhookwindowshookex (hhook) 6. Complete VB hook ProgramModule code: Option explicit
Private declare sub copymemory lib "kernel32.dll" alias "rtlmovememory" (byref destination as any, byref source as any, byval length as long)
Private 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
Private declare function unhookwindowshookex lib "USER32" (byval hhook as long) as long
Private declare function callnexthookex lib "USER32" (byval hhook as long, byval ncode as long, byval wparam as long, lparam as any) as longprivate const hc_action = 0
Private const wh_mouse_ll as long = 14
Private const wm_mousemove = & h200
Private const wm_lbuttondown = & h201
Private const wm_lbuttonup = & H202
Private const wm_lbuttondblclk = & h203
Private const wm_rbuttondown = & h204
Private const wm_rbuttonup = & h205
Private const wm_rbuttondblclk = & h206
Private const wm_mbuttondown = & h207
Private const wm_mbuttonup = & h208
Private const wm_mbuttondblclk = & h209
Private const wm_mouseactivate = & h21
Private const wm_mousefirst = & h200
Private const wm_mouselast = & h209
Private const wm_mousewheel = & h20a above is the private type pointapi of each mouse Value
X as long
Y as long
End typeprivate type msllhookstruct
Pt as pointapi
Mousedata as long
Flags as long
Time as long
Dwextrainfo as long
End typepublic hhook as longpublic sub enablehook ()
If hhook = 0 then
Hhook = setwindowshookex (wh_mouse_ll, addressof hookproc, app. hinstance, 0)
End if
End subpublic sub freehook ()
If hhook <> 0 then
Call unhookwindowshookex (hhook)
Hhook = 0
End if
End subpublic function hookproc (byval ncode as long, byval wparam as long, byval lparam as long) as long
Dim typmhs as msllhookstruct, Pt as pointapi if wparam = wm_mousemove then
Call copymemory (typmhs, byval lparam, lenb (typmhs ))
PT = typmhs.pt
Debug. Print "mouse cursor at" + CSTR (Pt. X) + "," + CSTR (Pt. Y)
End if

If wparam = wm_lbuttondown then
Debug. Print "L"
End if

If wparam = wm_rbuttondown then
Debug. Print "R"
End if

Hookproc = callnexthookex (hhook, ncode, wparam, lparam)
 
End function window code: private sub form_load ()
Enablehook
End subprivate sub form_unload (cancel as integer)
Freehook
End sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.