Microsoft Knowledge Base Email Alertz

KBAlertz.com: You can use hooks to monitor certain types of events. This article describes how to set a hook that is specific to a thread and to a hook procedure by using the mouse hook as an example.

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 319524 - Last Review: December 3, 2007 - Revision: 3.6

How to set a hook in Visual Basic .NET

This article was previously published under Q319524
For a Microsoft Visual C# version of this article, see 318804  (http://kbalertz.com/Feedback.aspx?kbNumber=318804/ ) .

On This Page

SUMMARY

This article describes how to set a hook that is specific to a thread and to a hook procedure. This article uses the mouse hook as an example.

You can use hooks to monitor certain types of events. You can associate these events with a specific thread or with all threads that are in the same desktop as a calling thread.

MORE INFORMATION

Set a mouse hook

To set a hook, call the SetWindowsHookEx function from the User32.dll file. This function installs an application-defined hook procedure in the hook chain that is associated with the hook.

To set a mouse hook and to monitor the mouse events, follow these steps:
  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. In the New Project dialog box, click Visual Basic Projects under Project Types. Then, click Windows Application under Templates.
  4. In the Name box, type ThreadSpecificMouseHook. By default, a form that is named Form1 is created.
  5. At the beginning of the Form1.vb file, paste the following code.
    Imports System.Runtime.InteropServices
    Public Delegate Function CallBack( _
        ByVal nCode As Integer, _
        ByVal wParam As IntPtr, _
        ByVal lParam As IntPtr) As Integer
  6. In the Form1 class, paste the following code.
    'Declare the mouse hook constant.
    'For other hook types, obtain these values from Winuser.h in Microsoft SDK.
        Dim WH_MOUSE As Integer = 7
        Shared hHook As Integer = 0
    
        'Keep the reference so that the delegate is not garbage collected.
        Private hookproc As CallBack
    
        'Import for the SetWindowsHookEx function.
        <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
         Public Overloads Shared Function SetWindowsHookEx _
              (ByVal idHook As Integer, ByVal HookProc As CallBack, _
               ByVal hInstance As IntPtr, ByVal wParam As Integer) As Integer
        End Function
    
        'Import for the CallNextHookEx function.
        <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
         Public Overloads Shared Function CallNextHookEx _
              (ByVal idHook As Integer, ByVal nCode As Integer, _
               ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
        End Function
        'Import for the UnhookWindowsHookEx function.
        <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
             Public Overloads Shared Function UnhookWindowsHookEx _
                  (ByVal idHook As Integer) As Boolean
        End Function
    
        'Point structure declaration.
        <StructLayout(LayoutKind.Sequential)> Public Structure Point
            Public x As Integer
            Public y As Integer
        End Structure
    
        'MouseHookStruct structure declaration.
        <StructLayout(LayoutKind.Sequential)> Public Structure MouseHookStruct
            Public pt As Point
            Public hwnd As Integer
            Public wHitTestCode As Integer
            Public dwExtraInfo As Integer
        End Structure
  7. Add a button to the form. Then, paste the following code in the button1_click procedure.
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            If hHook.Equals(0) Then
                hookproc = AddressOf MouseHookProc
                hHook = SetWindowsHookEx(WH_MOUSE, _
                                         hookproc, _
                                         IntPtr.Zero, _
    AppDomain.CurrentDomain.GetCurrentThreadId())
                If hHook.Equals(0) Then
                    MsgBox("SetWindowsHookEx Failed")
                    Return
                Else
                    Button1.Text = "UnHook Windows Hook"
                End If
            Else
                Dim ret As Boolean = UnhookWindowsHookEx(hHook)
    
                If ret.Equals(False) Then
                    MsgBox("UnhookWindowsHookEx Failed")
                    Return
                Else
                    hHook = 0
                    Button1.Text = "Set Windows Hook"
                    Me.Text = "Mouse Hook"
                End If
            End If
    
        End Sub
  8. In the Form1 class, paste the following code for the MouseHookProc function.
        Public Shared Function MouseHookProc( _
        ByVal nCode As Integer, _
        ByVal wParam As IntPtr, _
        ByVal lParam As IntPtr) As Integer
    Dim MyMouseHookStruct As New MouseHookStruct()
    
            Dim ret As Integer
    
            If (nCode < 0) Then
                Return CallNextHookEx(hHook, nCode, wParam, lParam)
            End If
    
            MyMouseHookStruct = CType(Marshal.PtrToStructure(lParam, MyMouseHookStruct.GetType()), MouseHookStruct)
    
            Dim tempForm As Form
            tempForm = Form.ActiveForm()
    
            Dim strCaption As String
            strCaption = "x = " & MyMouseHookStruct.pt.x & " y = " & MyMouseHookStruct.pt.y
    
            tempForm.Text = strCaption
            Return CallNextHookEx(hHook, nCode, wParam, lParam)
    
        End Function
  9. Press F5 to run the project. Click the button on the form to set the hook. The pointer coordinates appear on the form caption bar when the pointer moves on the form.
  10. Click the button again to remove the hook.

Global hooks are not supported in the .NET Framework

You cannot implement global hooks in Microsoft .NET Framework. To install a global hook, a hook must have a native DLL export to insert itself in another process that requires a valid, consistent function to call into. This behavior requires a DLL export. The .NET Framework does not support DLL exports. Managed code has no concept of a consistent value for a function pointer because these function pointers are proxies that are built dynamically.

REFERENCES

For more information about hooks, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn.microsoft.com/en-us/library/ms644959.aspx (http://msdn.microsoft.com/en-us/library/ms644959.aspx)

APPLIES TO
  • Microsoft .NET Framework 2.0
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbhowtomaster KB319524
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Anonymous User Report As Irrelevant  
Written: 6/2/2007 10:36 PM
Where to import Point types from? Thank you!

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please