Run MFC app as a console app

Flaviu_ 1,031 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?

Developer technologies C++
{count} votes

Accepted answer
  1. RLWA32 49,536 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 49,536 Reputation points
    2024-03-28T09:40:09.45+00:00

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.