我试图捕捉创建/销毁另一个应用程序的指定窗口的事件。为此我设置了WM_SHELLHOOK
.
这是我的 WPF 应用程序中的简化代码:
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 (ERROR_HOOK_NEEDS_HMOD) 不能在没有模块句柄的情况下设置非本地挂钩。
我看过另一个相关的问题。当我为鼠标、键盘等设置挂钩时 - 一切正常。
请指出我如何解决这个错误。谢谢。