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

Get assertion failure in MFC Dialog-based app

We have a MFC dialog-based app which also supports COM automation.  The MFC wizard which created it placed the following line in the "DlgProxy.cpp" file:

      ASSERT (AfxGetApp()->m_pMainWnd != NULL);

The above line is in the CDlgProxy ctor, which seems to be a class used for automation support.

My problem is that when I run this application via OLE embedding, I get a failure on this line.

m_pMainWnd should be initialised in CMyApp::InitInstance() by the following code:

      CMyAppDlg dlg;
      m_pMainWnd = &dlg;
      int nResponse = dlg.DoModal();
      if (nResponse == IDOK)

 - but obviously the dialog is being created by some other class when the app is started by automation.

I have tried placing the following code in CMyAppDlg ctor:

    // Set myself as the main window if not done already
    if (theApp.m_pMainWnd == NULL)
        theApp.m_pMainWnd = this;

However I still get the assertion.

What is the correct way to avoid this assertion?
1 Solution
For what you use:
m_pMainWnd = &dlg;

When you InitInstance terminating, m_pMainWnd you window still destroyed....
kbridgeAuthor Commented:
I set m_pMainWnd because this is a dialog-based app and I do want the dialog to be the main window for the application.  m_pMainWnd is used by other objects to determine which is the main window for the application.

I am aware that the dialog terminates when InitInstance terminates, but this is how dialog-based apps work.

Thanks for taking the trouble to look at my question anyway.
Are you checked for call InitInstance in OLE embedding mode...
May be it not calling?
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Is the theApp.m_pMainWnd a global variable.
Vinayak KumbarSr Program ManagerCommented:

Ok, I have the dialog based app with automation enabled. Its comiling and running properly. I could not get ur line
>when I run this application via OLE embedding...

How I can run my exe using OLE embedding?

kbridgeAuthor Commented:
Hi VinExpert,

If you want to get OLE support for your exe, you can try the following:

1. Create a fresh app, using classwizard, and check OLE/COM support when prompted.  
2. Then create another one without OLE/COM checked.
3. Use WinDiff to check the differences between the two projects.  (Should only be the OLE/COM support bits.)
4. You can then manually add the OLE/COM support bits into your application.

kbridgeAuthor Commented:
Hi Nicolay_Ch,

I managed to solve the problem myself.  Actually what was happening was that there was some code further up in InitInstance which was causing it to return before executing the dialog creation code.  I have the following code in my InitInstance():

        // Not allowed to run standalone!
        // Print message and quit.
        return FALSE;

      CWCManagementDlg dlg;
      m_pMainWnd = &dlg;
      int nResponse = dlg.DoModal();

There was a problem with the setting of m_bRunStandalone attribute so that it was being set when the app was run via OLE embedding.  When I fixed that, the problem went away.
Yea... It is...

In my servers I not give do this automatically...

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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