.NET框架下可以用C#实现全局钩子吗?
http://support.microsoft.com/kb/318804
在微软的帮助网站上提到
"在 .NET 框架中不支持全局挂钩
您无法在 Microsoft .NET 框架中实现全局挂钩。若要安装全局挂钩,挂钩必须有一个本机动态链接库 (DLL) 导出以便将其本身插入到另一个需要调入一个有效而且一致的函数的进程中。这需要一个 DLL 导出,而 .NET 框架不支持这一点。托管代码没有让函数指针具有统一的值这一概念,因为这些函数是动态构建的代理。 "
是不是这就意味着.NET框架中不能实现全局钩子吗?如果要实现,有别的途径吗?
我想实现的一个功能是屏蔽所有的按键,部分代码如下:
[DllImport( "user32.dll ", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
[DllImport( "user32.dll ", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);
[DllImport( "user32.dll ", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
[DllImport( "kernel32.dll ")]
static extern int GetCurrentThreadId();
public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
static int hKeyboardHook = 0;
HookProc KeyboardHookProcedure;
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
{
if (nCode > = 0)
{
return 1;
}
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
}
private void KBUnable_Click(object sender, EventArgs e)
{