简体   繁体   中英

Why does MFC DoModal returns -1 ? What does -1 mean?

I am very new to MFC and have been asked to create a MFC application in Visual Studio 2008. I am trying to Create two dialog. The first one opens on launch and if OK is pressed on the first one, the second dialog opens. However my first dialog opens properly but the second one returns -1 when I call DoModal(). Can anybody please let me know what am I doing wrong ? The -1 according to MSDN documentation is "something has gone wrong". I couldn't figure out what I am doing wrong.

// The main file from where the dialogs are launched - Encrypt.cpp
#include "stdafx.h"
#include "Encrypt.h"
#include "MainDialog.h"
#include "AddlDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

BEGIN_MESSAGE_MAP(CEncryptApp, CWinApp)
 ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()

CEncryptApp::CEncryptApp()
{

}

CEncryptApp theApp;

BOOL CEncryptApp::InitInstance()
{
    CWinApp::InitInstance();
    AfxEnableControlContainer();
    AfxInitRichEdit();

    CMainDialog dlg;
    INT_PTR nResponse = dlg.DoModal();

    if (nResponse == IDOK)
    {
        CAddlDlg ldg;
        INT_PTR nResponse = ldg.DoModal();

        switch (nResponse)
        {
            case -1: 
                AfxMessageBox(_T("-1"));
                break;
            case IDABORT:
                AfxMessageBox(_T("1!")); 
                break;
            case IDOK:
                AfxMessageBox(_T("2!")); 
                break;
             case IDCANCEL:
                AfxMessageBox(_T("3!"));  
                break;
             default:
                AfxMessageBox(nResponse);  
             break;
        };


    }
    else if (nResponse == IDCANCEL)
    {

    }

return FALSE;
}

This is the main dialog

// Main Dialog

#include "stdafx.h"
#include "Encrypt.h"
#include "MainDialog.h"

#ifdef _DEBUG
 #define new DEBUG_NEW
#endif


IMPLEMENT_DYNAMIC(CMainDialog, CDialog)

CMainDialog::CMainDialog(CWnd* pParent /*=NULL*/)
     : CDialog(CMainDialog::IDD, pParent)
 {
      m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  }

 CMainDialog::~CMainDialog()
 {
 }

void CMainDialog::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
 }

BEGIN_MESSAGE_MAP(CMainDialog, CDialog)
   ON_WM_PAINT()
   ON_WM_QUERYDRAGICON()
   //}}AFX_MSG_MAP
END_MESSAGE_MAP()

  BOOL CMainDialog::OnInitDialog()
  {
     CDialog::OnInitDialog();

   SetIcon(m_hIcon, TRUE);          // Set big icon
   SetIcon(m_hIcon, FALSE);     // Set small icon
    return TRUE;  // return TRUE  unless you set the focus to a control
 }

 void CMainDialog::OnPaint()
 {
    if (IsIconic())
    {
        CPaintDC dc(this); // device context for painting

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

        // Center icon in client rectangle
        int cxIcon = GetSystemMetrics(SM_CXICON);
       int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;
        dc.DrawIcon(x, y, m_hIcon);
   }
   else
   {
       CDialog::OnPaint();
    }
}

 HCURSOR CMainDialog::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(m_hIcon);
 }

// The second dialog

#include "stdafx.h"
#include "Encrypt.h"
#include "AddlDlg.h"

IMPLEMENT_DYNAMIC(CAddlDlg, CDialog)
CAddlDlg::CAddlDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CAddlDlg::IDD, pParent)
{      // Reaches until here
}

CAddlDlg::~CAddlDlg()
{
}

void CAddlDlg::DoDataExchange(CDataExchange* pDX)
{
   CDialog::DoDataExchange(pDX);
}

BOOL CAddlDlg::OnInitDialog() 
{
    CDialog::OnInitDialog();
    return TRUE;
} 

BEGIN_MESSAGE_MAP(CAddlDlg, CDialog)
END_MESSAGE_MAP()

When I try printing debug statements, I enter into the constructor of the second dialog but the OnInitDialog never gets called. Can someone please help me?

**UPDATE :: **

The error I see on further debugging says it is on line 311 in dlgcore.cpp in the function ::CreateDialogIndirect with the actual error is

::CreateDialogIndirect() did NOT create the window (ie. due to error in template) and returns NULL.

I do not what that means. Can some one explain me ?

From MSDN site:

The return value is –1 if the function could not create the dialog box, or IDABORT if some other error occurred, in which case the Output window will contain error information from GetLastError.

Error citation can be found here: http://msdn.microsoft.com/en-us/library/619z63f5.aspx

The system is unable to create and run your dialog. You can read the provided MSDN link for more information.

It is likely you have a control or DLL that is not registered correctly that the dialog needs and therefore cannot find.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM