4

my wxWidget Application does not make any std::cout << "xyz" ... on a windows console (Windows XP) when it is startet from a console by e.g.: "call MyApplication.exe". It will produce no output at all. The Application instead rises correctly and works fine. All Buttons and Widgets on the Frame have their functions working.

When I run my application from Eclipse, it produces its outputs as it should be to Eclipse' console.

So, why i can't see any output on windows console? What do i have to activate?

Benjamin
  • 1,143
  • 1
  • 14
  • 29
  • Generally an app is either a console app or a windowed app - never both. I hope you find someone with experience in this area, because it's going to be rare. – Mark Ransom Jul 27 '11 at 14:49

2 Answers2

3

I've always been curious about this, so I followed the links provided in Bo Persson's answer and pieced together some code. To use it, just define a UseConsole object in main.

UseConsole.h:

class UseConsole
{
public:
    UseConsole();
    ~UseConsole();
private:
    bool    m_good;
};

UseConsole.cpp:

#include <windows.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <iostream>
#include <fstream>

#include "UseConsole.h"

// The following function is taken nearly verbatim from
// http://www.halcyon.com/~ast/dload/guicon.htm
void RedirectIOToConsole()
{
    int hConHandle;
    long lStdHandle;
    FILE *fp;

    // redirect unbuffered STDOUT to the console
    lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
    hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
    fp = _fdopen( hConHandle, "w" );
    *stdout = *fp;
    setvbuf( stdout, NULL, _IONBF, 0 );

    // redirect unbuffered STDIN to the console
    lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
    hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
    fp = _fdopen( hConHandle, "r" );
    *stdin = *fp;
    setvbuf( stdin, NULL, _IONBF, 0 );

    // redirect unbuffered STDERR to the console
    lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
    hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
    fp = _fdopen( hConHandle, "w" );
    *stderr = *fp;
    setvbuf( stderr, NULL, _IONBF, 0 );

    // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog
    // point to console as well
    std::ios::sync_with_stdio();
}

UseConsole::UseConsole()
{
    m_good = !!AttachConsole(ATTACH_PARENT_PROCESS);
    if (m_good)
        RedirectIOToConsole();
}

UseConsole::~UseConsole()
{
    if (m_good)
        FreeConsole();
}
Community
  • 1
  • 1
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • I tried this example together with some output:`std::cout << "Test output to cout" << std::endl; std::cerr << "Test output to cerr" << std::endl; std::clog << "Test output to clog" << std::endl;` The output displayed on console was: `Test output to cerr Test output to clog` Where is std::cout hiding?? A fprintf-output works fine ... i'm confused – Benjamin Jul 29 '11 at 11:43
  • Finally accepted your answer as it works on other computers but mine. Bad for me, but you're right with your example. Thanks – Benjamin Sep 13 '11 at 09:50
2

A windowed application by default doesn't have a console. You can create one if you like to have one anyway.

See the answers to this question:

Visual C++ Enable Console

When running in an IDE, the IDE often does that for you.


If you already have a console window open, you can alternatively attach to the parent process' console using AttachConsole(ATTACH_PARENT_PROCESS).

http://msdn.microsoft.com/en-us/library/ms681952(v=vs.85).aspx

Community
  • 1
  • 1
Bo Persson
  • 90,663
  • 31
  • 146
  • 203