window 失焦时 WndProc 不起作用

WndProc does not work when window is out of focus

我想使用 SendMessage 从全局挂钩 WM_COPYDATAWM_COPYDATA,然后将其发送到我的 Mainwindow WndProcWndProc只在活动屏幕时监听procs,不在焦点外时不接收dll发送的消息。

这是 WndProc 的限制吗?有更好的替代品吗?

我发现问题出在我的 SendMessage 调用中使用了 HWND。它必须像这样由所有 dll 共享:

#pragma data_seg("Shared")
//our hook handle which will be returned by calling SetWindowsHookEx function
HHOOK hkKey = NULL;
HINSTANCE hInstHookDll = NULL;  //our global variable to store the instance of our DLL
HWND pHWnd = NULL; // global variable to store the mainwindow handle
#pragma data_seg() //end of our data segment

#pragma comment(linker,"/section:Shared,rws")
// Tell the compiler that Shared section can be read,write and shared

__declspec(dllexport) LRESULT CALLBACK procCharMsg(int nCode, WPARAM wParam, LPARAM lParam)
//this is the hook procedure
{
    //a pointer to hold the MSG structure that is passed as lParam
    MSG* msg;
    //to hold the character passed in the MSG structure's wParam
    char charCode;
    if (nCode >= 0 && nCode == HC_ACTION)
        //if nCode is less than 0 or nCode
        //is not HC_ACTION we will call CallNextHookEx
    {
        //lParam contains pointer to MSG structure.
        msg = (MSG*)lParam;
        if (msg->message == WM_CHAR)
            //we handle only WM_CHAR messages
        {
            SendMessage(pHWnd, WM_CHAR, (WPARAM)msg->message, (LPARAM)0); // This should now work globally
        }
    }
    return CallNextHookEx(hkKey, nCode, wParam, lParam);
}

// called by main app to establish a pointer of itself to the dlls
extern "C" __declspec(dllexport) int SetParentHandle(HWND hWnd) {
    pHWnd = hWnd;
    if (pHWnd == NULL) {
        return 0;
    }

    return 1;
}

我应该连同问题一起发布我的代码,因为小事情总是很难单独发现。