是否有可能发现当低级别的键盘钩子由Windows已经自动断开? [英] Is it possible to detect when a low-level keyboard hook has been automatically disconnected by Windows?

查看:238
本文介绍了是否有可能发现当低级别的键盘钩子由Windows已经自动断开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,使用键盘钩子程序。然而,当该程序运行的PC只是稍微超载,它会导致Windows从程序断开挂钩,使其不再按键响应。



有没有办法防止这种情况,甚至更好,提出解决相同的问题,通过使用不同的架构,可能涉及到管道的不同的方式?


解决方案

您不能检测这一点,你绝对不应该需要。什么你所描述的是一种功能,具体之一,Windows 7的推出,以保护您的系统免受恶意应用程序



applicable文档描述它正是如此(特别注意粗体部分):




挂钩过程应该比在以下注册表项中的 LowLevelHooksTimeout 值指定的数据录入时间不处理消息:



<预类=郎无prettyprint-覆盖> HKEY_CURRENT_USER\Control Panel\Desktop

该值以毫秒为单位。如果钩子程序超时,系统将消息传递到下一个钩子。然而, Windows 7和以后,无钩被称为被悄悄删除。有没有办法让应用程序知道挂钩是否被删除。




这里的解决方案肯定是<强>不可以找出​​一种方法来检测,当钩被卸载并重新安装。你应该已经想通了,你做错事的时候,操作系统卸载挂钩的第一个的时间。



实际的解决方案是重新设计你的应用程序从挂钩过程更快地返回。理想情况下,你应该几乎立即返回。如果您需要响应低水平运行的消息某种类型的密集计算(我真的不能想象,你为什么会),那么你应该保存您收到的信息,从挂钩过程中返回,并做你的处理。稍后的时间(可能是在一个单独的线程)



在事实上,这几乎的究竟的文档继续在什么建议:




注意:调试钩子不能跟踪该类型的低级别的键盘钩子。如果该应用程序必须使用低级别钩子,它应该运行上传递的工作开了一个工作线程,然后立即返回一个专用线程钩。当应用程序需要使用低的水平挂钩大多数情况下,它应该监视原始输入代替。这是因为原始输入可以异步监控比低水平钩能更有效地针对其他线程鼠标和键盘消息。有关原始输入的更多信息,请参见原始输入


< /块引用>

I am working on a program that uses keyboard hooks. However, when the PC that the program is running on is just slightly overloaded, it causes Windows to disconnect the hook from the program, causing it to no longer respond to keystrokes.

Is there a way to prevent this, or even better, propose a different way of solving the exact same problem, by using a different architecture, maybe involving a pipeline?

解决方案

You can't "detect" this, and you absolutely shouldn't need to. What you're describing is a feature, specifically one introduced in Windows 7 to protect your system from rogue applications.

The applicable documentation describes it thusly (pay particular attention to the bolded section):

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.

The solution here is most certainly not to figure out a way to "detect" when the hook is uninstalled and reinstall it. You should have figured out that you're doing something wrong when the operating system uninstalled the hook the first time.

The actual solution is to redesign your application to return from the hook procedure more quickly. Ideally, you should return almost immediately. If you need to run some type of intensive calculation in response to the low level messages (and I can't really imagine why you would), then you should store the information you receive, return from the hook procedure, and do your processing at a later time (probably on a separate thread).

In fact, that's almost exactly what the documentation continues on to suggest:

Note: Debug hooks cannot track this type of low level keyboard hooks. If the application must use low level hooks, it should run the hooks on a dedicated thread that passes the work off to a worker thread and then immediately returns. In most cases where the application needs to use low level hooks, it should monitor raw input instead. This is because raw input can asynchronously monitor mouse and keyboard messages that are targeted for other threads more effectively than low level hooks can. For more information on raw input, see Raw Input.

这篇关于是否有可能发现当低级别的键盘钩子由Windows已经自动断开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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