Solved

using MFC in a console app..how?

Posted on 1998-10-20
4
424 Views
Last Modified: 2013-11-25
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()? ??
0
Comment
Question by:gunn
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
jstolan earned 50 total points
ID: 1323570
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
 

Author Comment

by:gunn
ID: 1323571
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
 
LVL 3

Expert Comment

by:stefanr
ID: 1323572
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
 

Author Comment

by:gunn
ID: 1323573
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

911 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

20 Experts available now in Live!

Get 1:1 Help Now