Solved

Calling DoModal() and getting -1

Posted on 2004-08-05
16
690 Views
Last Modified: 2013-11-20
Hello again. I am setting up a Dialog based application. I am using the applications Main Dialog to display the introduction and title of the program, then when the user presses OK...I set up my property sheet and display the page..the problem is...when I go to DoModal for that page...I am not having successful results. Can someone tell me why? Here is the code snippet:

BOOL CAlchemyIntTestApp::InitInstance()
{
      // InitCommonControls() is required on Windows XP if an application
      // manifest specifies use of ComCtl32.dll version 6 or later to enable
      // visual styles.  Otherwise, any window creation will fail.
      InitCommonControls();

      CWinApp::InitInstance();

      // Standard initialization
      // If you are not using these features and wish to reduce the size
      // of your final executable, you should remove from the following
      // the specific initialization routines you do not need
      // Change the registry key under which our settings are stored
      // TODO: You should modify this string to be something appropriate
      // such as the name of your company or organization
      SetRegistryKey(_T("Local AppWizard-Generated Applications"));

      CAlchemyIntTestDlg dlg;
      m_pMainWnd = &dlg;
      INT_PTR nResponse = dlg.DoModal();
      if (nResponse == IDOK)
      {
            CCheckExpPage cePage;
            CImportFormPage ifPage;
            CTestInfoPage tstinfPage;
            CVerifyFilesPage vfPage;
            CVerifyTFPage vtfPage;

            //Begin Wizard Sequence
            CPropertySheet prpshtMain;
            prpshtMain.AddPage(&tstinfPage);
            prpshtMain.AddPage(&vtfPage);
            prpshtMain.AddPage(&vfPage);
            prpshtMain.AddPage(&cePage);
            prpshtMain.AddPage(&ifPage);

            prpshtMain.SetWizardMode();
            prpshtMain.DoModal();    ///Getting -1 here
            
      }
      else if (nResponse == IDCANCEL)
      {
            // TODO: Place code here to handle when the dialog is
            //  dismissed with Cancel
      }

      // Since the dialog has been closed, return FALSE so that we exit the
      //  application, rather than start the application's message pump.
      return FALSE;
}
0
Comment
Question by:SGyves
  • 8
  • 8
16 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11730548
You don't have to invoke DoModal inside of InitInstance, because it will stop the application's initilization process. Normally a modeless dialog is used. If you want to make your dialog to lauch another dialog, use a 3rd button (not IDOK or IDCANCEL) and control the WM_COMMAND message inside your dialog.
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 11730770
About my last line:
Go to dialog editor and add a button (Let's say "Proceed", id= ID_PROCEED). If you want, eliminate Ok button.
Go to ClassWizard (Ctrl-W) and add a handler by choosing CAlchemyIntTestDlg, ID_PROCEED identifier, and BN_CLICKED event, press "Add function" and name it OnProceed, press "Edit code" button, and inside OnProceed function, copy all your PropertySheet code, up to SetWizandMode and DoModal. You can close main dialog after that by invoking OnOk() or OnCancel().
0
 

Author Comment

by:SGyves
ID: 11730827
I also find that it does work if I comment out m_pMainWnd = &dlg...but then only the first page of my PropSheet displays...after hitting the Next button....the app fails again
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

Author Comment

by:SGyves
ID: 11730874
Ok...I am setting it up now...I will let you knwo how it goes.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11730894
>I also find that it does work if I comment out m_pMainWnd = &dlg...but then only the first page of my PropSheet displays...after hitting the Next button....the app fails again

That's because you application has not properly initialized, and some MFC internal context variables are still pointing to NULL. It is a bad trick.
0
 

Author Comment

by:SGyves
ID: 11730895
If I do it like that...then the original window is still open somewhere...I do not want that. I at the very least do not want it visible.
0
 

Author Comment

by:SGyves
ID: 11730911
Also...I am still getting invalid address messages when I hit Next on the first property page.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11730937
>I at the very least do not want it visible.
You can reposition main dialog (outside the screen) before calling Property Sheet, with SetWindowPos(), I dont recommend you to hide dialog because PropertySheet can disappear, but I am not sure about that (if you dont make it main dialog's child).
0
 

Author Comment

by:SGyves
ID: 11731013
I invoke OnOK() in the Continue handler...and that seems to get rid of the Original dialog. But for some reason...only the first page of my propertysheet list appears. As soon as I hit Next...I get Access Violations:

First-chance exception at 0x77f1850d in AlchemyIntTest.exe: 0xC0000005: Access violation writing location 0x00434d74.
First-chance exception at 0x00000030 in AlchemyIntTest.exe: 0xC0000005: Access violation reading location 0x00000030.
Unhandled exception at 0x00000030 in AlchemyIntTest.exe: 0xC0000005: Access violation reading location 0x00000030.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11731034
You have to invoke OnOk() AFTER PropertyDialog has closed, becuase you have created propertyPages within dialog, if you destroy main dialog, you destroy propertyPages objects.
0
 

Author Comment

by:SGyves
ID: 11731173
Ok....I had something major wrong with my stuff...I had to go back and redo all of my inheriting for my property pages. They were all inheriting from OlePropertyPage. OOOOppssss. So I deleted those classes and redid them...now all is well. You answered my original question so that is cool. I just wish I could get rid of the original window in a neater way.
0
 

Author Comment

by:SGyves
ID: 11731234
What is wrong with doing this???

BOOL CAlchemyIntTestApp::InitInstance()
{
      // InitCommonControls() is required on Windows XP if an application
      // manifest specifies use of ComCtl32.dll version 6 or later to enable
      // visual styles.  Otherwise, any window creation will fail.
      InitCommonControls();

      CWinApp::InitInstance();

      // Standard initialization
      // If you are not using these features and wish to reduce the size
      // of your final executable, you should remove from the following
      // the specific initialization routines you do not need
      // Change the registry key under which our settings are stored
      // TODO: You should modify this string to be something appropriate
      // such as the name of your company or organization
      SetRegistryKey(_T("Local AppWizard-Generated Applications"));

      

      CAlchemyIntTestDlg dlg;
      
      
      INT_PTR nResponse = dlg.DoModal();
      if (nResponse == IDOK)
      {
            CCheckExpPage cePage;
            CImportPage ifPage;
            CTestInfoPage tstinfPage;
            CVerifyFilesPage vfPage;
            CVerifyTFPage vtfPage;

            //Begin Wizard Sequence
            CPropertySheet prpshtMain;
      
            prpshtMain.AddPage(&tstinfPage);
            prpshtMain.AddPage(&vtfPage);
            prpshtMain.AddPage(&vfPage);
            prpshtMain.AddPage(&cePage);
            prpshtMain.AddPage(&ifPage);

            prpshtMain.SetWizardMode();
            m_pMainWnd = &prpshtMain;
            prpshtMain.DoModal();
            
      }
      else if (nResponse == IDCANCEL)
      {
            // TODO: Place code here to handle when the dialog is
            //  dismissed with Cancel
      }

      // Since the dialog has been closed, return FALSE so that we exit the
      //  application, rather than start the application's message pump.
      return FALSE;
}
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11731281
> So I deleted those classes and redid them...now all is well.
Yes! Good choice! Most members refuse to start again the job, but they don't realize sometimes it is the best decision. In spanish we say something like: "Lazy man works twice".
Thanks,
Jaime.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11731323
I think this way could work, very interesting...
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11731379
could be better if you leave PropertyDialog as modeless.
I can't examine all MFC internals but as documented, you have to return TRUE "soon" to start your app correctly. CWinApp::InitInstances does nothing, just return TRUE.
0
 

Author Comment

by:SGyves
ID: 11731462
Well...if it is just a Dialog app...once InitInstance is done...I don't want to do anything else. Because all of my stuff will have been completed by the time Initinstance is done.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Get filename and folder into excel 7 79
FizzBuzz challenge 9 82
My project did see openJDK that I installed. What could be the problem 7 152
Increment column based of a FK 8 31
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question