Discuss Hook Functions

Source: Internet
Author: User
Document directory
  • Step-by-Step example
Recently, I went to an interview for a part-time job and was asked what is a callback function. I have heard of it before. I have also seen the C ++ version of BBS discussing it, but I don't have a deep understanding. Let's just take a look at it! I can't think of this Part-time interview. Let's take a look! Later, I found that the callback function that is most used in the actual project is used to test the hook function.

What is a hook function? Windows hook functions are classified into two types: global functions and thread functions. The global hook function can capture messages of any application, but must be a standard DLL. VB cannot. VB can implement the thread, that is, the message of the current application, which has an impact on the capture of mouse messages.

Setwindowshookex is defined as follows:

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

Idhook is a hook type, for example, wh_keyboard captures keyboard messages, while wh_mouse captures mouse messages. Hmod is used for global hooks. To implement hooks in VB, it must be set to 0. Dwthreadid can be set to app. threadid in thread hook VB. Lpfn is the hook function. In VB, you can use addressof to obtain the address of the hook function. This function varies depending on the hook type. For example, the keyboard hook is:

Public Function keyboardproc (byval ncode as long ,_

Byval wparam as long ,_

Byval lparam as long) as long

If the code is not 0, the hook function must call callnexthookex to pass the message to the following hook. Wparam and lparam are not buttons.

The following is an example of a hook in the previous article of msdn:

SUMMARYThis article between strates how to position a message box on the screen. more ionionyou can create a CBT hook for your application so that it has es restrictions when windows are created and destroyed. if you display a message box with this CBT hook in place, your application will receive a hcbt_activate message when the message box is activated. once you receive this hcbt_activate message, you can position the window with the setwindowpos API function and then release the CBT hook if it is no longer needed.

Step-by-Step example
1. Start a new standard EXE project. form1 is created by default.
2. Add a module to the project and add the following code to the new module:
      Type RECT         Left As Long         Top As Long         Right As Long         Bottom As Long      End Type      Public Declare Function UnhookWindowsHookEx Lib "user32" ( _         ByVal hHook As Long) As Long      Public Declare Function GetWindowLong Lib "user32" Alias _         "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) _         As Long      Public Declare Function GetCurrentThreadId Lib "kernel32" () As Long      Public 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      Public Declare Function SetWindowPos Lib "user32" ( _         ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _         ByVal x As Long, ByVal y As Long, ByVal cx As Long, _         ByVal cy As Long, ByVal wFlags As Long) As Long      Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd _         As Long, lpRect As RECT) As Long      Public Const GWL_HINSTANCE = (-6)      Public Const SWP_NOSIZE = &H1      Public Const SWP_NOZORDER = &H4      Public Const SWP_NOACTIVATE = &H10      Public Const HCBT_ACTIVATE = 5      Public Const WH_CBT = 5      Public hHook As Long      Function WinProc1(ByVal lMsg As Long, ByVal wParam As Long, _         ByVal lParam As Long) As Long         If lMsg = HCBT_ACTIVATE Then            'Show the MsgBox at a fixed location (0,0)            SetWindowPos wParam, 0, 0, 0, 0, 0, _                         SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE            'Release the CBT hook            UnhookWindowsHookEx hHook         End If         WinProc1 = False      End Function      Function WinProc2(ByVal lMsg As Long, ByVal wParam As Long, _         ByVal lParam As Long) As Long      Dim rectForm As RECT, rectMsg As RECT      Dim x As Long, y As Long         'On HCBT_ACTIVATE, show the MsgBox centered over Form1         If lMsg = HCBT_ACTIVATE Then            'Get the coordinates of the form and the message box so that            'you can determine where the center of the form is located            GetWindowRect Form1.hwnd, rectForm            GetWindowRect wParam, rectMsg            x = (rectForm.Left + (rectForm.Right - rectForm.Left) / 2) - _                ((rectMsg.Right - rectMsg.Left) / 2)            y = (rectForm.Top + (rectForm.Bottom - rectForm.Top) / 2) - _                ((rectMsg.Bottom - rectMsg.Top) / 2)            'Position the msgbox            SetWindowPos wParam, 0, x, y, 0, 0, _                         SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE            'Release the CBT hook            UnhookWindowsHookEx hHook         End If         WinProc2 = False      End Function                                    

3. Add two commandbuttons to form1.
4. Add the following code to form1:
      Private Sub Command1_Click()      Dim hInst As Long      Dim Thread As Long         'Set up the CBT hook         hInst = GetWindowLong(Me.hwnd, GWL_HINSTANCE)         Thread = GetCurrentThreadId()         hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc1, hInst, _                                  Thread)         'Display the message box         MsgBox "This message box has been positioned at (0,0)."      End Sub      Private Sub Command2_Click()      Dim hInst As Long      Dim Thread As Long         'Set up the CBT hook         hInst = GetWindowLong(Me.hwnd, GWL_HINSTANCE)         Thread = GetCurrentThreadId()         hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc2, hInst, _                                  Thread)         'Display the message box         MsgBox "This message box is centered over Form1."      End Sub                                    

5. Press the F5 key to run the program. click command1 and the message box appears at the upper-left corner of the screen (0, 0 ). click OK to dismiss the message box. click command2 and the message box appears at the center of form1. Click OK to dismiss the message box.

I have tried these steps. We can see that the two buttons in the above example call the winproc1 and winproc2 hook functions respectively!

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.