Link to home
Start Free TrialLog in
Avatar of felonius
felonius

asked on

Creating modeless windows in a MFC regular dll

I am creating a program which has two components, both writtin in Visual C++ 6.0 using C++ as a shared library.

The main component contains a window and I would also like to create a modeless window in the dll. I have tried to do so according to the Microsoft documentation (and the sample DLLTRACE) but for some reason the window in the dll is never shown on screen. Initinstance is called in the dll and so is PreTranslateMessage with the message from the original program. The new of MainWindow2 creates an object but it has no effect to call ShowWindow and UpdateWindow on it. The window never shows. What is wrong?

Code fragments appear here:

Main.cpp: (exe)

BOOL CMyApp::InitInstance ()
{
    m_pMainWnd = new CMainWindow;
    m_pMainWnd->ShowWindow (m_nCmdShow);
    m_pMainWnd->UpdateWindow ();
    return TRUE;
}

BOOL CMyApp::PreTranslateMessage(MSG* pMsg)
{
      if (CWinApp::PreTranslateMessage(pMsg))
            return TRUE;

      return FilterDllMsg(pMsg);
}

extra.cpp: (dll)

class CMyApp2 : public CWinApp
{
public:
    virtual BOOL InitInstance();

    virtual BOOL PreTranslateMessage(MSG* lpMsg)
    {
      return CWinApp::PreTranslateMessage(lpMsg);
    }
};

CMyApp2 myApp;

BOOL CMyApp2::InitInstance ()
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    m_pMainWnd = new CMainWindow2;
    m_pMainWnd->ShowWindow (m_nCmdShow);
    m_pMainWnd->UpdateWindow ();
    return TRUE;
}

BEGIN_MESSAGE_MAP (CMainWindow2, CFrameWnd)
    ON_WM_PAINT ()
END_MESSAGE_MAP ()

int FilterDllMsg(LPMSG lpMsg)
{
      AFX_MANAGE_STATE(AfxGetStaticModuleState());
      //CWinThread* p = ;
  return AfxGetThread()->PreTranslateMessage(lpMsg);
}

If required I will print the complete source code but I limited here to prevent code overload on the screen.
Avatar of akalmani
akalmani

Hi felonius !!
 
The main application class in MFC encapsulates the initialization, running, and termination of an application for Windows. An application built on the framework must have one (and only one) object of a class derived fromCWinApp. This object is constructed before windows are created.

Hope these statements make u clear.
ASKER CERTIFIED SOLUTION
Avatar of Vinayak Kumbar
Vinayak Kumbar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of felonius

ASKER

First to akalmani:

According to the documentation one need a seperate CWinApp derived global object in regular MFC dll's. This means that there must bo one in the main app and one for each dll. If I used extension dll's then this would be no problem, but I also need to interface the functions in the dll from a MFC app so this is no solution.

And to VinExpert:
I do call create. It was just not shown in the fragment, because i do it in the constructor of CMainWindow.

CMainWindow2::CMainWindow2 ()
{
    VERIFY(Create (NULL, "The Second Hello Application"));
}

But your example code gave me differs from mine in that you call ShowWindow with SW_SHOW and not m_nCmdShow. I tried this and it works!! IN other words the window was created but was just hidden from view. Messages are now sent correctly to both modeless windows.