MFC Application and stdout

If possible, what is the best way to write to standard output (or standard error) from a VS C++ MFC Windows Application I want to be able to print a a few lines of text to standard output when the application is exiting. This would be useful when the application is invoked from the DOS Command prompt and the application outputs a few lines of text that could be viewed in the command prompt window (or redirected to a text log file).

Answer this question

MFC Application and stdout

  • CyTom

    Does using the /SUBSYSTEM:CONSOLE (linker->system->subsystem) setting impose any undesireable limitations (memory, processor performance, etc.) on the C++ Window MFC Application   Are there any tradeoffs compared to using the windows subsystem Thanks.


  • Lao K

    As far as I know, all /SUBSYSTEM:CONSOLE does is initialize the CRT to use a console; it is a different code path from the kernel to your executable's entry point.  It doesn't preclude you from using any Windows APIs.   

    Nobody likes to make definitive statements about performance because it's not an exact science and often based on assumptions out of our know.  But you don't have anything to worry about in terms of resource consumption by changing the subsystem type in this way.

    (Besides, any possible difference in perf is overwhelmed by the fact you're using MFC.)

    Brian

  • Phizz

    Here's a start.  I can't guarantee that this will work for all MFC apps because of possible effects of a different CRT initialization.  This assumes that _UNICODE is defined.

    1. Change the linker subsystem type to /SUBSYSTEM:CONSOLE (linker->system->subsystem)

    2. Override the entry point to what it was before: wWinMainCRTStartup.  (linker->advanced->entry point)

    3. Create a new cpp file with:
    #include "stdafx.h"
    void MyWriteConsole( CString& str )
    {
       fwprintf( stdout, str.GetString() );
    }

    4. Add the prototype extern void MyWriteConsole( CString& str ); at the end of stdafx.h.



  • MFC Application and stdout