Microsoft KB Archive/318804

= How to set a Windows hook in Visual C# .NET =

Article ID: 318804

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft .NET Framework 2.0
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0
 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q318804





For a Microsoft Visual Basic version of this article, see 319524.



Introduction
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. You can use hooks to monitor certain types of events. You can associate these events with a specific thread or with all the threads 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 C# Projects under Project Types, and then click Windows Application under Templates. In the Name box, type ThreadSpecificMouseHook. By default, a form that is named Form1 is created.  Add the following line of code in the Form1.cs file after the other using statements. using System.Runtime.InteropServices;   Add following code in the Form1 class. public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);

//Declare the hook handle as an int. static int hHook = 0;

//Declare the mouse hook constant. //For other hook types, you can obtain these values from Winuser.h in the Microsoft SDK. public const int WH_MOUSE = 7; private System.Windows.Forms.Button button1;

//Declare MouseHookProcedure as a HookProc type. HookProc MouseHookProcedure;

//Declare the wrapper managed POINT class. [StructLayout(LayoutKind.Sequential)] public class POINT {   public int x;    public int y; }

//Declare the wrapper managed MouseHookStruct class. [StructLayout(LayoutKind.Sequential)] public class MouseHookStruct {   public POINT pt; public int hwnd; public int wHitTestCode; public int dwExtraInfo; }

//This is the Import for the SetWindowsHookEx function. //Use this function to install a thread-specific hook. [DllImport(&quot;user32.dll&quot;,CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);

//This is the Import for the UnhookWindowsHookEx function. //Call this function to uninstall the hook. [DllImport(&quot;user32.dll&quot;,CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] public static extern bool UnhookWindowsHookEx(int idHook); //This is the Import for the CallNextHookEx function. //Use this function to pass the hook information to the next hook procedure in chain. [DllImport(&quot;user32.dll&quot;,CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);   Add a Button control to the form, and then add the following code in the Button1_click procedure. private void button1_Click(object sender, System.EventArgs e) { if(hHook == 0) {           // Create an instance of HookProc. MouseHookProcedure = new HookProc(Form1.MouseHookProc); hHook = SetWindowsHookEx(WH_MOUSE,                    MouseHookProcedure,                     (IntPtr)0,                    AppDomain.GetCurrentThreadId); //If the SetWindowsHookEx function fails. if(hHook == 0 ) {           MessageBox.Show(&quot;SetWindowsHookEx Failed&quot;); return; }       button1.Text = &quot;UnHook Windows Hook&quot;; }   else {       bool ret = UnhookWindowsHookEx(hHook); //If the UnhookWindowsHookEx function fails. if(ret == false ) {           MessageBox.Show(&quot;UnhookWindowsHookEx Failed&quot;); return; }       hHook = 0; button1.Text = &quot;Set Windows Hook&quot;; this.Text = &quot;Mouse Hook&quot;; } } </li>  Add the following code for the MouseHookProc function in the Form1 class. public static int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam) {   //Marshall the data from the callback. MouseHookStruct MyMouseHookStruct = (MouseHookStruct) Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));

if (nCode < 0) {       return CallNextHookEx(hHook, nCode, wParam, lParam); }   else {       //Create a string variable that shows the current mouse coordinates. String strCaption = &quot;x = &quot; + MyMouseHookStruct.pt.x.ToString(&quot;d&quot;) + &quot; y = &quot; + MyMouseHookStruct.pt.y.ToString(&quot;d&quot;); //You must get the active form because it is a static function. Form tempForm = Form.ActiveForm; //Set the caption of the form. tempForm.Text = strCaption; return CallNextHookEx(hHook, nCode, wParam, lParam); } } </li> Press F5 to run the project. Click the button on the form to set the hook. The mouse coordinates appear on the form caption bar when the pointer moves on the form. Click the button again to remove the hook.</li></ol>

Global hooks are not supported in the .NET Framework
Except for the WH_KEYBOARD_LL low-level hook and the WH_MOUSE_LL low-level hook, you cannot implement global hooks in the Microsoft .NET Framework. To install a global hook, a hook must have a native DLL export to inject 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.

Low-level hook procedures are called on the thread that installed the hook. Low-level hooks do not require that the hook procedure be implemented in a DLL.

<div class="references_section">