UnhookWindowsHookEx 产生 ERROR_INVALID_HOOK_HANDLE?
UnhookWindowsHookEx produces ERROR_INVALID_HOOK_HANDLE?
我正在尝试在 WPF 中实现一个仅在右键单击屏幕顶部时才执行某些操作的后台应用程序,因此我尝试挂钩 WH_MOUSE_LL
,效果很好。问题是退出应用程序时取消挂钩回调,这在 C 中工作得很好,但在 C# 中给了我 ERROR_INVALID_HOOK_HANDLE
,即使传递给 UnhookWindowsHookEx
的钩子句柄与我从 SetWindowsHookEx
(以下示例)。
编辑: 我刚刚创建了一个简单的 C# 控制台应用程序,注册了相同的钩子并立即注销它,它也工作正常,所以我认为 WPF 可能会导致问题。
这是无法运行的 C# 代码,问题出在析构函数中:
using System;
using System.Runtime.InteropServices;
public class LowLevelMouseHook {
#region Win32 API
private const int WH_MOUSE_LL = 14;
private const ulong WM_RBUTTONDOWN = 0x0204;
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate long LowLevelMouseProc (int nCode, UIntPtr wParam, IntPtr lParam);
private struct POINT {
long x;
long y;
}
private struct MSLLHOOKSTRUCT {
POINT pt;
int mouseData;
int flags;
int time;
UIntPtr dwExtraInfo;
}
[DllImport("User32.dll", EntryPoint = "CallNextHookEx", SetLastError = true)]
private static extern long CallNextHookEx (IntPtr hHook, int nCode, UIntPtr wParam, IntPtr lParam);
[DllImport("Kernel32.dll", EntryPoint = "GetLastError", SetLastError = true)]
private static extern uint GetLastError ();
[DllImport("Kernel32.dll", EntryPoint = "LoadLibraryW", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern IntPtr LoadLibrary (string lpFileName);
[DllImport("User32.dll", EntryPoint = "SetWindowsHookExW", SetLastError = true)]
private static extern IntPtr SetWindowsHookEx (int idHook, Delegate lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("User32.dll", EntryPoint = "UnhookWindowsHookEx", SetLastError = true)]
private static extern byte UnhookWindowsHookEx (IntPtr hHook);
#endregion
// There shall only be one instance of this class
private static LowLevelMouseHook instance = null;
private IntPtr hHook;
private LowLevelMouseProc hProc; // If I don't store the delegate, I get some CallbackOnCollectedDelegate exception
private LowLevelMouseHook () {
this.hProc = new LowLevelMouseProc(MouseProc);
this.hHook = SetWindowsHookEx(WH_MOUSE_LL, this.hProc, LoadLibrary("User32.dll"), 0);
}
~LowLevelMouseHook () {
//
// PROBLEM:
// UnhookWindowsHookEx always returns 0 and GetLastError returns ERROR_INVALID_HOOK_HANDLE (1404)
// even though this.hHook is still the same value SetWindowsHookEx returned.
//
if (UnhookWindowsHookEx(this.hHook) == 0)
System.Windows.Forms.MessageBox.Show($"Error {GetLastError()} unhooking WH_MOUSE_LL", "UnhookWindowsHookEx Error");
LowLevelMouseHook.instance = null;
}
// Create new LowLevelMouseHook instance.
// This is called during the Startup-Event of Application.
public static void Init () {
LowLevelMouseHook.instance = new LowLevelMouseHook();
}
private static long MouseProc (int nCode, UIntPtr wParam, IntPtr lParam) {
if ((ulong)wParam == WM_RBUTTONDOWN) {
; // Things to do; that works fine
}
return CallNextHookEx(LowLevelMouseHook.instance.hHook, nCode, wParam, lParam);
}
}
以下是运行良好的 C 代码:
#include <stdio.h>
#include <Windows.h>
FILE *file;
HHOOK hHook;
LRESULT CALLBACK LowLevelMouseProc_Test (int nCode, WPARAM wParam, LPARAM lParam) {
PMSLLHOOKSTRUCT pHookStruct = (PMSLLHOOKSTRUCT)lParam;
fprintf(file, "MouseProc_Test(nCode = %i, wParam = %lu, lParam->pt = (%i, %i))\n", nCode, wParam, pHookStruct->pt.x, pHookStruct->pt.y);
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
VOID CALLBACK TimerProc_Test (HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) {
PostQuitMessage(0);
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int nShow) {
fopen_s(&file, "wm_mouse_ll.txt", "w");
hHook = SetWindowsHookExW(WH_MOUSE_LL, LowLevelMouseProc_Test, LoadLibrary("User32.dll"), 0);
if (hHook != 0) {
fprintf(file, "SetWindowsHookExW Success (hHook = 0x%p)\n", hHook);
SetTimer(NULL, 0, 5000, TimerProc_Test);
MSG msg = {0};
while (GetMessageW(&msg, NULL, 0, 0) != 0) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
if (UnhookWindowsHookEx(hHook) != 0)
fputs("UnhookWindowsHookEx Success\n", file);
else
fprintf(file, "UnhookWindowsHookEx Error %u\n", GetLastError());
} else {
fprintf(file, "SetWindowsHookExW Error %u\n", GetLastError());
}
fclose(file);
return 0;
}
好的,我只是稍微重构了 LowLevelMouseHook
并添加了一个自定义 Main()
方法,如下所示。 UnhookWindowsHookEx
现在成功了。
public static class EntryPoint {
public static void Main () {
LowLevelMouseHook.Begin() // Here SetWindowsHookEx is called
App.Main() // Original entrypoint
LowLevelMouseHook.End() // Here UnhookWindowsHookEx is called
}
}
我正在尝试在 WPF 中实现一个仅在右键单击屏幕顶部时才执行某些操作的后台应用程序,因此我尝试挂钩 WH_MOUSE_LL
,效果很好。问题是退出应用程序时取消挂钩回调,这在 C 中工作得很好,但在 C# 中给了我 ERROR_INVALID_HOOK_HANDLE
,即使传递给 UnhookWindowsHookEx
的钩子句柄与我从 SetWindowsHookEx
(以下示例)。
编辑: 我刚刚创建了一个简单的 C# 控制台应用程序,注册了相同的钩子并立即注销它,它也工作正常,所以我认为 WPF 可能会导致问题。
这是无法运行的 C# 代码,问题出在析构函数中:
using System;
using System.Runtime.InteropServices;
public class LowLevelMouseHook {
#region Win32 API
private const int WH_MOUSE_LL = 14;
private const ulong WM_RBUTTONDOWN = 0x0204;
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate long LowLevelMouseProc (int nCode, UIntPtr wParam, IntPtr lParam);
private struct POINT {
long x;
long y;
}
private struct MSLLHOOKSTRUCT {
POINT pt;
int mouseData;
int flags;
int time;
UIntPtr dwExtraInfo;
}
[DllImport("User32.dll", EntryPoint = "CallNextHookEx", SetLastError = true)]
private static extern long CallNextHookEx (IntPtr hHook, int nCode, UIntPtr wParam, IntPtr lParam);
[DllImport("Kernel32.dll", EntryPoint = "GetLastError", SetLastError = true)]
private static extern uint GetLastError ();
[DllImport("Kernel32.dll", EntryPoint = "LoadLibraryW", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern IntPtr LoadLibrary (string lpFileName);
[DllImport("User32.dll", EntryPoint = "SetWindowsHookExW", SetLastError = true)]
private static extern IntPtr SetWindowsHookEx (int idHook, Delegate lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("User32.dll", EntryPoint = "UnhookWindowsHookEx", SetLastError = true)]
private static extern byte UnhookWindowsHookEx (IntPtr hHook);
#endregion
// There shall only be one instance of this class
private static LowLevelMouseHook instance = null;
private IntPtr hHook;
private LowLevelMouseProc hProc; // If I don't store the delegate, I get some CallbackOnCollectedDelegate exception
private LowLevelMouseHook () {
this.hProc = new LowLevelMouseProc(MouseProc);
this.hHook = SetWindowsHookEx(WH_MOUSE_LL, this.hProc, LoadLibrary("User32.dll"), 0);
}
~LowLevelMouseHook () {
//
// PROBLEM:
// UnhookWindowsHookEx always returns 0 and GetLastError returns ERROR_INVALID_HOOK_HANDLE (1404)
// even though this.hHook is still the same value SetWindowsHookEx returned.
//
if (UnhookWindowsHookEx(this.hHook) == 0)
System.Windows.Forms.MessageBox.Show($"Error {GetLastError()} unhooking WH_MOUSE_LL", "UnhookWindowsHookEx Error");
LowLevelMouseHook.instance = null;
}
// Create new LowLevelMouseHook instance.
// This is called during the Startup-Event of Application.
public static void Init () {
LowLevelMouseHook.instance = new LowLevelMouseHook();
}
private static long MouseProc (int nCode, UIntPtr wParam, IntPtr lParam) {
if ((ulong)wParam == WM_RBUTTONDOWN) {
; // Things to do; that works fine
}
return CallNextHookEx(LowLevelMouseHook.instance.hHook, nCode, wParam, lParam);
}
}
以下是运行良好的 C 代码:
#include <stdio.h>
#include <Windows.h>
FILE *file;
HHOOK hHook;
LRESULT CALLBACK LowLevelMouseProc_Test (int nCode, WPARAM wParam, LPARAM lParam) {
PMSLLHOOKSTRUCT pHookStruct = (PMSLLHOOKSTRUCT)lParam;
fprintf(file, "MouseProc_Test(nCode = %i, wParam = %lu, lParam->pt = (%i, %i))\n", nCode, wParam, pHookStruct->pt.x, pHookStruct->pt.y);
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
VOID CALLBACK TimerProc_Test (HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) {
PostQuitMessage(0);
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int nShow) {
fopen_s(&file, "wm_mouse_ll.txt", "w");
hHook = SetWindowsHookExW(WH_MOUSE_LL, LowLevelMouseProc_Test, LoadLibrary("User32.dll"), 0);
if (hHook != 0) {
fprintf(file, "SetWindowsHookExW Success (hHook = 0x%p)\n", hHook);
SetTimer(NULL, 0, 5000, TimerProc_Test);
MSG msg = {0};
while (GetMessageW(&msg, NULL, 0, 0) != 0) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
if (UnhookWindowsHookEx(hHook) != 0)
fputs("UnhookWindowsHookEx Success\n", file);
else
fprintf(file, "UnhookWindowsHookEx Error %u\n", GetLastError());
} else {
fprintf(file, "SetWindowsHookExW Error %u\n", GetLastError());
}
fclose(file);
return 0;
}
好的,我只是稍微重构了 LowLevelMouseHook
并添加了一个自定义 Main()
方法,如下所示。 UnhookWindowsHookEx
现在成功了。
public static class EntryPoint {
public static void Main () {
LowLevelMouseHook.Begin() // Here SetWindowsHookEx is called
App.Main() // Original entrypoint
LowLevelMouseHook.End() // Here UnhookWindowsHookEx is called
}
}