WH_KEYBOARD_LL挂接不叫 [英] WH_KEYBOARD_LL hook not called

查看:156
本文介绍了WH_KEYBOARD_LL挂接不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题,一个 WH_KEYBOARD_LL 挂钩:

I'm having some problems with a WH_KEYBOARD_LL hook :

之所以我使用的是全球LL钩并不重要我只需要它为我的应用程序(我试过其他类型的钩子,但他们并没有为我工作)。

The reason why I'm using a global LL hook is not important I just need it for my app (I tried other types of hooks but they didn't work for me).

钩子函数是一个dll,该dll被加载在应用程序启动和钩也设置在主线程的启动。这完美的作品。当我需要停用挂钩并重新回出现的问题。如果我这样做,从应用程序的主线程它的作品不错,但我需要的是从一个计时器做到这一点,并在这里出问题。我用计时器来检查,如果我的应用程序的窗口是前台窗口(活动窗口),如果是这样的钩被激活,如果没有钩被取消。 和SetWindowsHookEx 的返回值从定时器调用时始终是ok(NOT NULL)根据MSDN这意味着钩设置成功,但我的钩子函数永远不会被调用。

The hook function is in a dll, the dll is loaded on the application start-up and the hook is set also on start-up from the main thread. This works perfectly. The problem appears when I need to deactivate the hook and reactivate it back. If I do that from the main thread of the application it works ok , but what I need is to do this from a timer, and here things go wrong. I use the timer to check if my app's window is the foreground window (the active window), if so the hook is activated and if not the hook is deactivated. The return value of SetWindowsHookEx when called from the timer is always ok (not null) which according to MSDN means that the hook was successfully set, but my hook function never gets called.

下面是我如何设置我的挂钩:

Here is how I set my hook:

SetWindowsHookEx(WH_KEYBOARD_LL, keyboardHookProcedure,
                 GetModuleHandle(curModule.ModuleName), 0);



有没有人遇到过这个?

Has anyone ever experienced this??

我唯一的猜测是,我的钩子函数是一个DLL和定时器的回调是从另一个DLL,这是否有什么关系与我的问题??

My only guess is that my hook function is in a dll and the timer callback is from another dll, does this has anything to do with my problem??

推荐答案

钩子回调被调用SetWindowsHookEx函数在同一个线程上进行()。神奇该位要求线程泵消息循环。这是致命的问题,你的计时器回调方法是从一个线程池线程调用。它不抽,它甚至没有足够长的时间都无法得到钩子回调。

The hook callback is made on the same thread that called SetWindowsHookEx(). That bit of magic requires that thread to pump a message loop. Which is the rub, your timer callback method is called from a threadpool thread. It doesn't pump, it's not even around long enough to ever be able to get the hook callback.

调用到你的UI线程或使用同步计时器。或考虑只是暂时禁用无论你在钩子回调做的完全禁用或更换钩代替,那肯定最有意义的。

Invoke to your UI thread or use a synchronous timer. Or consider just temporarily disabling whatever you do in the hook callback instead of completely disabling or replacing the hook, that certainly makes most sense.

这篇关于WH_KEYBOARD_LL挂接不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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