I have an MFC dialog based application where I want to change the dialog. To do this I close the dialog and try to load it again with another dialog template. The second invocation of the dialog fails with a return code of -1.
Even without changing the template, the problem stays the same. GetLastError() returns 0. I have used AppWizard to generate the simplest possible example.
The App wizard generates the following code in CMyApp::InitInstance:
CMyAppDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
...
This I changed to:
CMyAppDlg *pdlg = new CMyAppDlg;
m_pMainWnd = pdlg;
INT_PTR nResponse = pdlg->DoModal();
if (nResponse == IDOK)
{}
delete pdlg;
CMyAppDlg dlg1;
m_pMainWnd = &dlg1; // leaving this out makes no difference
nResponse = dlg1.DoModal();// this exits immediately with a -1
if (nResponse == IDOK)
...
The first DoModal() works fine. When I press OK or Cancel, the second DoModal() fails returning -1.
GetLastError
returns, after you see such return value, as described in the documentation? – Algirdas PreidžiusGetLastError
in an MFC application. Please read the documentation again. – IInspectableGetLastError
in an MFC application. – Algirdas PreidžiusCDialog::DoModal
saysGetLastError
gives additional information ifDoModal
returnsIDABORT
. OtherwiseGetLastError
is not useful. In this caseGetLastError
is 0, because there was no Windows error, it's an MFC issue. – Barmak Shemirani