Retired Microsoft Blog disclaimer

This directory is a mirror of retired "Decrypt My World" MSDN blog and is provided as is. All posting authorship and copyrights belong to respective authors.
Original URL: https://blogs.msdn.microsoft.com/alejacma/2010/10/14/global-hooks-getting-lost-on-windows-7/
Post name: Global hooks getting lost on Windows 7
Original author: Alejandro Campos Magencio
Posting date: 2010-10-14T00:23:23+00:00


Hi all,

Some time ago a customer of mine reported the following issue withSetWindowsHookExAPI:

Their application had global hooks to monitor for both keyboard and mouse input. On Windows 7, and under high CPU usage, those hooks were getting lost. They tried several things to solve the issue, without success: adjust registry setting "Control Panel\Desktop\LowLevelKeyboardProc" to 10000 (10sec), restart the hooks every X minutes (withUnhookWindowsHookExandSetWindowsHookEx), etc.At the end, they had to restart the app to reactivate the hooks.

After some research I found out the following:

On Windows 7 we have to make sure that the callback function of the hook can return in less than LowLevelHooksTimeout, which is 300 ms. And weallow for the application to be timed out 10 times when processing the hook callback message. If it times out an 11th time, Windows will unhook the application from the hook chain. This is a by design feature and it was added in Win7 RTM.

My recommendation is that low level hooks should be avoided whenever possible. If you are monitoring keystrokes (and not trying to block them), you can get the keyboard input via Raw Input. This is lighter weight than hooks, won’t affect other apps’ responsiveness, and won’t be turned off if the app isn’t responsive.

My customer got rid of the hooks and used Raw Input instead with success.I hope this helps you too.

Regards,

Alex (Alejandro Campos Magencio)


Share this article:

Comments:

Comments are closed.