简体   繁体   English

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

[英]WndProc does not work when window is out of focus

I wanted to use SendMessage to a WM_COPYDATA from a global hook dll , and then send it to my Mainwindow WndProc . 我想从全局钩子dll使用SendMessageWM_COPYDATA ,然后将其发送到我的Mainwindow WndProc WndProc only listens to procs when it is the active screen, and does not receive the message being sent by the dll when it is out of focus. WndProc仅在活动屏幕上时才侦听proc,而在焦点未对准时则不会接收dll发送的消息。

Is this a limitation of WndProc? 这是WndProc的限制吗? are there better alternatives to this? 有更好的替代方法吗?

I found out the problem to be the HWND being used on my SendMessage call. 我发现问题是在我的SendMessage调用中使用了HWND It has to be shared by all dlls like so: 必须由所有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;
}

I should have posted my code along with the problem, since little things are always pretty hard to spot alone. 我应该将我的代码与问题一起发布,因为很少有东西总是很难单独发现的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM