Windows Applications that Have Access To Console Features


Click here for larger image


I always wanted to use cout, cin, cerr, printf, etc. in windows application if  I started it from command line. There was an article (, contributed Richard Eperjesi) about how the MSDEV can work in console mode (MSDEV /?) and in windows mode (MSDEV). That's a very great idea, and here is another tricky way.


Crete your MFC windows project and add the ConsoleWindowsApp.cpp or this source code to your project, and 
  • Do Define _CONSOLEWIN in your project settings, if you want your project to work like a console and windows application. The standard output, and input will be redirected to the parent console. You can use cout, cerr, cin, printf, etc.
  • Do not define _CONSOLEWIN in your project settings, and you will get a windows application. (Default)

Source code


//Set subsystem to console, just overwrites the default windows subsystem
#pragma comment ( linker, "/subsystem:console" )

BOOL WINAPI ConsoleWinHandlerRoutine( DWORD dwCtrlType )
    // Signal type
    switch( dwCtrlType )
    case CTRL_C_EVENT:
    // You can stop here gracefully:
    // AfxGetMainWnd()->SendMessage( WM_CLOSE, 0, 0 );
    // WaitForSingleObject( AfxGetThread(), INFINITE );

return TRUE;

// Console main function, this code is from WinMainCRTStartup()
int _tmain( DWORD, TCHAR**, TCHAR** )
#define SPACECHAR _T(' ')
#define DQUOTECHAR _T('\"')

    // Set the new handler
    SetConsoleCtrlHandler( ConsoleWinHandlerRoutine, TRUE );

    // Get command line
    LPTSTR lpszCommandLine = ::GetCommandLine();

    if(lpszCommandLine == NULL)
        return -1;

    // Skip past program name (first token in command line).
    // Check for and handle quoted program name.
    if(*lpszCommandLine == DQUOTECHAR)
        // Scan, and skip over, subsequent characters until
        // another double-quote or a null is encountered.
            lpszCommandLine = ::CharNext(lpszCommandLine);
        } while((*lpszCommandLine != DQUOTECHAR) && (*lpszCommandLine != _T('\0')));

        // If we stopped on a double-quote (usual case), skip over it.
        if(*lpszCommandLine == DQUOTECHAR)
            lpszCommandLine = ::CharNext(lpszCommandLine);
        while(*lpszCommandLine > SPACECHAR)
            lpszCommandLine = ::CharNext(lpszCommandLine);

    // Skip past any white space preceeding the second token.
    while(*lpszCommandLine && (*lpszCommandLine <= SPACECHAR))
        lpszCommandLine = ::CharNext(lpszCommandLine);

    STARTUPINFO StartupInfo;
    StartupInfo.dwFlags = 0;


    // Your original windows application will be started
    return _tWinMain(::GetModuleHandle(NULL), NULL, lpszCommandLine,
                (StartupInfo.dwFlags & STARTF_USESHOWWINDOW) ?
          StartupInfo.wShowWindow : SW_SHOWDEFAULT);


Download source code - 2 Kb


  • Re-entering the Windows part

    Posted by dms489 on 03/31/2006 02:54pm

    I am trying (and perhaps I shouldn't) to use this technique to allow a console application to open a window to draw stuff, wait for it to close, then do some more computing and open another window. This second instantiation of MFC dies a quick, horrible death, and I was hoping someone knew how to fix that.

  • How to create more than one Console Window

    Posted by Legacy on 07/25/2003 12:00am

    Originally posted by: H. Descho

    Does anyone know, how to create/show more than one Console application window??

  • nucleus with VC++

    Posted by Legacy on 06/23/2003 12:00am

    Originally posted by: Prashanth sylvester

    if we use nuclues with VC++ is it possible to desin a GUI for the application ..!

  • using cout AND cin

    Posted by Legacy on 02/22/2003 12:00am

    Originally posted by: Mr. Bungle

    This is very useful, however:

    is it possible to redirect cout in a graphical windows program (bc++b6) to a form's text control (e.g. memo control) and be able to use cin to retrieve use input in the control somehow?

    That would really be useful!


  • couldn't open STDAFX.H

    Posted by Legacy on 12/14/2002 12:00am

    Originally posted by: sorokano

    I got this error while compiling:
    >>fatal error C1083: Cannot open include file: 'StdAfx.h': No such file or directory

    where do i get stdafx.h?

  • How to make hybrid GUI/console application truly seamless

    Posted by Legacy on 10/21/2002 12:00am

    Originally posted by: Akif

    This example is doing a useful thing however I should
    say it is not very desirable, since it requires re-compilation between pure GUI and console mode of the same application. Suppose I have a dialog based GUI application, I want to turn it it to a hybrid console application. What I mean by hybrid is: when user starts it from a command line, no GUI or GUI related stuff will be generated (this is my part and I can guarantee that) but I want to see cout, printf etc regular console outputs to be enabled..
    So the whole application should be seamless, between GUI and console mode. The example here always first creates a parent console, it is disturbing view when you only want GUI, and you don't want to re-compile.

  • can you help me

    Posted by Legacy on 07/26/2002 12:00am

    Originally posted by: jianhua qu

    I didn't know how to use this ,and I hope you can send me a demo.

  • Very Useful Program, but I can't stop it

    Posted by Legacy on 05/06/2002 12:00am

    Originally posted by: nomadeous

    This program is very useful for me,
    Thanks Zoltan.

    But I have a problem :
    I added _CONSOLEWIN to my project's settings like you told.
    But when I launch my prog, it works good, the cout messages appear in the console. But when I leave my prog by a PostQuitMessage(0), the console stays on.

    Please could you help me ?

  • Eeeeeexcellent, Smithers!

    Posted by Legacy on 02/11/2002 12:00am

    Originally posted by: Migraine

    Well done. Simple and functional.

  • Does not work

    Posted by Legacy on 02/07/2002 12:00am

    Originally posted by: Feroz Zahid

    The piece of code that you have provided to us doesn't work.
    Please try to make it less error prone.

  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

  • Enterprises are increasingly looking to platform as a service (PaaS) to lower their costs and speed their time to market for new applications. Developing, deploying, and managing applications in the cloud eliminates the time and expense of managing a physical infrastructure to support them. PaaS offerings must deliver additional long-term benefits, such as a lower total cost of ownership (TCO), rapid scalability, and ease of integration, all while providing robust security and availability. This report …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date