为什么必须使用SetWindowsHookEx与Windows消息队列 [英] Why must SetWindowsHookEx be used with a windows message queue

查看:751
本文介绍了为什么必须使用SetWindowsHookEx与Windows消息队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试一些钩子,我不明白为什么钩子必须与消息队列一起使用

  hook = SetWindowsHookEx(WH_KEYBOARD_LL,KeyboardProc,NULL,0); 
MSG msg;
while(GetMessage(& msg,NULL,0,0)> 0)
{
TranslateMessage(& msg);
DispatchMessage(& msg);
}
UnhookWindowsHookEx(hook);

为什么这样不起作用?

  hook = SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,NULL,0); 
cin>>关键;
UnhookWindowsHookEx(hook);

使用boost线程,并且障碍也不起作用。为什么不能用另一种方式在钩子和钩子之间等待?



编辑:



我创建了一个WH_KEYBOARD_LL钩子,而不是WH_KEYBOARD,(我不认为这是一个很大的区别)



也没有循环只需等待GetMessage函数。



只有当我发布退出消息 PostThreadMessage(id,WM_QUIT,2323,NULL) / code>所以我不明白在等待,它有什么做它有一些内部处理?



相关:



C ++ SetWindowsHookEx WH_KEYBOARD_LL正确设置

如何在Win32控制台窗口上设置CBT挂钩?

解决方案

低级钩子WH_KEYBOARD_LL和WH_MOUSE_LL与所有其他钩子不同。它们不需要将DLL注入到目标进程中。相反,Windows在您自己的进程内直接调用钩子回调。要进行这项工作,需要一个消息循环。没有其他机制让Windows在您的主线程上进行回调,只有当您调用Get / PeekMessage()时,才会发生回调,以便Windows可以控制。



像WH_KEYBOARD一样的全局钩子是非常不同的。它需要一个DLL,并且回调发生在处理键盘消息的进程内。你需要某种进程间的通信,让你自己的程序知道这一点。命名管道是通常的选择。否则当然要求该注入过程泵送消息循环。否则不会收到键盘消息。



喜欢低级钩子,他们更容易上手。但做泵或它不会工作。注意超时,如果你没有足够的反应,那么Windows将杀死你的钩子,而不通知。



了解低级鼠标和键盘钩子(win32)


I've been trying some things with hooks, and I don't understand why hooks must be used with a message queue

hook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, NULL, 0);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
        TranslateMessage(&msg);
        DispatchMessage(&msg);
}
UnhookWindowsHookEx(hook);

Why doesn't something like this work ?

hook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, NULL, 0);
cin >> aKey;
UnhookWindowsHookEx(hook);

Using boost threads, and a barrier doesn't work either. Why can't the waiting between the hook and unhook be done in another manner ?

EDIT:

I did a mistake when I created this sample, I create a WH_KEYBOARD_LL hook, not WH_KEYBOARD, (I don't think it makes a big difference)

Also the loop never executes only waits on the GetMessage function.

The loop executes only when I post the quit message PostThreadMessage(id, WM_QUIT, 2323, NULL); so I don't understand what does it do beside waiting, is there some internal processing ?

RELATED:

C++ SetWindowsHookEx WH_KEYBOARD_LL Correct Setup

How can I set up a CBT hook on a Win32 console window?

解决方案

The low-level hooks, WH_KEYBOARD_LL and WH_MOUSE_LL are different from all the other hooks. They don't require a DLL to be injected into the target process. Instead, Windows calls your hook callback directly, inside your own process. To make that work, a message loop is required. There is no other mechanism for Windows to make callbacks on your main thread, the callback can only occur when you've called Get/PeekMessage() so that Windows is in control.

A global hook like WH_KEYBOARD is very different. It requires a DLL and the callback occurs within the process that processes the keyboard message. You need some kind of inter-process communication to let your own program be aware of this. Named pipes are the usual choice. Which otherwise of course requires that this injected process pumps a message loop. It wouldn't get keyboard messages otherwise.

Favor a low-level hook, they are much easier to get going. But do pump or it won't work. And beware of timeouts, if you're not responsive enough then Windows will kill your hook without notice.

Understanding the low-level mouse and keyboard hook (win32)

这篇关于为什么必须使用SetWindowsHookEx与Windows消息队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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