2011-07-27 16 views
4

我的wxWidget應用程序不會在Windows控制檯(Windows XP)上作出任何std::cout << "xyz" ...當它從控制檯啓動例如:「call MyApplication.exe」 。它根本不會產生任何輸出。應用程序反而正確地上升並且工作正常。框架上的所有按鈕和小部件都具有其功能。從wxWidget應用程序沒有cout,但與Eclipse工作正常

當我從Eclipse運行我的應用程序時,它產生的輸出應該是Eclipse的控制檯。

那麼,爲什麼我不能在Windows控制檯上看到任何輸出?我必須激活什麼?

+0

通常一個應用程序或者是一個控制檯應用程序或窗口應用程序 - 不可能兼顧。我希望你找到有這方面經驗的人,因爲這很少見。 –

回答

3

我一直好奇,所以我跟着Bo Persson's answer提供和拼湊一些代碼的鏈接。要使用它,只需在main中定義一個UseConsole對象。

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

我試着這個例子一起輸出:'std :: cout <<「Test output to cout << << std :: endl; \t std :: cerr <<「測試輸出到cerr」<< std :: endl; \t的std ::堵塞<< 「測試輸出堵塞」 <<的std :: ENDL;' 控制檯上顯示的輸出是: '測試輸出CERR 測試輸出clog' 哪裏的std :: cout隱藏? 一個fprintf輸出工作正常......我很困惑 – Benjamin

+0

@Benjamin,我也很困惑,因爲'cout'爲我工作。 –

+0

終於接受了你的答案,因爲它適用於其他電腦,但我的。對我不好,但你對你的榜樣是正確的。謝謝 – Benjamin

2

窗口應用程序默認情況下沒有控制檯。無論如何,你可以創建一個。

看到這個問題的答案:

Visual C++ Enable Console

在IDE中運行時,IDE常常會替你。


如果你已經有一個控制檯窗口打開,則可以選擇連接到使用AttachConsole(ATTACH_PARENT_PROCESS)父進程的控制檯。

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

+0

我認爲這個問題涉及到一個應用程序*從控制檯啓動*。創建第二個控制檯將會適得其反。 –

+0

啊,我錯過了。添加了一個到另一個功能的鏈接。 –

相關問題