最近在项目用到低级键盘钩子。发现一个很奇怪的事情,在开发环境和测试环境下都正常运行的键盘钩子,

到了现场环境,总是偶发性出现 键盘钩子不能用了,而且退出时产生1404 错误。

 

后经过阅读MSDN 的Remark

 

An application installs the hook procedure by specifying the WH_KEYBOARD_LL hook type and a pointer to the hook procedure in a call to the SetWindowsHookEx function.

This hook is called in the context of the thread that installed it. The call is made by sending a message to the thread that installed the hook. Therefore, the thread that installed the hook must have a message loop.

The hook procedure should process a message in less time than the data entry specified in the LowLevelHooksTimeout value in the following registry key:

HKEY_CURRENT_USER\Control Panel\Desktop

The value is in milliseconds. If the hook procedure does not return during this interval, the system will pass the message to the next hook.

Note that debug hooks cannot track this type of hook.

 

发现是现场的机器没有 Lowlevelhookstimeout 这个注册表项目,增加,修改值为10000,重启,解决。

 

这个地方的问题说明,钩子的处理一定要快。其次超时的值应该要记得设置。最好是程序中进行设置一下。

 

posted on 2016-01-14 21:13  zooz  阅读(1561)  评论(0编辑  收藏  举报