开发者

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

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?


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();
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜