Calling twice a CDialog::DoModal from InitInstance

I wrote a small dialog based program. Within InitInstance() I want display another dialog box after returning form the first dialog. But the second dialog box never shows. Is this not possible in MFC or do I something wrong?

My InitInstance looks like:

BOOL CTestdlgApp::InitInstance()
{
 AfxEnableControlContainer();
 Enable3dControls();

 {
  CTestdlgDlg dlg;
  m_pMainWnd = &dlg;
  dlg.DoModal();
 }

 {
  CTestdlgDlg dlg2;
  m_pMainWnd = &dlg2;
  dlg2.DoModal();
 }

 return FALSE;
}
LVL 2
eelkoAsked:
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.

ZoppoCommented:
Hi eelko,

the problem seems to be the expression 'm_pMainWnd = &dlg;'. I think because the application sees the first dialog as main window a WM_QUIT message is posted when it is destroyed.
Just remove this line and it works without problems.

hope that helps,

ZOPPO
0

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
eelkoAuthor Commented:
This works!

But do you know the purpose of this main window? Cause if I change my code like this (see below) m_pMainWnd will never be assigned when the user presses "Cancel".

 CTestdlgDlg dlg;
 if (dlg.DoModal() == IDOK)
 {
  CTestdlgDlg dlg2;
  m_pMainWnd = &dlg2;
  dlg2.DoModal();
 }
0
ZoppoCommented:
Hi eelko,

there's another way for this without changing the m_pMainWnd pointer. You can catch the WM_QUIT message before showing the second dialog with something like this:


CTestdlgDlg dlg;
int result = dlg.DoModal();

// remove WM_QUIT message from queue
MSG msg;
while (::PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ))
  if ( msg.message == WM_QUIT )
    break;

// process dialogs return value
if (result == IDOK)
{
CTestdlgDlg dlg2;
m_pMainWnd = &dlg2;
dlg2.DoModal();
}

hope that helps,

ZOPPO
0
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.