使用 SetWindowsHookEx() 阻止 windows 鼠标点击

Blocking windows mouse click using SetWindowsHookEx()

我编写了一个应用程序来将一些过程挂接到新进程上以监视鼠标按下事件并在新进程上禁用鼠标按下事件。截至目前,我能够捕获进入此过程的鼠标按下事件,并且我正在尝试禁用所有鼠标按下事件作为 POC。这就是我目前在钩子程序中所做的。

extern "C" __declspec(dllexport) LRESULT  __stdcall meconnect(int code, WPARAM wParam, LPARAM lParam) {

    if (code >= 0) {
        LPMSG msg = (LPMSG)lParam;

        if (msg->message == WM_LBUTTONDOWN) {

            OutputDebugString(L"Mouse down event happened \n");

            return false;

        }

    }

    return(CallNextHookEx(NULL, code, wParam, lParam));

}

当我执行鼠标按下事件时,我收到了我编写的日志消息。但我也希望 click 事件被阻止,因为我返回 false。但它并没有发生,点击事件作为正常点击进行。我怎样才能禁用鼠标按下事件。在此先感谢您对此的任何帮助

这就是我调用 setWindowsHookEx 的方式

HHOOK handle = SetWindowsHookEx(WH_GETMESSAGE, addr, dll, threadID);

您应该在挂钩例程中调用 CallNextHookEx 的原因是消息可以传递给可能已安装的任何其他挂钩。不这样做不会阻止接收消息的应用程序看到该消息。

documentation for WM_NULL说明了如何屏蔽消息:

For example, if an application has installed a WH_GETMESSAGE hook and wants to prevent a message from being processed, the GetMsgProc callback function can change the message number to WM_NULL so the recipient will ignore it.

因此,更正后的代码应如下所示:

extern "C" __declspec(dllexport) LRESULT  __stdcall meconnect(int code, WPARAM wParam, LPARAM lParam) {

    if (code >= 0) {

        LPMSG msg = (LPMSG)lParam;

        if (msg->message == WM_LBUTTONDOWN) {

            OutputDebugString(L"Mouse down event happened \n");

            msg->message = WM_NULL;

            return false;

        }

    }

    return(CallNextHookEx(NULL, code, wParam, lParam));

}

但是,如果存在其他钩子,这可能会导致不一致的行为,因为另一个钩子是否看到 WM_LBUTTONDOWNWM_NULL 将取决于钩子链的顺序,这是不可预测的。尝试这样的事情可能更可取:

extern "C" __declspec(dllexport) LRESULT  __stdcall meconnect(int code, WPARAM wParam, LPARAM lParam) {

    if (code >= 0) {

        LPMSG msg = (LPMSG)lParam;

        int result = CallNextHookEx(NULL, code, wParam, lParam);

        if (msg->message == WM_LBUTTONDOWN) {

            OutputDebugString(L"Mouse down event happened \n");

            msg->message = WM_NULL;

        }

        return result;

    }

    return(CallNextHookEx(NULL, code, wParam, lParam));

}