How to make a CDialog?

1.9k views Asked by At

I have tried multiple things but the base comes to this:

#include <stdio.h>
#include <afxwin.h>


main( int argc, const char* argv[] )
{

   printf( "\nHello World\n\n" );

   CDialog *dlg = new CDialog();
   dlg->DoModal();

   while (true) {
      Sleep(1); // Sleep is a windows function
   }
}

When I run this, I get the following error:

Debug Assertion Failed Message

What am I missing for this dialog?

I looked up several resources, but everything results in the same error message.

Can someone tell me what am I not seeing?

1

There are 1 answers

2
xMRi On

Using the MFC in a console application requires some initializations. Without this you will get asserts.

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    // initialize MFC and print and error on failure
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        // TODO: change error code to suit your needs
        _tprintf(_T("Fatal Error: MFC initialization failed\n"));
        return 8;
    }

You must also use a resource that is bound to the CDialog. You may use the appropriate constructors. Or you derive your own dialog from CDialog using the class wizard.

But it doesn't make sense to me to create an MFC console application and use dialogs... Your question may need more details, what you want to do, and why you want to do it in this way.

You may need to read some books or article before you continue this way of programming.