Search code examples
c++mfccdialog

A Dialog derived from CDialog returns -1 after DoModal (MFC/C++)


I have created a Dialog which it's derived from CDialog (MFC does this automatically), but I had to override the OnInitDialog method to make initialization of a ComboBox:

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

 InitCommonControls();

 if ( FillInCombo() != 0 )
  //erro ao tentar ler leitoras
  return TRUE;

 return TRUE;
}

The method FillInCombo calls a PCSC class to fill this combo with all available smart card readers.

However, while debugging a strange behavior takes place. While attempting to select an item on the combo, it closes automatically (it actually generates an OnClose event) and DoModal returns -1.

Then, next, the application is never again loaded correctly unless I clean the solutions and build it again.

 CLogin SmartNetData;
 int ret = SmartNetData.DoModal();
 switch ( ret )
 {
    case IDOK:
    break;
    case -1: 
    // strange error
    OnOK();
    return TRUE;
    case IDABORT:
    case IDCANCEL:
    OnOK();
    return TRUE;
 };

Thanks for any possible help on this.

LATER EDIT: I realized the problem arises because of the combobox. I am not sure why. I replaced it by a ListBox and I am not getting troubles.


Solution

  • I suspect some thing goes wrong in your OnInitDialog().

    I suggest you to go for debug steps:
    a) remove InitCommonControls() in OnInitDialog()
    b) remove FillInCombo() in OnInitDialog()
    c) remove InitCommonControls() and FillInCombo() in OnInitDialog()

    After removing one by one, check with the DoModal returns value.