0

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user1173240
  • 1,455
  • 2
  • 23
  • 50
  • You should use Spy++ on each window in question to see what window, if any, is actually receiving the messages. Also, you did not post the code that actually dispatches the registered messages. That would be nice to see. – rrirower Aug 20 '14 at 12:20
  • I used `Spy++`, and created a condition so my application can receive the messages. But I did not get anything. The message dispatcher application uses the `HWND` to send the messages. I'm just wondering whether I've to send the `HWND` of the parent window, or that of the `CDialog` window form the` DLL`. Also, I cannot get the `HWND `Of the `DLL` Dialog window, `FindWindowEx` from the `mainFrame` class returns nothing, despite the fact that `Spy++` shows that the `Dialog `Window is created with a name. – user1173240 Aug 20 '14 at 13:02
  • The code you've posted implies you will be handling the messages in the mainframe class. – rrirower Aug 20 '14 at 13:06
  • Yes, that is where I would like to handle it. And I have tried to get the `HWND` of the Active Window, and have used it to register with the Message Dispatcher. However, when the messages are sent, they are not caught by the Window. So is the `GetActiveWindow()` function in the `MainFrame` returning something else, maybe because of visibility settings? Or is there something else I need to do to receive these messages? – user1173240 Aug 20 '14 at 13:11
  • Given what you've posted, You should be using the handle to the mainframe window when sending the messages. You can use the CWinApp object to get the mainframe window and it's handle. – rrirower Aug 20 '14 at 13:22
  • `HWND MyHandle = (HWND)pFrame->GetActiveWindow();` in the mainframe window won't do the trick? – user1173240 Aug 20 '14 at 13:25
  • 3
    Try using pFrame->m_hWnd. You can't assume that the Mainframe window is always active. – rrirower Aug 20 '14 at 13:31
  • Ah thank you. That helped. It was the `FindActiveWindow` indeed which was causing the issue. If you write it out as the answer, I'll accept it. – user1173240 Aug 21 '14 at 06:50
  • I am facing a similar issue, where are you calling manageregistration – Geek May 21 '19 at 20:24

1 Answers1

1

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

rrirower
  • 4,338
  • 4
  • 27
  • 45