Error when using SetWindowsHookEx in Windows XP, but not in Windows 7
Asked Answered
R

1

6

I have developed an application that uses a global keyboard/mouse hook. It works perfectly in Windows 7, but not in Windows XP.

When I call SetWindowsHookEx in Windows XP, I get the error code 1428.

int MouseLowLevel   = 14
int code = SetWindowsHookEx(MouseLowLevel,
                 MouseHookProc,
                 IntPtr.Zero,
                 0);

private IntPtr MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam) {}
Rehabilitate answered 9/5, 2012 at 12:47 Comment(0)
M
13

Curious that this code doesn't fail on Win7 but I certainly never tried. But it is correct behavior, looks like they improved it. The argument validation for SetWindowsHookEx() requires a valid non-zero 3rd or 4th argument. The error code is highly descriptive, from WinError.h:

//
// MessageId: ERROR_HOOK_NEEDS_HMOD
//
// MessageText:
//
// Cannot set nonlocal hook without a module handle.
//
#define ERROR_HOOK_NEEDS_HMOD            1428L

Any module handle will do since it doesn't actually get used for low-level hooks, no DLL needs to be injected to make them work. Some care in selecting one is required for .NET 4 since its CLR no longer fakes module handles for pure managed assemblies. A good one to use is the one you get out of pinvoking LoadLibrary("user32.dll") since it is always already loaded. You don't have to call FreeLibrary().

You'll need this declaration to call LoadLibrary:

[DllImport("kernel32", SetLastError=true, CharSet = CharSet.Auto)]
private static extern IntPtr LoadLibrary(string fileName);
Manstopper answered 9/5, 2012 at 12:56 Comment(5)
Thank you so much for the superb response. Now it works on both Windows 7 and XP.Rehabilitate
You have another bug in your code, the return type for SetWindowsHookEx() is IntPtr, not int.Manstopper
oops, I made some errors when I simplified the code. In the real code, I use a class that inherits from SafeHandleZeroOrMinusOneIsInvalid. But I did not want too much detail when I attached the code. But thanks anyway :-)Rehabilitate
I'd have thought GetModuleHandle("kernel32.dll") would have been a more obvious choice.Contravallation
Principle of Least Surprise, SetWindowsHookEx requires a pinvoke declaration that uses user32.dllManstopper

© 2022 - 2024 — McMap. All rights reserved.