VB pointer manipulation and message hooks

Source: Internet
Author: User

Second, VB How to use the pointerTo understand how VB uses pointers, you have to figure out two things, first, how to get the pointer to an array, and second, how to take the array that the pointer points to. A, before explaining these two questions, we need to understand a few questions: 1. The difference between an array of VB and an array of C + +It is possible, we now think that the VB array and C + + arrays do not have any difference, is an in-memory address, it is not.    C + + is really an array, really is an address, and, when your pointer access beyond the scope of the array, and no one to talk to you, but it is easy to cause the system to crash. And the VB array is actually a struct, in this structure contains a description of the array of information, its structure is similar to the following: Private Type vb array array dimension array size
The real array
End Type 2. CopyMemory system functionThis function is a bit like the Arraycopy function in Java, which is to copy the two-segment memory space. Its declaration is this: public Declare Sub copymemory Lib "kernel32" Alias "RtlMoveMemory" (Destination as any, Source as any, ByVal Leng th as Long) function prototype VOID CopyMemory (
PVOID Destination,
CONST VOID *source,
DWORD Length
); First parameter: Destination address pointer second parameter: Source address pointer The third parameter: size of the copy note that in CopyMemory's VB declaration, the source to be passed is the any type, that is, a variable of any type. B, how to use the pointer 1. How to get pointers to arraysPlease see the following program: Poutputarray as Long
Outputarray () As Byte ReDim Outputarray (+) as Byte
Poutputarray = VarPtr (Outputarray (0)) Description: We declare a byte array outputarray, with the VarPtr function, the pointer to the array is assigned to the long type variable Poutputarray. 2. How to take the array that the pointer points toSee the following procedure: CopyMemory ByVal Poutputarray, ByVal pData, UBound (outputarray) Description: PData is a pointer to a memory block, through which we get the pData point The data in the memory area to the Poutputarray array. Third, advanced applications: Get a pointer to a functionSometimes, a callback function is required in a system function library call, and when the callback function is passed as a parameter, it is not possible to pass in the callback function name, but instead to pass the address of the callback function, we need to get the address of the callback function. Here I only give examples of code, interested friends can go to study. Public Sub Registerwinproc (ByVal hwnd as Long) ' incoming HWND is the handle to this window
' GetWindowLong get information from the structure of the specified window
Prevwndproc = GetWindowLong (hwnd, GWL_WNDPROC)
' SetWindowLong sets the information for the specified window in the window structure
SetWindowLong hwnd, GWL_WNDPROC, AddressOf WNDPROC
Desthwnd = hwnd
End Sub

VB has always been considered as a language that can be completed in a simple way by the Windows interface application. The programming of Windows system seems to be the first thought is definitely not VB, and most programmers think of must be VC. Today, we introduce a method of intercepting Windows global messages with VB. I. Introduction of Windows global MessagingI don't like to introduce a simple thing in a very obscure language, so I want to explain this concept in plain language in this article.    What is Windows global messaging?    For example, where the mouse moves in a custom window, we can get it through the relevant method in the form, however, if the mouse does not move in its own form, then how to get its position?    For example: The keystroke of the keyboard, in other places hit the keyboard, and did not hit the keyboard in their own form, how to get the keys of the specific key value? To the microcontroller know that the operation of the mouse and keyboard is to use the "interrupt" trigger event to complete, then when the system "interrupted" when the message will be issued to the operating system, and these messages are Windows global messages. second, Hook introductionHooks are translated by English hooks, which are the programs that are intercepted before the Windows global message is delivered to the operating system, processed and then passed to the operating system or other hooks.    Think about it, what does the hook sound like?    Yes, the hacker program, Trojan program, all through the method of the hook to obtain the corresponding operation of the mouse keyboard information, but also to obtain a password or other useful information. However, the hook itself is not a virus, but provides us with a programmer to get an operating system action, this method is sometimes very useful, it is very convenient to develop a high-quality program for the operating system. iii. Types of messages1, Wh_callwndproc, and Wh_callwndprocret messages are messages that are sent to a window procedure, the system calls Wh_callwndproc before the message is sent to the receive window procedure, and calls the Wh_ after the window procedure finishes processing the message.     Callwndpro.     2, WH_CBT It is to activate, build, destroy, minimize, maximize, move, change the size of the window events, complete the system instructions, from the system Message Queue Mobile mouse, keyboard events, set input focus events, synchronization system Message Queuing events. 3, Wh_keyboard wm_keydown monitoring Wm_keyup 4, WH_KEYBOARD_LL monitoring input to the thread message queue of the keyboard message 5, Wh_mouse monitor input to the message queue mouse Message 6, WH_MOUSE_LL monitoring mouse messages entered into thread message queues
iv. Types of hooks1) Thread Hook: Monitors event messages for the specified thread.
2) System hook: Monitors event messages for all threads in the system. VB can only set thread hooks, because the system hooks affect all applications in the system, so the hook function must be placed in a separate dynamic link library (DLL) to do. Five, VB set up the method of hook steps Step 1: Hooking upThe hook is the first step to 1, telling the operating system what message I want to listen to, and 2 is telling the operating system what callback function to call after the message is received. Hook method: For example: Private Const wh_mouse_ll as Long = + Hhook as long ... hhook = SetWindowsHookEx (wh_mouse   _ll, AddressOf HookProc, app.hinstance, 0) The first parameter: Tell the operating system, I want to hang a mouse monitor hook. The second argument: tell the operating system that my callback function is hookproc,addressof to get the address of the HookProc function. Step Two: Write 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 (HRT, Pt.x, Pt.y) <> 0 Then
' HookProc = 1 ' cancels the original motion to be completed
' Else
Form1.caption = "Mouse Cursor at" + CStr (pt.x) + "," + CStr (PT.Y)
HookProc = 0 ' to complete the pending motion
' 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 One: format of the callback function public function HookProc (ByVal nCode as Long, ByVal WParam as Long, ByVal lparam as Long) As Long NCode: The handle of the hook
Wparam,lparam: Contains the content of the intercepted message, which is also different from the type of hook and the value of ncode. For example, in the keyboard hooks (KeyBoard), wparam is the key return code.     If it is a mouse event, then contains the mouse's position information and key information.    Note two: At the end of the callback function, you need to add a CallNextHookEx to pass the message to the next hook or operating system. This is also very good understanding, you intercept the message, not to really intercept the message, but just want to do the corresponding processing, and then the message to the next bar. Step Three: Release the hooksAfter the end of the program, you need to remove the hook, otherwise, the system will always listen to the message. Call UnhookWindowsHookEx (Hhook) Six, the complete VB hook procedureModule code: Option Explicit
Private Declare Sub copymemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination as any, ByRef Source as Any, by Val 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, Lpar Am 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 individual value of the mouse private Type POINTAPI
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

VB pointer manipulation and message hooks

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.