How to To:set a Windows Hook in Visual C #. NET

Source: Internet
Author: User
Tags bool requires thread tostring wrapper visual studio
How to To:set a Windows Hook in Visual C #. NET
The information in this article applies to:
Microsoft. NET Framework SDK
Microsoft Visual C #. NET (2002)

In the This TASK
SUMMARY
Set a Mouse Hook
The Global Hook isn't supported in. NET Framework
REFERENCES
SUMMARY
This article describes how to set a hook, and the specific to a thread and to a hooks procedure by using the mouse hook as a n Example. can use hooks to monitor certain types of events. You can associate this events with a specific thread or with the "threads in the same desktop as a calling thread."

Back to the top


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

To set a mouse hooks 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 Temp Lates. In the Name box, type Threadspecificmousehook. Form1 is added to the project by default.
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 hook handle as int.
static int hhook = 0;

Declare mouse Hook constant.
For the other hook types, you can obtain this values from Winuser.h in the 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=callingconvention.stdcall)]
public static extern int SetWindowsHookEx (int idhook, HookProc lpfn,
INTPTR hinstance, int threadId);

Import for UnhookWindowsHookEx.
Call this function to uninstall the hook.
[DllImport ("user32.dll", CharSet=CharSet.Auto,
Callingconvention=callingconvention.stdcall)]
public static extern bool UnhookWindowsHookEx (int idhook);

Import for CallNextHookEx.
Use the ' This function ' to ' pass ' hook information to next hook procedure in chain.
[DllImport ("user32.dll", 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 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";
}
}
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 callback.
MouseHookStruct mymousehookstruct = (mousehookstruct) marshal.ptrtostructure (LParam, typeof (MouseHookStruct));

if (Ncode < 0)
{
Return CallNextHookEx (Hhook, Ncode, WParam, LParam);
}
Else
{
Create A string variable with shows current mouse. Coordinates
String 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 The form.
Tempform.text = strcaption;
Return CallNextHookEx (Hhook, Ncode, WParam, LParam);
}
}
Press F5 to run the project, and then click the button on the form to set the hook. The mouse coordinates appear on the form caption bar when the pointer on the form. Click the button again to remove the hook.
Back to the top
The Global Hook isn't supported in. NET Framework
You are cannot implement global hooks in the Microsoft. NET Framework. To install a global hook, a hooks must have a native Dynamic-link library (DLL) export to inject itself in another process That's requires a valid, consistent function to call into. This requires a DLL export, which the. NET Framework does not support. Managed code has no concept of a consistent value for a function pointer because this function pointers are proxies that are built dynamically.

Back to the top
REFERENCES
For more information about Windows hooks, and the following MSDN documentation:
About Hooks
Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/hooks_9rg3.asp
Back to the top





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.