简体   繁体   English

使用挂钩 (SetWindowsHookEX & WH_GETMESSAGE)

[英]Working with hooks (SetWindowsHookEX & WH_GETMESSAGE)

I'll start with a description of what exactly I need and why.我将首先描述我到底需要什么以及为什么。

I am making an in-game interface in a library (dll), and I need the ability to both receive and delete messages (prevent the target process from receiving them), depending on different conditions in the code.我正在库(dll)中制作游戏内界面,并且我需要能够接收和删除消息(防止目标进程接收它们),具体取决于代码中的不同条件。

In addition to messages from the mouse and keyboard, I do not need anything else.除了来自鼠标和键盘的消息,我不需要其他任何东西。 For this, there are two ways.为此,有两种方法。 Find some kind of hook that will allow me to receive messages from both the mouse and the keyboard, or put two separate hooks on the mouse and keyboard, but there will be much more code than with one hook.找一种可以让我从鼠标和键盘接收消息的钩子,或者在鼠标和键盘上放置两个单独的钩子,但是代码会比一个钩子多得多。

I decided to go the first way and put a WH_GETMESSAGE hook on the messages of the thread that created the window.我决定采用第一种方式 go 并在创建 window 的线程的消息上放置一个WH_GETMESSAGE挂钩。 However, my attempts to block the message were unsuccessful.但是,我阻止该消息的尝试没有成功。

LRESULT CALLBACK messageHandler(int nCode, WPARAM wParam, LPARAM lParam)
{
    return -1; // This works fine with WH_MOUSE or WH_KEYBOARD, but for some reason, with the WH_GETMESSAGE hook, the process still receives a message
}
 
DWORD WINAPI messageDispatcher(LPVOID thread)
{
    hookHandle = SetWindowsHookEx(WH_GETMESSAGE, messageHandler, GetModuleHandle(nullptr), *reinterpret_cast<DWORD*>(thread));
 
    if (!hookHandle)
    {
        return GetLastError();
    }
 
    MSG message{};
 
    while (GetMessage(&message, 0, 0, 0) > 0)
    {
        TranslateMessage(&message);
        DispatchMessage(&message);
    }
 
    return 0;
}

I'm not sure if WH_GETMESSAGE is the right hook for me.我不确定WH_GETMESSAGE是否适合我。 Perhaps much more experienced programmers will tell me that it is better to do, for example, two hooks, WH_MOUSE and WH_KEYBOARD , rather than using WH_GETMESSAGE .也许更有经验的程序员会告诉我,最好使用两个钩子, WH_MOUSEWH_KEYBOARD ,而不是使用WH_GETMESSAGE

But if, nevertheless, using WH_GETMESSAGE is not a bad idea, then please help me to make it so that I can control the receipt of some messages by the process (do not allow them to be seen by the process).但是,如果使用WH_GETMESSAGE不是一个坏主意,那么请帮我制作它,以便我可以控制进程对某些消息的接收(不允许它们被进程看到)。

I decided to go the first way and put the WH_GETMESSAGE hook on the messages of the thread that created the window.我决定采用第一种方式 go 并将 WH_GETMESSAGE 钩子放在创建 window 的线程的消息上。 However, my attempts to block the message were unsuccessful.但是,我阻止该消息的尝试没有成功。

Per the documentation, a WH_GETESSAGE hook cannot block a message, only view/modify it.根据文档, WH_GETESSAGE挂钩不能阻止消息,只能查看/修改它。 When the hook exits, the message is always delivered to the target thread:当钩子退出时,消息总是被传递到目标线程:

GetMsgProc callback function GetMsgProc 回调 function

The GetMsgProc hook procedure can examine or modify the message. GetMsgProc 挂钩过程可以检查或修改消息。 After the hook procedure returns control to the system, the GetMessage or PeekMessage function returns the message, along with any modifications, to the application that originally called it.在挂钩过程将控制权返回给系统后,GetMessage 或 PeekMessage function 会将消息以及任何修改返回给最初调用它的应用程序。

WH_MOUSE/_LL and WH_KEYBOARD/_LL hooks, on the other hand, can block messages, per their respective documentations:另一方面,WH_MOUSE/ WH_MOUSE/_LLWH_KEYBOARD/_LL钩子可以根据各自的文档阻止消息:

MouseProc callback function MouseProc 回调 function

LowLevelMouseProc callback function LowLevelMouseProc 回调 function

KeyboardProc callback function KeyboardProc 回调 function

LowLevelKeyboardProc callback function LowLevelKeyboardProc 回调 function

If the hook procedure processed the message, it may return a nonzero value to prevent the system from passing the message to the rest of the hook chain or the target window procedure.如果钩子程序处理了消息,它可能会返回一个非零值,以防止系统将消息传递给钩子链的 rest 或目标 window 程序。

As such...像这样...

Perhaps much more experienced programmers will tell me that it is better to do, for example, two hooks, WH_MOUSE and WH_KEYBOARD, rather than using WH_GETMESSAGE.也许更有经验的程序员会告诉我,最好使用两个钩子,WH_MOUSE 和 WH_KEYBOARD,而不是使用 WH_GETMESSAGE。

You will indeed have to use separate WH_MOUSE / WH_KEYBOARD hooks.您确实必须使用单独的WH_MOUSE / WH_KEYBOARD挂钩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM