Run MFC app as a console app

Flaviu_ 911 Reputation points
2024-03-28T09:06:53.2033333+00:00

I have a SDI MFC app. CView based on CRichEditView, where I print some messages. Nothing fancy. Is there possible to run this SDI MDC app as console app, without GUI, but dispatching messages to console instead of CRichEditView?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,527 questions
{count} votes

Accepted answer
  1. RLWA32 40,286 Reputation points
    2024-03-28T15:03:18.79+00:00

    Here is another way to "convert" an MFC SDI application into a console application.

    Code for InitInstance -

    	if (m_lpCmdLine && *m_lpCmdLine)
    	{
    		puts("This is the console interface");
    		puts("The MFC main window has not been created");
    		puts("Hit a key to exit the program");
    		_getch();
    
    		return FALSE;  // exit the program
    	}
    	else
    	{
    		ShowWindow(GetConsoleWindow(), SW_HIDE);
    	}
    

    Code for ExitInstance to restore console if hidden -

    int CMFCSDIconsoleApp::ExitInstance()
    {
    	//TODO: handle additional resources you may have added
    
    	ShowWindow(GetConsoleWindow(), SW_SHOW);
    ....
    

    After building the MFC SDI app use the VC++ EDITBIN utility to change the subsystem --

    Editbin


1 additional answer

Sort by: Most helpful
  1. RLWA32 40,286 Reputation points
    2024-03-28T09:40:09.45+00:00