<script type="text/javascript">
</script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
The information in this article applies TO:
Microsoft . NET Framework SDK
Microsoft Visual C# . NET (2002)
in THIS TASK
SUMMARY
Set a Mouse Hook
Global Hook Is Not Supported in . NET Framework
REFERENCES
SUMMARY
This article describes HOW TO Set a Hook that is specific TO a thread and TO a Hook procedure by us ing the mouse Hook as an example. You can use Hooks TO moni TOr certa in types of events. You can associate these events with a specific thread or with all of the threads in the same desk TOp as a call ing thread.
back TO the TOp
Set a Mouse Hook
TO Set a Hook, call the Set W indows HookEx function from the User32.dll file. This function installs an application-def ined Hook procedure in the Hook cha in that is associated with the Hook.
TO Set a mouse Hook and TO moni TOr the mouse events, follow these steps:
Start Microsoft Visual Studio . NET.
On the File menu, po int TO New, and then click Project.
in the New Project dialog box, click Visual C# Projects under Project Types, and then click W indows Application under Templates. in the Name box, type ThreadSpecificMouse Hook. Form1 is added TO the project by default.
Add the follow ing l ine of code in the Form1.cs file after the other us ing statements:
us ing System.Runtime. interopServices;
Add follow ing code in the Form1 class:
public delegate int HookProc( int nCode, intPtr wParam, intPtr lParam);
//Declare Hook handle as int.
static int h Hook = 0;
//Declare mouse Hook constant.
//For other Hook types, you can obta in these values from W inuser.h in Microsoft SDK.
public const int WH_MOUSE = 7;
private System. W indows.Forms.But TOn but TOn1;
//Declare Mouse HookProcedure as HookProc type.
HookProc Mouse HookProcedure;
//Declare wrapper managed PO inT class.
[StructLayout(LayoutK ind.Sequential)]
public class PO inT
{
public int x;
public int y;
}
//Declare wrapper managed Mouse HookStruct class.
[StructLayout(LayoutK ind.Sequential)]
public class Mouse HookStruct
{
public PO inT pt;
public int hwnd;
public int wHitTestCode;
public int dwExtra info;
}
//Import for Set W indows HookEx function.
//Use this function TO install thread-specific Hook.
[DllImport("user32.dll",Char Set=Char Set.Au TO,
Call ingConvention=Call ingConvention.StdCall)]
public static extern int Set W indows HookEx( int id Hook, HookProc lpfn,
intPtr h instance, int threadId);
//Import for Un Hook W indows HookEx.
//Call this function TO un install the Hook.
[DllImport("user32.dll",Char Set=Char Set.Au TO,
Call ingConvention=Call ingConvention.StdCall)]
public static extern bool Un Hook W indows HookEx( int id Hook);
//Import for CallNext HookEx.
//Use this function TO pass the Hook information TO next Hook procedure in cha in.
[DllImport("user32.dll",Char Set=Char Set.Au TO,
Call ingConvention=Call ingConvention.StdCall)]
public static extern int CallNext HookEx( int id Hook, int nCode,
intPtr wParam, intPtr lParam);
Add a But TOn control TO the form, and then add the follow ing code in the But TOn1_click procedure:
private void but TOn1_Click(object sender, System.EventArgs e)
{
if(h Hook == 0)
{
// Create an instance of HookProc.
Mouse HookProcedure = new HookProc(Form1.Mouse HookProc);
h Hook = Set W indows HookEx(WH_MOUSE,
Mouse HookProcedure,
( intPtr)0,
AppDoma in.GetCurrentThreadId());
//If Set W indows HookEx fails.
if(h Hook == 0 )
{
MessageBox.S HOW(" Set W indows HookEx Failed");
return;
}
but TOn1.Text = "Un Hook W indows Hook";
}
else
{
bool ret = Un Hook W indows HookEx(h Hook);
//If Un Hook W indows HookEx fails.
if(ret == false )
{
MessageBox.S HOW("Un Hook W indows HookEx Failed");
return;
}
h Hook = 0;
but TOn1.Text = " Set W indows Hook";
this.Text = "Mouse Hook";
}
}
Add the follow ing code for the Mouse HookProc function in the Form1 class:
public static int Mouse HookProc( int nCode, intPtr wParam, intPtr lParam)
{
//Marshall the data from callback.
Mouse HookStruct MyMouse HookStruct = (Mouse HookStruct) Marshal.Ptr TOStructure(lParam, typeof(Mouse HookStruct));
if (nCode < 0)
{
return CallNext HookEx(h Hook, nCode, wParam, lParam);
}
else
{
//Create a str ing variable with s HOWs current mouse. coord inates
Str ing strCaption = "x = " +
MyMouse HookStruct.pt.x. TOStr ing("d") +
" y = " +
MyMouse HookStruct.pt.y. TOStr ing("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 CallNext HookEx(h Hook, nCode, wParam, lParam);
}
}
Press F5 TO run the project, and then click the but TOn on the form TO Set the Hook. The mouse coord inates appear on the form caption bar when the po inter moves on the form. Click the but TOn aga in TO remove the Hook.
back TO the TOp
Global Hook Is Not Supported in . NET Framework
You cannot implement global Hooks in Microsoft . NET Framework. TO install a global Hook, a Hook must have a native dynamic-l ink library (DLL) export TO inject itself in another process that requires a valid, consistent function TO call in TO. This requires a DLL export, which . NET Framework does not support. Managed code has no concept of a consistent value for a function po inter because these function po inters are proxies that are built dynamically.
back TO the TOp
REFERENCES
For more information about W indows Hooks, see the follow ing MSDN documentation:
About Hooks
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/w inui/ Hooks_9rg3.asp
back TO the TOp
ing/wangzhanyouhua/" title="seo,搜索引擎优化">seover="w
indow.status='正文--
HOW
TO:
Set a
W
indows
Hook
in
Visual C# .
NET';return true">
<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript">
</script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
HOW
TO:
Set a
W
indows
Hook
in
Visual C# .
NET
The information in this article applies TO:
Microsoft . NET Framework SDK
Microsoft Visual C# . NET (2002)
in THIS TASK
SUMMARY
Set a Mouse Hook
Global Hook Is Not Supported in . NET Framework
REFERENCES
SUMMARY
This article describes HOW TO Set a Hook that is specific TO a thread and TO a Hook procedure by us ing the mouse Hook as an example. You can use Hooks TO moni TOr certa in types of events. You can associate these events with a specific thread or with all of the threads in the same desk TOp as a call ing thread.
back TO the TOp
Set a Mouse Hook
TO Set a Hook, call the Set W indows HookEx function from the User32.dll file. This function installs an application-def ined Hook procedure in the Hook cha in that is associated with the Hook.
TO Set a mouse Hook and TO moni TOr the mouse events, follow these steps:
Start Microsoft Visual Studio . NET.
On the File menu, po int TO New, and then click Project.
in the New Project dialog box, click Visual C# Projects under Project Types, and then click W indows Application under Templates. in the Name box, type ThreadSpecificMouse Hook. Form1 is added TO the project by default.
Add the follow ing l ine of code in the Form1.cs file after the other us ing statements:
us ing System.Runtime. interopServices;
Add follow ing code in the Form1 class:
public delegate int HookProc( int nCode, intPtr wParam, intPtr lParam);
//Declare Hook handle as int.
static int h Hook = 0;
//Declare mouse Hook constant.
//For other Hook types, you can obta in these values from W inuser.h in Microsoft SDK.
public const int WH_MOUSE = 7;
private System. W indows.Forms.But TOn but TOn1;
//Declare Mouse HookProcedure as HookProc type.
HookProc Mouse HookProcedure;
//Declare wrapper managed PO inT class.
[StructLayout(LayoutK ind.Sequential)]
public class PO inT
{
public int x;
public int y;
}
//Declare wrapper managed Mouse HookStruct class.
[StructLayout(LayoutK ind.Sequential)]
public class Mouse HookStruct
{
public PO inT pt;
public int hwnd;
public int wHitTestCode;
public int dwExtra info;
}
//Import for Set W indows HookEx function.
//Use this function TO install thread-specific Hook.
[DllImport("user32.dll",Char Set=Char Set.Au TO,
Call ingConvention=Call ingConvention.StdCall)]
public static extern int Set W indows HookEx( int id Hook, HookProc lpfn,
intPtr h instance, int threadId);
//Import for Un Hook W indows HookEx.
//Call this function TO un install the Hook.
[DllImport("user32.dll",Char Set=Char Set.Au TO,
Call ingConvention=Call ingConvention.StdCall)]
public static extern bool Un Hook W indows HookEx( int id Hook);
//Import for CallNext HookEx.
//Use this function TO pass the Hook information TO next Hook procedure in cha in.
[DllImport("user32.dll",Char Set=Char Set.Au TO,
Call ingConvention=Call ingConvention.StdCall)]
public static extern int CallNext HookEx( int id Hook, int nCode,
intPtr wParam, intPtr lParam);
Add a But TOn control TO the form, and then add the follow ing code in the But TOn1_click procedure:
private void but TOn1_Click(object sender, System.EventArgs e)
{
if(h Hook == 0)
{
// Create an instance of HookProc.
Mouse HookProcedure = new HookProc(Form1.Mouse HookProc);
h Hook = Set W indows HookEx(WH_MOUSE,
Mouse HookProcedure,
( intPtr)0,
AppDoma in.GetCurrentThreadId());
//If Set W indows HookEx fails.
if(h Hook == 0 )
{
MessageBox.S HOW(" Set W indows HookEx Failed");
return;
}
but TOn1.Text = "Un Hook W indows Hook";
}
else
{
bool ret = Un Hook W indows HookEx(h Hook);
//If Un Hook W indows HookEx fails.
if(ret == false )
{
MessageBox.S HOW("Un Hook W indows HookEx Failed");
return;
}
h Hook = 0;
but TOn1.Text = " Set W indows Hook";
this.Text = "Mouse Hook";
}
}
Add the follow ing code for the Mouse HookProc function in the Form1 class:
public static int Mouse HookProc( int nCode, intPtr wParam, intPtr lParam)
{
//Marshall the data from callback.
Mouse HookStruct MyMouse HookStruct = (Mouse HookStruct) Marshal.Ptr TOStructure(lParam, typeof(Mouse HookStruct));
if (nCode < 0)
{
return CallNext HookEx(h Hook, nCode, wParam, lParam);
}
else
{
//Create a str ing variable with s HOWs current mouse. coord inates
Str ing strCaption = "x = " +
MyMouse HookStruct.pt.x. TOStr ing("d") +
" y = " +
MyMouse HookStruct.pt.y. TOStr ing("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 CallNext HookEx(h Hook, nCode, wParam, lParam);
}
}
Press F5 TO run the project, and then click the but TOn on the form TO Set the Hook. The mouse coord inates appear on the form caption bar when the po inter moves on the form. Click the but TOn aga in TO remove the Hook.
back TO the TOp
Global Hook Is Not Supported in . NET Framework
You cannot implement global Hooks in Microsoft . NET Framework. TO install a global Hook, a Hook must have a native dynamic-l ink library (DLL) export TO inject itself in another process that requires a valid, consistent function TO call in TO. This requires a DLL export, which . NET Framework does not support. Managed code has no concept of a consistent value for a function po inter because these function po inters are proxies that are built dynamically.
back TO the TOp
REFERENCES
For more information about W indows Hooks, see the follow ing MSDN documentation:
About Hooks
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/w inui/ Hooks_9rg3.asp
back TO the TOp
<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript">
</script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>