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
    virtual BOOL InitInstance();

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

CMyApp2 myApp;

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

BEGIN_MESSAGE_MAP (CMainWindow2, CFrameWnd)
    ON_WM_PAINT ()

int FilterDllMsg(LPMSG lpMsg)
      //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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
Vinayak KumbarSr Program ManagerCommented:

But U not creating the window!!!. U r just making a object of new window saying it to show. But without creating how it can show the window?. So use the Create(...) function to create the window and then show it.
The sequence will be

m_MyWindow = new CMainWindow;
CRect rect(10,10,200,200);//some coordinates
m_MyWindow->Create(NULL, "New", WS_CHILD | WS_VISIBLE | WS_CAPTION | WS_SYSMENU, rect, this, 4545, NULL);

Then it will display the window.

Try it out.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
feloniusAuthor Commented:
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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.