how do I write to stdout from an MFC program?

23,671

Solution 1

Use AllocConsole function to create a console for writing into. The following article explains how to use it to print to console.

Creating a console for your MFC app's debug output

Dont forget to FreeConsole once you're done with it.

Solution 2

If you are just looking for output to the debug window, you can use TRACE.

TRACE("This is a debug string of text in MFC");

I do this when I am quickly testing something and don't want to use dialog boxes, like MessageBox("text").

Solution 3

This will attach to the calling console window, if one is present. GotConsoleAttach will be FALSE when the application wasn't called from a console.

GotConsoleAttach = FALSE;    
if (AttachConsole(ATTACH_PARENT_PROCESS))
{   
    int osfh = _open_osfhandle((intptr_t) GetStdHandle(STD_OUTPUT_HANDLE), 8);
    if ((HANDLE)osfh != INVALID_HANDLE_VALUE)
    {
        *stdout = *_tfdopen(osfh, _T("a"));
        GotConsoleAttach = TRUE;
    }
}

Solution 4

Here's a one-liner that I found online a while back that attaches stdout to a console in MFC. This allows printf and cout to write to the console window of the current process. I never looked into how it works, so if you need a cerr or cin version you're on your own.

AllocConsole();
*stdout = *_tfdopen(_open_osfhandle((intptr_t) GetStdHandle(STD_OUTPUT_HANDLE), _O_APPEND), _T("a"));

Solution 5

After spending an entire day trying to make my MFC program to print using printf() and cout I finally found a solution and decide to post it here to help who wants to print at MFC...

void EnablePrintfAtMFC()
{
    if (AttachConsole(ATTACH_PARENT_PROCESS))
    {
        FILE* pCout;
        freopen_s(&pCout, "CONOUT$", "w", stdout);
        std::cout.clear();
        std::wcout.clear();
    }
}

Just call the above function in some place of your program, and after that you will be able to use printf() and cout...

EnablePrintfAtMFC();
printf("Hello world!\n");
std::cout << "It works!" << endl;
Share:
23,671

Related videos on Youtube

Stephen Kellett
Author by

Stephen Kellett

Software engineer at Software Verification Limited. Into folk music, bagpipes, mandolins, french dancing, swimming, walking, mountains, technology, marketing. I suffer from RSI, and have lived with it since 1994. I'm one of a few people I've met that have kept their careers after being effectively disabled by the injury. If you have any problems with RSI, you would do well to heed some of my advice on Repetitive Strain Injury The RSI also affected my music making. I hardly play guitar now (too much stretching of the hands). I replaced that with Mandolin and found that I can automatically knock out really good Irish sounding Jigs. But that didn't last - also bad for my hands. Then I found, to my amazement that bagpipes work for me (but whistles do not). I found bagpipes through French dancing. If you live near Ely, learn some French Music and come and play with us.

Updated on July 09, 2022

Comments

  • Stephen Kellett
    Stephen Kellett almost 2 years

    MFC programs can't normally write to stdout. MFC does something weird with the stdout/stdin pipes during startup and anything you write (for example doing a printf("hello");) just goes to /dev/null.

    Does anyone know how to successfully write to stdout from an MFC program?

    Thanks for reading.

  • mateuscb
    mateuscb over 9 years
    This worked great for me. On a side note, since this is a windows app, I had used START /WAIT myapp.exe in order for it to work correctly.
  • Stephen Kellett
    Stephen Kellett over 9 years
    This is not answering the question I asked.
  • opetroch
    opetroch about 7 years
    Thanks, this worked for me. I needed to include <fcntl.h>.
  • Jesse
    Jesse about 5 years
    This was the only one that worked for me out of all of the above answers - so thanks for posting it! :)
  • 4LegsDrivenCat
    4LegsDrivenCat over 4 years
    This crashes on Windows 10.
  • 4LegsDrivenCat
    4LegsDrivenCat over 4 years
    This crashes on Windows 10.
  • 4LegsDrivenCat
    4LegsDrivenCat over 4 years
    This works even on Windows 10! And it works for any GUI Windows application so you can rename it to "EnablePrintfAtGUI" :)

Related