低级键盘钩可在Windows 7 x64上的一个上运行,而不能在另一个上运行 [英] Low level Keyboard Hook works on one on Windows 7 x64 and not another

查看:80
本文介绍了低级键盘钩可在Windows 7 x64上的一个上运行,而不能在另一个上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试挂接键盘(而不是键盘记录器)时遇到问题,我试图使Word自动化,然后使用特定的钩子调用dll.

我有一个台式机和一个笔记本电脑(两者具有相同的防病毒+ Windows 7 x64),唯一的区别是,在笔记本电脑中,Windows安装了较新的版本.问题:在笔记本电脑上,一切正常.但是在桌面上却发生了奇怪的事情:该挂钩已安装并且如果以记事本为目标可以很好地工作,但是,如果以Word为目标,尽管已安装了该挂钩,但对外部函数的调用被禁止了!

 LRESULT CALLBACK HookProc(int code, WPARAM wParam, LPARAM lParam)
 {
   if (code<0) {
      return CallNextHookEx(HookHandle,code,wParam,lParam);
   }

   bool callNextHook = true;
   if (callFunction != NULL) {
          // ONLY WITH WORD AND ONLY IN THE DESKTOP callFunction SEENS TO BE NULL!!!
          // this is a pointer to a function in main application
      callFunction(code,wParam,lParam,&callNextHook);
   } else {
      ShowMessage("THE UNKNOW ERROR! THIS MESSAGE IS SHOWED, THEN HOOK IS INSTALLED");
   }

   //Call the next hook in the chain
   if (callNextHook) {
      return CallNextHookEx(HookHandle,code,wParam,lParam);
   }

   return 0;
 }

我已经尝试禁用防病毒,更改用户帐户控制,以管理员身份运行程序...无济于事.是什么导致了这种差异?

解决方案

它可能与注册表中的LowLevelHooksTimeout值有关.

在速度更快的计算机上,他们可以足够快地处理挂钩,并将其设置为默认200毫秒以处理超时.在速度较慢的计算机上,它们的工作时间更困难.

对于我来说,对于涉及钩子的应用程序,我必须将该值从默认值提高到500毫秒(0x1F4),以确保在所有机器上都可靠.

要查看更改此注册表值的效果,必须重新启动计算机.

请参阅此处文档备注中的第四段:

LowLevelKeyboardProc回调函数

挂钩过程应该比数据处理时间更短地处理一条消息 在下面的LowLevelHooksTimeout值中指定的条目 注册表项:

HKEY_CURRENT_USER \控制面板\桌面

值在 毫秒.如果挂接过程超时,系统将通过 消息到下一个钩子.但是,在Windows 7和更高版本上,挂钩是 默默地删除而没有被调用.没有办法 应用程序,以了解是否已删除钩子.

希望有帮助.

I have a problem when trying to hook the keyboard (not a keylogger!) I´m trying to automate Word, then i´m calling dll with a especific hook.

I have a desktop and a notebook (the two have same antivirus + windows 7 x64), the only diference is that in the notebook the windows was installed with a newer version. THE PROBLEM: In the notebook EVERYTHING WORKS FINE. But in the desktop odd things happen: the hook was installed and works well if targeted to Notepad, but, when targeted to Word, though the hook was installed, the call to a external function is supressed!

 LRESULT CALLBACK HookProc(int code, WPARAM wParam, LPARAM lParam)
 {
   if (code<0) {
      return CallNextHookEx(HookHandle,code,wParam,lParam);
   }

   bool callNextHook = true;
   if (callFunction != NULL) {
          // ONLY WITH WORD AND ONLY IN THE DESKTOP callFunction SEENS TO BE NULL!!!
          // this is a pointer to a function in main application
      callFunction(code,wParam,lParam,&callNextHook);
   } else {
      ShowMessage("THE UNKNOW ERROR! THIS MESSAGE IS SHOWED, THEN HOOK IS INSTALLED");
   }

   //Call the next hook in the chain
   if (callNextHook) {
      return CallNextHookEx(HookHandle,code,wParam,lParam);
   }

   return 0;
 }

I already tried disabling antivirus, changing user account control, running the program as admin... nothing works. What is causing this difference?

解决方案

It probably has to do with the LowLevelHooksTimeout value in the registry.

On faster machines, they can process the hooks fast enough and make it under the default 200 ms to process timeout. On slower machines, they have a harder time.

For me I've had to bump up this value from the default to 500 ms (0x1F4) for my application involving hooks, to be reliable across machines.

To see the effect of changing this registry value, you have to restart your computer.

See the fourth paragraph in the remarks on the documentation here:

LowLevelKeyboardProc callback function

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 times out, the system passes the message to the next hook. However, on Windows 7 and later, the hook is silently removed without being called. There is no way for the application to know whether the hook is removed.

Hope that helps.

这篇关于低级键盘钩可在Windows 7 x64上的一个上运行,而不能在另一个上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆