17

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.

Stephen Kellett
  • 3,078
  • 1
  • 22
  • 25

5 Answers5

14

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").

JasonH
  • 589
  • 9
  • 18
14

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;
Joao Louzada
  • 183
  • 1
  • 10
  • 1
    This was the only one that worked for me out of all of the above answers - so thanks for posting it! :) – Jesse Jan 24 '19 at 05:35
  • 1
    This works even on Windows 10! And it works for any GUI Windows application so you can rename it to "EnablePrintfAtGUI" :) – 4LegsDrivenCat Dec 13 '19 at 23:57
12

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.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • This is almost what I want, but you are creating a new console. I was using AttachToConsole(ATTACH_PARENT_PROCESS) and getting nothing. But the solution is _cprintf() which you have given me. Hence you get the points. Thanks! – Stephen Kellett Feb 23 '11 at 17:40
  • @Stephen: Probably you also want to see [AttachConsole](http://msdn.microsoft.com/en-us/library/ms681952(v=vs.85).aspx) function. It's a bit different than what you were experimenting with. – Nawaz Feb 23 '11 at 17:46
  • This creates a new console window when I call my EXE from a command line. How to write the output to same console window? – Lakshman Rao Sep 29 '18 at 18:56
10

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;
    }
}
noelicus
  • 14,468
  • 3
  • 92
  • 111
9

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"));
John K
  • 496
  • 3
  • 14