Search code examples
c++dllmfcwindows-messageswindow-messages

Unable to receive / capture Windows Messages


I'm fairly new to MFC and the Message handling context.

I've a DLL Consumer application, which has a CFrameWndEx derived class,CMainFrame. Now this calls a DLL, which puts a CDialog etc. into this MainFrame window.

I wish to receive certain messages into my application.

So what I have done is, declared the expected messages in the Message Map, of the DLL Consumer application, and defined the appropriate message handlers.

Now, even though I can see that the application is being sent those registered messages, I'm not able to receive / handle them in the Consumer Window, i.e., nothing happens when those messages are broadcast.

Mainfrm.h

class CMainFrame : public CFrameWndEx
{
public:
    CMainFrame();
protected: 
    DECLARE_DYNAMIC(CMainFrame)
public:
    void OnFileDialogdisplay(void);
    afx_msg LRESULT OnLogonChanged(WPARAM,LPARAM);
    afx_msg LRESULT OnLanguageChanged(WPARAM,LPARAM);
    afx_msg LRESULT OnShutdownRequested(WPARAM,LPARAM);
    afx_msg LRESULT OnReconnectServer(WPARAM,LPARAM); 
    afx_msg LRESULT OnChangeRole(WPARAM,LPARAM); 
}

Mainfrm.cpp

<some header files>
static UINT UWM_LOGON_CHANGED = ::RegisterWindowMessage(UWM_LOGON_CHANGE);
static UINT UWM_LANGUAGE_CHANGED = ::RegisterWindowMessage(UWM_LANGUAGE_CHANGE);
static UINT UWM_RECONNECT = ::RegisterWindowMessage(UWM_RECONNECT_SERVER);
static UINT UWM_SHUTDOWN_REQUESTED = ::RegisterWindowMessage(UWM_REQUEST_SHUTDOWN);
static UINT UWM_ROLE = ::RegisterWindowMessage(UWM_ROLE_CHANGE);

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWndEx)
    ON_WM_CREATE()
    ON_WM_SETFOCUS()
    ON_COMMAND(ID_VIEW_CUSTOMIZE, &CMainFrame::OnViewCustomize)
    ON_REGISTERED_MESSAGE(AFX_WM_CREATETOOLBAR, &CMainFrame::OnToolbarCreateNew)
    ON_COMMAND(ID_FILE_DIALOGDISPLAY, &CMainFrame::OnFileDialogdisplay)
    ON_REGISTERED_MESSAGE(UWM_LOGON_CHANGED, OnLogonChanged)
    ON_REGISTERED_MESSAGE(UWM_LANGUAGE_CHANGED, OnLanguageChanged)
    ON_REGISTERED_MESSAGE(UWM_SHUTDOWN_REQUESTED, OnShutdownRequested)
    ON_REGISTERED_MESSAGE(UWM_RECONNECT, OnReconnectServer) 
    ON_REGISTERED_MESSAGE(UWM_ROLE, OnChangeRole) 
    //ON_WM_NCCALCSIZE()
END_MESSAGE_MAP()

//code to register to Main server application to be able to receive messages

void manageregistration(CMainFrame* pFrame, bool flag)
{
    if (flag) 
    { // registration
        HWND MyHandle = (HWND)pFrame->GetActiveWindow();

        RegisterApmsClientPgm(_T("AAUserInterface"), MyHandle);
    }
}

//Handlers

LRESULT CMainFrame::OnShutdownRequested(WPARAM,LPARAM lp)
{
    AfxMessageBox(_T("Error"),MB_ICONERROR | MB_OK);
    testProgram();
    return 0;
}

LRESULT CMainFrame::OnChangeRole(WPARAM,LPARAM lp)
{
    AfxMessageBox(_T("Error"),MB_ICONERROR | MB_OK);
    testProgram();
    return 0;
}

// etc etc.etc.

So, after all this, I can see that the Consumer application, is registered to receive these messages from another application which broadcasts them.

However, upon creating the condition where the messages are being broadcast, and they are as I can verify from other applications which receive them, no such message is being caught in my application.

I am not sure where the problem could be. The window is always on top, albeit with another CDialog derived DLL inside it.


Solution

  • Try using pFrame->m_hWnd. You can't assume that the Mainframe window is always active.