Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. problem using CDialog::Create, assert error

problem using CDialog::Create, assert error

Scheduled Pinned Locked Moved C / C++ / MFC
helpdebuggingquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Luis E Cuadrado
    wrote on last edited by
    #1

    Hello everybody, Here is my problem. I'm trying to display a dialog and in the Create() function I'm getting an assert error. I dig up through the debugger and I found out where I'm getting the assert error is in AfxGetResourceHandle(). Here is my code:

    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
     	// TODO: Place code here.
    	CMainDlg *mainDlg = new CMainDlg(); //Base class is a CDialog
    	BOOL bRet = mainDlg->Create(IDD_MAIN_DIALOG, NULL); //-->I'm getting the assertion error here!
    	if (bRet != 0)
    	{
    		mainDlg->ShowWindow(SW_SHOW);
    	}
    	else if(bRet == 0)
    	{
    		AfxMessageBox("Error Creating Dialog");
    	}
    
    	return 0;
    }
    

    As you noticed, all I'm trying to do is to display a dialog. Is there anything that I have to set in advance before I create the dialog and display it? Any answer is more than welcome. Best regards, Luis E. Cuadrado :0)

    T D R 3 Replies Last reply
    0
    • L Luis E Cuadrado

      Hello everybody, Here is my problem. I'm trying to display a dialog and in the Create() function I'm getting an assert error. I dig up through the debugger and I found out where I'm getting the assert error is in AfxGetResourceHandle(). Here is my code:

      int APIENTRY WinMain(HINSTANCE hInstance,
                           HINSTANCE hPrevInstance,
                           LPSTR     lpCmdLine,
                           int       nCmdShow)
      {
       	// TODO: Place code here.
      	CMainDlg *mainDlg = new CMainDlg(); //Base class is a CDialog
      	BOOL bRet = mainDlg->Create(IDD_MAIN_DIALOG, NULL); //-->I'm getting the assertion error here!
      	if (bRet != 0)
      	{
      		mainDlg->ShowWindow(SW_SHOW);
      	}
      	else if(bRet == 0)
      	{
      		AfxMessageBox("Error Creating Dialog");
      	}
      
      	return 0;
      }
      

      As you noticed, all I'm trying to do is to display a dialog. Is there anything that I have to set in advance before I create the dialog and display it? Any answer is more than welcome. Best regards, Luis E. Cuadrado :0)

      T Offline
      T Offline
      Tim Smith
      wrote on last edited by
      #2

      It has been a long time since I have done MFC, but since you have replaced MFCs WinMain, it has been unable to initialize. What you should have done is display your dialog from the InitInstance method of your CApplication class. Tim Smith I'm going to patent thought. I have yet to see any prior art.

      1 Reply Last reply
      0
      • L Luis E Cuadrado

        Hello everybody, Here is my problem. I'm trying to display a dialog and in the Create() function I'm getting an assert error. I dig up through the debugger and I found out where I'm getting the assert error is in AfxGetResourceHandle(). Here is my code:

        int APIENTRY WinMain(HINSTANCE hInstance,
                             HINSTANCE hPrevInstance,
                             LPSTR     lpCmdLine,
                             int       nCmdShow)
        {
         	// TODO: Place code here.
        	CMainDlg *mainDlg = new CMainDlg(); //Base class is a CDialog
        	BOOL bRet = mainDlg->Create(IDD_MAIN_DIALOG, NULL); //-->I'm getting the assertion error here!
        	if (bRet != 0)
        	{
        		mainDlg->ShowWindow(SW_SHOW);
        	}
        	else if(bRet == 0)
        	{
        		AfxMessageBox("Error Creating Dialog");
        	}
        
        	return 0;
        }
        

        As you noticed, all I'm trying to do is to display a dialog. Is there anything that I have to set in advance before I create the dialog and display it? Any answer is more than welcome. Best regards, Luis E. Cuadrado :0)

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Have you stepped through the Create() code to see why afxCurrentResourceHandle is still NULL?

        L 1 Reply Last reply
        0
        • D David Crow

          Have you stepped through the Create() code to see why afxCurrentResourceHandle is still NULL?

          L Offline
          L Offline
          Luis E Cuadrado
          wrote on last edited by
          #4

          Hi DavidCrow, Yes, I stepped through the Create() code. The assertion is comming in the AfxFindResourceHandle() function. I included the portion of the code where the assert error comes and I pointed out where the assert error occurs. Aparently the value pModuleState->m_bSystem returned by AfxGetModuleState() is causing the assert error. Here is the code.

          HINSTANCE AFXAPI AfxFindResourceHandle(LPCTSTR lpszName, LPCTSTR lpszType)
          {
          	ASSERT(lpszName != NULL);
          	ASSERT(lpszType != NULL);
          
          	HINSTANCE hInst;
          
          	// first check the main module state
          	AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
          	if (!pModuleState->m_bSystem)
          	{
          		hInst = AfxGetResourceHandle(); //-->ASSERT OCCURS HERE!!!
          		if (::FindResource(hInst, lpszName, lpszType) != NULL)
          			return hInst;
          	}
          .
          .
          .
          

          I hope this helps. Before I forget, thanks for your reply David Luis E. Cuadrado :0)

          D 1 Reply Last reply
          0
          • L Luis E Cuadrado

            Hi DavidCrow, Yes, I stepped through the Create() code. The assertion is comming in the AfxFindResourceHandle() function. I included the portion of the code where the assert error comes and I pointed out where the assert error occurs. Aparently the value pModuleState->m_bSystem returned by AfxGetModuleState() is causing the assert error. Here is the code.

            HINSTANCE AFXAPI AfxFindResourceHandle(LPCTSTR lpszName, LPCTSTR lpszType)
            {
            	ASSERT(lpszName != NULL);
            	ASSERT(lpszType != NULL);
            
            	HINSTANCE hInst;
            
            	// first check the main module state
            	AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
            	if (!pModuleState->m_bSystem)
            	{
            		hInst = AfxGetResourceHandle(); //-->ASSERT OCCURS HERE!!!
            		if (::FindResource(hInst, lpszName, lpszType) != NULL)
            			return hInst;
            	}
            .
            .
            .
            

            I hope this helps. Before I forget, thanks for your reply David Luis E. Cuadrado :0)

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            Technically, the assertion is being fired within the inline function AfxGetResourceHandle(), which, in this case, is being called by AfxFindResourceHandle(). Have you tried Tim's suggestion? I've never tried creating an MFC application without the other supporting pieces in place.

            L 1 Reply Last reply
            0
            • D David Crow

              Technically, the assertion is being fired within the inline function AfxGetResourceHandle(), which, in this case, is being called by AfxFindResourceHandle(). Have you tried Tim's suggestion? I've never tried creating an MFC application without the other supporting pieces in place.

              L Offline
              L Offline
              Luis E Cuadrado
              wrote on last edited by
              #6

              I'm going to try Tim's suggestion. As soon as I find something, I will post back the results. Thanks for your replys. Luis E. Luis E. Cuadrado :0)

              1 Reply Last reply
              0
              • L Luis E Cuadrado

                Hello everybody, Here is my problem. I'm trying to display a dialog and in the Create() function I'm getting an assert error. I dig up through the debugger and I found out where I'm getting the assert error is in AfxGetResourceHandle(). Here is my code:

                int APIENTRY WinMain(HINSTANCE hInstance,
                                     HINSTANCE hPrevInstance,
                                     LPSTR     lpCmdLine,
                                     int       nCmdShow)
                {
                 	// TODO: Place code here.
                	CMainDlg *mainDlg = new CMainDlg(); //Base class is a CDialog
                	BOOL bRet = mainDlg->Create(IDD_MAIN_DIALOG, NULL); //-->I'm getting the assertion error here!
                	if (bRet != 0)
                	{
                		mainDlg->ShowWindow(SW_SHOW);
                	}
                	else if(bRet == 0)
                	{
                		AfxMessageBox("Error Creating Dialog");
                	}
                
                	return 0;
                }
                

                As you noticed, all I'm trying to do is to display a dialog. Is there anything that I have to set in advance before I create the dialog and display it? Any answer is more than welcome. Best regards, Luis E. Cuadrado :0)

                R Offline
                R Offline
                Ryan Binns
                wrote on last edited by
                #7

                Simple. In order to use MFC you must initialise it first.

                if(!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
                {
                // MFC failed to initialise properly
                return 1;
                }

                Then your MFC operations will work as normal :). You don't need to call any cleanup functions at the end. Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
                Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups