using MFC in a console app..how?

I want to create a console application, but I would also like to use some of the features that are in MFC.

I linked my console app with #include <afx.h> and calls to MFC classes successfully. However on run, it crashes immediately (before even my main() function). I think I recall something about winmain()? ??
gunnAsked:
Who is Participating?
 
jstolanConnect With a Mentor Commented:
I'm running one now by including stdafx.h in the main cpp file.  Do not use winmain() for your main program use main().

Make sure to add a stdafx.cpp to the project and link it in.  My stdafx.cpp only has one line.

#include "stdafx.h"

My stdafx.h looks like this

// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
//

#if !defined(AFX_STDAFX_H__791E939E_5C6B_11D2_9423_080009ACC8C1__INCLUDED_)
#define AFX_STDAFX_H__791E939E_5C6B_11D2_9423_080009ACC8C1__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

#define VC_EXTRALEAN            // Exclude rarely-used stuff from Windows headers

#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions
#include <afxdisp.h>        // MFC OLE automation classes
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h>                  // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT


// This macro is the same as IMPLEMENT_OLECREATE, except it passes TRUE
//  for the bMultiInstance parameter to the COleObjectFactory constructor.
//  We want a separate instance of this application to be launched for
//  each OLE automation proxy object requested by automation controllers.
#ifndef IMPLEMENT_OLECREATE2
#define IMPLEMENT_OLECREATE2(class_name, external_name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
      AFX_DATADEF COleObjectFactory class_name::factory(class_name::guid, \
            RUNTIME_CLASS(class_name), TRUE, _T(external_name)); \
      const AFX_DATADEF GUID class_name::guid = \
            { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } };
#endif // IMPLEMENT_OLECREATE2

//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__791E939E_5C6B_11D2_9423_080009ACC8C1__INCLUDED_)

0
 
gunnAuthor Commented:
Thanks for the reply. I just tried it, by copying your stdafx.h file and making a stdafx.cpp file as you say. So, my project has 2 files, main.cpp and stdafx.cpp . Some reason, that didn't work.  By compiling the stdafx.h or main.cpp, I get 22 errors that are in the .h file.

What kind of project workspace should I have? Win32 Console Application, MFC AppWizard, or other? Currently, i have console app. MFC AppWizard seems like it'll only make GUI apps...

Thanks,
Tom
0
 
stefanrCommented:
1) Change the selection in the Project Settings/General/Microsoft Foundation Classes list box to Use MFC in a Shared DLL (that is what I have used, and I know it's work).

2) Write the following skeletal code:

#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers

#include <afxwin.h>
#include <afxext.h>

#include <iostream.h>

// #include any other headers necessary

CWinApp theApp; // Just to keep certain MFC classes happy.

int main()
{
   if (!::AfxWinInit((::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
   {
      cerr << "Can not initialize MFC!" << endl;
      return EXIT_FAILURE;
   }

   CWnd wndMain; // Dummy CWnd to keep certain MFC classes happy.
   theApp.m_pMainWnd = &wndMain;

   ... // Use any MFC classes (except CWnd, and derivatives).

   return EXIT_SUCCESS;
}

0
 
gunnAuthor Commented:
Thanks for the reply. I just tried it, by copying your stdafx.h file and making a stdafx.cpp file as you say. So, my project has 2 files, main.cpp and stdafx.cpp . Some reason, that didn't work.  By compiling the stdafx.h or main.cpp, I get 22 errors that are in the .h file.

What kind of project workspace should I have? Win32 Console Application, MFC AppWizard, or other? Currently, i have console app. MFC AppWizard seems like it'll only make GUI apps...

Thanks,
Tom
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.