使用SetWindowsHookEx()阻止Windows鼠标单击 [英] Blocking windows mouse click using SetWindowsHookEx()

查看:958
本文介绍了使用SetWindowsHookEx()阻止Windows鼠标单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个应用程序,将某些过程挂接到新进程上,以监视鼠标按下事件并禁用新进程上的鼠标按下事件.到目前为止,我能够捕获到此过程中发生的鼠标按下事件,并且尝试禁用所有鼠标按下事件作为POC.这是我当前在挂接过程中正在做的事情.

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

}

当我执行鼠标按下事件时,我得到的是我写的日志消息.但我也希望单击事件会被阻止,因为我返回的是false.但这种情况不会发生,并且点击事件会像普通点击一样进行.我如何禁用鼠标按下事件.预先感谢您对此

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

这就是我所说的setWindowsHookEx

This is how I call setWindowsHookEx

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

推荐答案

应该在挂接例程中调用CallNextHookEx的原因是,以便可以将消息传递到可能已安装的任何其他挂接.未能做到这一点不会阻止收到该消息的应用程序看到该消息.

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.

WM_NULL 的文档说明了如何阻止该消息:

The documentation for WM_NULL explains how to block the message:

例如,如果应用程序安装了WH_GETMESSAGE挂钩并希望阻止处理消息,则GetMsgProc回调函数可以将消息号更改为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.

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

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

}

但是,如果存在其他挂钩,这可能会导致行为不一致,因为另一个挂钩看到WM_LBUTTONDOWN还是WM_NULL将取决于挂钩链的顺序,这是无法预测的.尝试这样的操作可能更可取:

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

}

这篇关于使用SetWindowsHookEx()阻止Windows鼠标单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆