How to: Set the window hook in Visual C # .NET
This task content
•Summary
• Set the mouse hook • Global hooks are not supported in the .NET framework • Reference
This page
Summary reference
summary
This article uses a mouse hook as an example, describes how to set a hook specific to a certain thread and a hook specific to a hook process. You can use hook to monitor specific types of events. You can associate these events as a call thread with a specific thread or all threads in the same desktop.
Back to top
Set the mouse hook To set the hook, call from the user32.dll file
Setwindowshookex function. This function can install a hook process defined by an application to the hook chain associated with this hook.
To set a mouse hook and monitor your mouse event, 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 the Visual C # item under the project type, then click Windows Applications under Templates. Type ThreadspecificMousehook in the Name box. The Form1 is added to the project by default. 4. Add the following code line to other USING statements in the Form1.cs file. Using system.Runtime.InteropServices;
5. Add the following code to the Form1 class: Public Delegate Int hookProc (int Ncode, INTPTR WPARAM, INTPTR LPARAM);
// Declare Hook Handle As Int.
Static int hHHOOK = 0;
// Declare mouse hook constant.
// for other hook types, you can obtain these values from winuser.h in Microsoft SDK.
Public const INT wh_mouse = 7;
Private system.windows.Forms.Button button1;
// Declare MousehookProcedure As HookProc Type.
HookProc MousehookProcedure;
// Declare Wrapper Managed Point Class.
[Structlayout (layoutkind.sequential)]
Public Class Point
{
Public int x;
Public int y;
}
// Declare Wrapper Managed MousehookStruct Class.
[Structlayout (layoutkind.sequential)]
Public Class MousehookStruct
{
PUBLIC POINT PT;
Public int hwnd;
Public int whittestcode;
Public int dwextrainfo;
}
// Import for setWindowshookex function.
// Use this function to install thread-specific hook.
[DLLIMPORT ("User32.dll", charset = charset.auto,
Callingconvention = calingconvention.stdcall)]
Public Static Extern Int setWindowshookex (int IDHOK, HOOKPROC LPFN,
INTPTR HINSTANCE, INT THREADID;
// Import for unhookwindowshookex.
// Call this function to uninstall the hook. [DLLIMPORT ("User32.dll", charset = charset.auto,
Callingconvention = calingconvention.stdcall)]
Public Static Extern Bool UnHookWindowsHookex (Int IDHOOK);
// Import for callnexthookex.
// use this function to pass the hook information to next hook procedure in chain.
[DLLIMPORT ("User32.dll", charset = charset.auto,
Callingconvention = calingconvention.stdcall)]
Public Static Extern Int CallNexthookex (int IDHOK, INT NCODE,
INTPTR WPARAM, INTPTR LPARAM;
6. Add the Button control to the form, then add the following code to the Button1_Click Process: Private Void Button1_Click (Object Sender, System.EventArgs E)
{
IF (hHOOK == 0)
{
// Create an instance of hookproc.
MousehookProcedure = New HookProc (Form1.MouseHookProc);
HHOOK = SETWINDOWSHOKEX (Wh_Mouse,
MousehookProcedure,
(INTPTR) 0,
Appdomain.getCurrentThreadId ());
// if setwindowshookex fails.
IF (hHOOK == 0)
{
MessageBox.show ("SetWindowsHookex Failed";
Return;
}
Button1.text = "unhook windows hook";
}
Else
{
Bool Ret = UnHookWindowsHookex (HHOOK);
// if unhookwindowshookex fails.
IF (RET == FALSE)
{
MessageBox.Show ("UnhookWindowsHookex Failed";
Return;
}
hHOOK = 0;
Button1.text = "SET Windows Hook";
this.Text = "Mouse Hook";
}
}
7. Add the following code to the MouseHookProc function in the Form1 class: Public Static Int MousehookProc (int Ncode, INTPTR WPARAM, INTPTR LPARAM)
{
// Marshall The Data from Callback.
Mousehooking = (MousehookStruct) Marshal.PTRTOStructure (lParam, Typeof (MousehookStruct);
IF (ncode <0)
{
Return CallNexthookex (HHOOK, NCODE, WPARAM, LPARAM);
}
Else
{
// Create a string variable with shows current mouse. Coordinatesstring strcaption = "x ="
MyMouseHookStruct.pt.x.tostring ("d")
"y ="
MyMouseHookStruct.pt.y.tostring ("d");
// NEED TO 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);
}
}
8. Press the F5 key to run this project, and then click the button on the form to set this hook. When the pointer moves on the form, the mouse coordinate will appear on the form header bar. Click this button again to delete this hook.
Back to top
The global hooks do not support global hooks in the .NET framework you cannot implement global hooks in the Microsoft .NET framework. To install global hooks, the hook must have a native dynamic link library (DLL) export to insert it itself into another process that needs to be tuned into a valid and consistent function. This requires a DLL export, and the .NET framework does not support this. The hosted code does not allow function pointers with a unified value, because these functions are dynamically built a proxy.
Back to top
reference
For more information on window hooks, see the following MSDN documentation:
About Hooks
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/hook_9rg3.asp