使用钩子捕获全局粘贴事件WH_GETMESSAGE不起作用

时间:2018-01-15 08:33:16

标签: windows winapi hook setwindowshookex

目的: 捕获全局粘贴。只要在任何应用程序中完成ctrl + v,edit-> paste和right click-> paste中的任何一个。无论是文件粘贴还是文本粘贴。

为此,我正在为WH_GETMESSAGE安装一个钩子,并使用返回的message结构的MSG参数检查WM_PASTE消息。以下是hook dll源:

//Hook DLL source    

//---------------------------------------------------------------------------
//
// Shared by all processes variables
//
//---------------------------------------------------------------------------
#pragma comment(linker, "/section:.HKT,RWS")
#pragma data_seg(".HKT")
// The hook handle
HHOOK sg_hGetMsgHook       = NULL;
// Indicates whether the hook has been installed
BOOL  sg_bHookInstalled    = FALSE;
// get this from the application who calls SetWindowsHookEx()'s wrapper
HWND  sg_hwndServer        = NULL; 
#pragma data_seg()

using namespace std;

//---------------------------------------------------------------------------
//
// Forward declarations
//
//---------------------------------------------------------------------------
 __declspec(dllexport) BOOL WINAPI InstallHook(
    BOOL bActivate, 
    HWND hWndServer
    );

LRESULT CALLBACK GetMsgProc(
    int code,       // hook code
    WPARAM wParam,  // removal option
    LPARAM lParam   // message
    );

HMODULE hDLL;

//---------------------------------------------------------------------------
//
// Definitions
//
//---------------------------------------------------------------------------
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
    switch(ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        hDLL = hModule;
        break;

    case DLL_PROCESS_DETACH:
        UnhookWindowsHookEx(sg_hGetMsgHook);
        break;
    }
    return TRUE;
}

//---------------------------------------------------------------------------
// InstallHook
// This function is exported
//---------------------------------------------------------------------------
BOOL WINAPI InstallHook(
    BOOL bActivate, 
    HWND hWndServer
    )
{
    if(hWndServer)
    {
        sg_hwndServer = hWndServer;
    }

    if(bActivate)
    {
        sg_hGetMsgHook = SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc, hDLL, NULL);
        if(!sg_hGetMsgHook)
        {
            return FALSE;
        }
    }
    else
    {
        if(sg_hGetMsgHook)
        {
            return UnhookWindowsHookEx(sg_hGetMsgHook);
        }
    }
    return TRUE;
}

//---------------------------------------------------------------------------
// GetMsgProc
//
// Filter function for the WH_GETMESSAGE
//---------------------------------------------------------------------------
LRESULT CALLBACK GetMsgProc(
    int code,       // hook code
    WPARAM wParam,  // removal option
    LPARAM lParam   // message
    )
{
    if(code == HC_ACTION)   // if there is an incoming action and a key was pressed
    {
        MSG * lpMsg = (MSG *)lParam;
        if(lpMsg->message == WM_PASTE)
        {
            PostMessage(sg_hwndServer, WM_USER + 100, 0,0);
        }
    }

    //
    // We must pass the all messages on to CallNextHookEx.
    //
    return ::CallNextHookEx(sg_hGetMsgHook, code, wParam, lParam);
}

每当发生粘贴事件时,自定义消息WM_USER + 100应该发布到窗口(ctrl + v,edit-> paste,right click-> paste)。但它不会发生。

为确保此实现是否正确,我尝试挂钩WH_KEYBOARD并在点击空格时发布相同的消息,并正确接收消息。

我做错了什么?请分享您对此的宝贵意见。

0 个答案:

没有答案