WM_PASTE挂钩无法正常工作

时间:2012-09-09 00:40:37

标签: c++ winapi hook

我希望每次粘贴东西都能检测到。这只是使一些数据输入工作更简单的事情。 我设置了一个全局钩子然后“等待”wm_paste。这是我的代码的一部分:

LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if(nCode < 0)//Do not process the message
        return CallNextHookEx(msg_hook,nCode,wParam,lParam);

    LPMSG m=(LPMSG)lParam;

    if(m->message == WM_PASTE)
    {
        OutputString("Paste detected!\n");
    }
    if(m->message == WM_PASTE)
    {
        OutputString("Paste detected!\n");
    }


    return CallNextHookEx(msg_hook,nCode,wParam,lParam);
    }





//DLL_ATTACH:
...
if(strstr(ProcName, LOADERNAME))
        {
            InitCommonControls();

            if(!(msg_hook=SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc, hinstDLL, 0)))
            {
                ErrorExit(TEXT("SetWindowsHookEx"));
                //MessageBox(0, "WH_GETMESSAGE", 0, 0);
                //return -1;
            }
        }

永远不会打印WM_PASTE调试字符串。我知道并非所有应用程序都使用WM_PASTE。但至少记事本应该有用。

有什么建议吗? 谢谢!

2 个答案:

答案 0 :(得分:2)

GetMsgProc中,wParam参数不是被拦截的消息,而是一个标志,指示lParam中的消息是否已从消息队列中删除。

您应该使用m->wParam代替。

答案 1 :(得分:0)

仅在组合框和编辑控件中触发Wm_paste消息。没有简单的方法来捕获粘贴,但您可以通过创建一个小窗口并添加此window to the chain of clipboard viewers来获取复制消息。