Solved

Calling DoModal() and getting -1

Posted on 2004-08-05
16
679 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
 

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
xyBalance chalenge 58 88
if loop in java 3 114
Use of condition with 'serial' in ansible 2 37
Making an alias 7 54
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now