当窗口焦点不对准时,WndProc不起作用

时间:2019-07-19 07:56:28

标签: c# sendmessage wndproc

我想使用SendMessage到全局钩子WM_COPYDATA中的dll,然后将其发送到我的Mainwindow WndProcWndProc仅在活动屏幕上收听proc,而在焦点未对准时则不会接收dll发送的消息。

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

1 个答案:

答案 0 :(得分:0)

我发现问题出在我的HWND通话中正在使用SendMessage。它必须由所有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;
}

我应该将我的代码与问题一起发布,因为很少有东西总是很难单独发现的。