Microsoft KB Archive/319524

= How to set a hook in Visual Basic .NET =

Article ID: 319524

Article Last Modified on 12/3/2007

-

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

-



This article was previously published under Q319524





For a Microsoft Visual C# version of this article, see 318804.



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.



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:  Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. In the New Project dialog box, click Visual Basic Projects under Project Types. Then, click Windows Application under Templates. In the Name box, type ThreadSpecificMouseHook. By default, a form that is named Form1 is created.  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   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(&quot;User32.dll&quot;, 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(&quot;User32.dll&quot;, 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(&quot;User32.dll&quot;, 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 </li>  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(&quot;SetWindowsHookEx Failed&quot;) Return Else Button1.Text = &quot;UnHook Windows Hook&quot; End If       Else Dim ret As Boolean = UnhookWindowsHookEx(hHook)

If ret.Equals(False) Then MsgBox(&quot;UnhookWindowsHookEx Failed&quot;) Return Else hHook = 0 Button1.Text = &quot;Set Windows Hook&quot; Me.Text = &quot;Mouse Hook&quot; End If       End If

End Sub </li>  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 = &quot;x = &quot; & MyMouseHookStruct.pt.x & &quot; y = &quot; & MyMouseHookStruct.pt.y

tempForm.Text = strCaption Return CallNextHookEx(hHook, nCode, wParam, lParam)

End Function </li> 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.</li> Click the button again to remove the hook.</li></ol>

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.

<div class="references_section">