Home > Software engineering >  How to block a message using WH_GETMESSAGE hook?
How to block a message using WH_GETMESSAGE hook?

Time:08-16

In the CBTProc hook, there's: return 0 to allow the msg, 1 to deny.

What about the WH_GETMESSAGE hook? How I could 'block' a message from being executed?

In this example:

LRESULT CALLBACK GetMsgProc(_In_ int nCode, _In_ WPARAM wParam, _In_ LPARAM lParam
)
{
    if (nCode < 0)
        return CallNextHookEx(nullptr, nCode, wParam, lParam);

    MSG* pMsg = (MSG*)lParam;

    if ((pMsg->message == WM_KEYDOWN) && (wParam == PM_REMOVE))
    {

    }

    return CallNextHookEx(g->getmsgproc, nCode, wParam, lParam);

}

When the code gets inside of the block if ((pMsg->message == WM_KEYDOWN), how i could deny it from executing the WM_KEYDOWN msg?

Do I need to replace pMsg->message with something else or what?

Inside of both return CallNextHookEx(... the first parameter must be nullptr?

CodePudding user response:

What about the WH_GETMESSAGE hook? How I could 'block' a message from being executed?

You can't block the message. Once all hook procedures in the chain have exited, the message is then passed to the original caller of (Get|Peek)Message(). There is no option to avoid that.

Do I need to replace pMsg->message with something else or what?

Yes, you can modify the message. For instance, you could set its message field to WM_NULL so the caller will effectively ignore it.

Inside of both return CallNextHookEx(... the first parameter must be nullptr?

It doesn't really matter what you set it to. In very old versions of Windows (Win9x/ME), it had to be set to the actual HHOOK, but that hasn't been true for a very long. Since NT4/Win2K, the parameter is completely ignored. So yes, setting it to nullptr is fine.

  • Related