Blocking windows mouse click using SetWindowsHookEx()

守給你的承諾、 提交于 2019-12-06 18:03:10

问题


I have written an application to hook some procedure onto a new process to monitor mouse down events and to disable mouse down events on the new process. As of now, I am able to capture to mouse down events coming to this process and I am trying to disable all mouse down events as a POC. This is what I am doing currently in the hook procedure.

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

}

When I perform mouse down event, I am getting the log message that I have written. But I also expect that click event to be blocked since I am returning false. but it does not happen so and click event proceed as a normal click. How could I disable mouse down event. Thanks in advance for any help on this

This is how I call setWindowsHookEx

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

回答1:


The reason you are supposed to call CallNextHookEx during your hook routine is so that the message can be passed on to any other hooks that might be installed. Failing to do so does not prevent the message from being seen by the application that received it.

The documentation for WM_NULL explains how to block the message:

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.

The corrected code should therefore look something like this:

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

}

However, this may cause inconsistent behaviour if other hooks are present, because whether another hook sees WM_LBUTTONDOWN or WM_NULL will depend on the order of the hook chain, which is unpredictable. It might be preferable to try something like this:

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

}


来源:https://stackoverflow.com/questions/42756284/blocking-windows-mouse-click-using-setwindowshookex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!