• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1201
  • Last Modified:

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.
1 Solution
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.
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now