What can cause Windows to unhook a low level (global) keyboard hook?

Posted by Davy8 on Stack Overflow See other posts from Stack Overflow or by Davy8
Published on 2010-04-16T18:22:16Z Indexed on 2010/04/20 18:23 UTC
Read the original article Hit count: 419

We have some global keyboard hooks installed via SetWindowsHookEx with WH_KEYBOARD_LL that appear to randomly get unhooked by Windows.

We verified that they hook was no longer attached because calling UnhookWindowsHookEx on the handle returns false. (Also verified that it returns true when it was working properly)

There doesn't seem to be a consistent repro, I've heard that they can get unhooked due to timeouts or exceptions getting thrown, but I've tried both just letting it sit on a breakpoint in the handling method for over a minute, as well as just throwing a random exception (C#) and it still appears to work.

In our callback we quickly post to another thread, so that probably isn't the issue. I've read about solutions in Windows 7 for setting the timeout higher in the registry because Windows 7 is more aggressive about the timeouts apparently (we're all running Win7 here, so not sure if this occurs on other OS's) , but that doesn't seem like an ideal solution.

I've considered just having a background thread running to refresh the hook every once in a while, which is hackish, but I don't know of any real negative consequences of doing that, and it seems better than changing a global Windows registry setting.

Any other suggestions or solutions? The delegates are not being GC'd since they're static members, which is one cause that I've read about.

© Stack Overflow or respective owner

Related posts about setwindowshookex

Related posts about c#