Home > Mobile >  Working with hooks (SetWindowsHookEX & WH_GETMESSAGE)
Working with hooks (SetWindowsHookEX & WH_GETMESSAGE)

Time:12-03

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.

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. 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. 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.

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).

CodePudding user response:

I decided to go the first way and put the WH_GETMESSAGE hook on the messages of the thread that created the 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. When the hook exits, the message is always delivered to the target thread:

GetMsgProc callback function

The GetMsgProc hook procedure can examine or modify the message. 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.

WH_MOUSE/_LL and WH_KEYBOARD/_LL hooks, on the other hand, can block messages, per their respective documentations:

MouseProc callback function

LowLevelMouseProc callback function

KeyboardProc callback function

LowLevelKeyboardProc callback 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.

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.

You will indeed have to use separate WH_MOUSE/WH_KEYBOARD hooks.

  • Related