C# WPF应用程序中的全局shell钩子
我试图捕获创建/销毁另一个应用程序的指定窗口的事件。为此,我设置了C# WPF应用程序中的全局shell钩子,c#,wpf,shell,hook,setwindowshookex,C#,Wpf,Shell,Hook,Setwindowshookex,我试图捕获创建/销毁另一个应用程序的指定窗口的事件。为此,我设置了WM\u SHELLHOOK 以下是我的WPF应用程序中的SIPLIFED代码: public delegate IntPtr ProcDelegate(int hookCode, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", SetLastError = true)] private static extern IntPtr SetWindowsHookEx(
WM\u SHELLHOOK
以下是我的WPF应用程序中的SIPLIFED代码:
public delegate IntPtr ProcDelegate(int hookCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(
int hookId, ProcDelegate handler, IntPtr hInstance, uint threadId);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
private void buttonClick(object sender, RoutedEventArgs e)
{
IntPtr hookHandler;
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
var moduleHandle = GetModuleHandle(curModule.ModuleName);
hookHandler = SetWindowsHookEx(
10 /*WH_SHELL*/, shellHookHandler, moduleHandle, 0);
}
if (hookHandler == IntPtr.Zero)
{
// Get here error 1428 (ERROR_HOOK_NEEDS_HMOD) -
// "Cannot set nonlocal hook without a module handle."
throw new Exception(Marshal.GetLastWin32Error().ToString());
}
}
private IntPtr shellHookHandler(int hookCode, IntPtr wParam, IntPtr lParam)
{
// Some code...
return IntPtr.Zero;
}
问题是SetWindowsHookEx
总是返回0,最后一个错误是
1428(错误\u HOOK\u requires\u HMOD)在没有模块的情况下无法设置非本地钩子
处理
我看了另一个相关的问题。当我为鼠标、键盘等设置挂钩时,一切正常
请告诉我如何修复此错误。谢谢。for hooks说“如果应用程序为不同应用程序的线程安装了一个钩子过程,那么该过程必须在DLL中。”
这是因为您的DLL被加载到另一个应用程序的地址空间中;然后,您需要找到某种机制(例如内存映射文件)将信息传递给主应用程序
然而,与大多数文档(已经提到)相反,键盘和鼠标挂钩在没有DLL的情况下工作。这就是他们为你工作的原因