windows 使用 C++ 的全局键盘钩子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3492037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Global keyboard hook with C++
提问by Mikulas Dite
I've already saw many tutorials and articles about hooking, yet I don't quite understand it. Mainly because every single example uses different solution.
我已经看过很多关于hook的教程和文章,但我不太明白。主要是因为每个示例都使用不同的解决方案。
I know I will have to implement something that will keep the hook alive. Usually it's some kind of while cycle. Q1: If this loop was in some class with callbacks, will it prevent the thread from executing them?
我知道我将不得不实施一些能让钩子保持活力的东西。通常它是某种while循环。Q1:如果这个循环在某个带有回调的类中,它会阻止线程执行它们吗?
I know it will take a while, but I would highly appreciate some well explained example of global keyboard hook. Or simply link me to some workingexample with binaries. (Trust me, I've been trying to google it last few hours).
我知道这需要一段时间,但我非常感谢一些解释清楚的全局键盘钩子示例。或者简单地将我链接到一些带有二进制文件的工作示例。(相信我,过去几个小时我一直试图用谷歌搜索它)。
Thank you
谢谢
采纳答案by Hans Passant
I know I will have to implement something that will keep the hook alive
我知道我将不得不实施一些让钩子保持活力的东西
No, that's not a concern. A global hook requires a DLL with the callback. That DLL gets injected in all running processes. It will stay loaded in the process until you call UnHookWindowsHookEx() or the process terminates, whichever comes first.
不,这不是问题。全局钩子需要带有回调的 DLL。该 DLL 被注入到所有正在运行的进程中。它将在进程中保持加载状态,直到您调用 UnHookWindowsHookEx() 或进程终止,以先到者为准。
Do note that you can also hook the keyboard with WH_KEYBOARD_LL. That's nota global hook, Windows will switch context to your program and make the callback. It is much easier to use since you don't need an IPC mechanism with the injected DLL that the global hook requires. The low level hook stays active until you unhook, the thread that owns the message queue terminates or your process terminates, whichever comes first.
请注意,您也可以使用 WH_KEYBOARD_LL 挂钩键盘。这不是全局钩子,Windows 会将上下文切换到您的程序并进行回调。它更容易使用,因为您不需要带有全局钩子所需的注入 DLL 的 IPC 机制。低级钩子保持活动状态,直到您取消钩子,拥有消息队列的线程终止或您的进程终止,以先到者为准。
回答by Bob Moore
There's a skeletal keyboard hook with downloadable code on my web site here(note the "download" button at the bottom of the page). The article discusses some things you need to understand like shared sections, and informs you of Kyle Marsh's excellent article on Win32 hooks (if some idiot at MSDN hasn't taken it down by now, for being insufficiently .netty).
有一个与我的网站上下载代码骨骼键盘钩子这里(注意页面底部的“下载”按钮)。这篇文章讨论了一些你需要理解的东西,比如共享部分,并告诉你 Kyle Marsh 的关于 Win32 钩子的优秀文章(如果 MSDN 的某个白痴现在还没有把它拿下来,因为它不够 .netty)。
The source code is in the example, it just needs a makefile/sln building for it (I don't know what compiler/version you'll be using). Code spookily similar to that has been in a shipping commercial product for a decade, so I knowit works.
源代码在示例中,它只需要为它构建一个 makefile/sln(我不知道您将使用什么编译器/版本)。十年前,商业运输产品中的代码与该代码惊人地相似,所以我知道它是有效的。
Note that integrity level issues can reduce the utility of hooking in Vista and W7.
请注意,完整性级别问题会降低在 Vista 和 W7 中挂钩的效用。
回答by tenfour
your program will need to stay alive during the hook. Your program is the one that calls SetWindowsHookEx / UnSetWindowsHookEx (or whatever it's called). Between those calls, you will indeed have a message loop (this is probably the while loop you're talking about) just like any typical windows program.
您的程序需要在挂钩期间保持活动状态。您的程序是调用 SetWindowsHookEx / UnSetWindowsHookEx(或其他任何名称)的程序。在这些调用之间,您确实会有一个消息循环(这可能就是您正在谈论的 while 循环),就像任何典型的 Windows 程序一样。
But because your program is a different process than the ones you're hooking, your message loop will not cause other processes to hang. It's called multitasking :)
但是因为您的程序与您正在挂钩的程序是不同的进程,所以您的消息循环不会导致其他进程挂起。这叫做多任务处理:)