No cout from wxWidget application, but with Eclipse it works fine

my wxWidget Application does not do any std::cout << "xyz" ... on the Windows console (Windows XP) when it starts from the console, for example: "call MyApplication.exe". He will give nothing at all. The app instead gets up right and works great. All buttons and widgets in the frame perform their functions.

When I launch my application from Eclipse, it displays its results, as it does on the Eclipse console.

So why can't I see the output on the Windows console? What do I need to activate?

+4
source share
2 answers

I was always curious about this, so I followed the links provided in Beau Persson, the answer, and put together some code. To use it, simply define the 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(); } 
+3
source

By default, the window application does not have a console. You can create it if you want it to be alone.

See the answers to this question:

Visual C ++ Console Enable

When you are working in an IDE, the IDE often does this for you.


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

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

+2
source

All Articles