2015-08-24 66 views
5

我嘗試這樣的代碼輸出到控制檯:輸出從一個Win32 GUI應用程序控制臺在Windows 10

#include <Windows.h> 

#include <stdio.h> 
#include <io.h> 
#include <fcntl.h> 

int APIENTRY WinMain(HINSTANCE hInstance, 
        HINSTANCE hPrevInstance, 
        LPSTR  lpCmdLine, 
        int  nCmdShow) 
{ 
    AllocConsole(); 

    HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); 
    int hCrt = _open_osfhandle((long) handle_out, _O_TEXT); 
    FILE* hf_out = _fdopen(hCrt, "w"); 
    setvbuf(hf_out, NULL, _IONBF, 1); 
    *stdout = *hf_out; 

    HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE); 
    hCrt = _open_osfhandle((long) handle_in, _O_TEXT); 
    FILE* hf_in = _fdopen(hCrt, "r"); 
    setvbuf(hf_in, NULL, _IONBF, 128); 
    *stdin = *hf_in; 

    printf("Hello!"); 
} 

控制檯打開,但沒有什麼是輸出到它。該代碼有什麼問題?

我嘗試了所有這些建議:

https://justcheckingonall.wordpress.com/2008/08/29/console-window-win32-app/

http://dslweb.nwnexus.com/~ast/dload/guicon.htm

How do I print to the debug output window in a Win32 app?

,但我無法得到任何輸出中緊跟在Windows 10 AllocConsole()創建控制檯。 注:我其實並沒有創建任何真正的Window。 在Window 10中發生了什麼變化,導致上述解決方案無法正常工作,或者是否有可能丟失的東西(編譯器標誌或其他東西)?

您認爲如何?

+1

請不要在非現場鏈接提問您的問題。請顯示您嘗試的代碼。你會發生什麼,發生了什麼。就目前而言,這個問題不在話下。 –

+0

當我這樣做,我會得到50個答案已經答覆已經[重複]。 – Zingam

+0

所以我確實添加了一些我試過的代碼。 – Zingam

回答

4

根據從ProXicT鏈接接受的答案進行一些修改。以下代碼適用於std :: cout。

#include <iostream> 
#include <cstdio> 
#include <fstream> 

#include <Windows.h> 


// For debugging 
#include <io.h> 
#include <fcntl.h> 


#define UNUSED(x) (void)(x)  // Unused param (C compatible - not applicable to expressions) 

class outbuf : public std::streambuf { 
public: 
    outbuf() { 
     setp(0, 0); 
    } 

    virtual int_type overflow(int_type c = traits_type::eof()) { 
     return fputc(c, stdout) == EOF ? traits_type::eof() : c; 
    } 
}; 

int CALLBACK 
WinMain (HINSTANCE hInstance, 
     HINSTANCE /*hPrevInst*/, // Unused param (C++ only) 
     LPSTR lpCmdLine, 
     int (nShowCmd)) 
{ 
    UNUSED(hInstance); 
// UNUSED(hPrevInst); 
    UNUSED(lpCmdLine); 
    UNUSED(nShowCmd); // This param is used 


    // create the console 
    if (AllocConsole()) { 
     FILE* pCout; 
     freopen_s(&pCout, "CONOUT$", "w", stdout); 
     SetConsoleTitle(L"Debug Console"); 
     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED); 
    } 

    // set std::cout to use my custom streambuf 
    outbuf ob; 
    std::streambuf *sb = std::cout.rdbuf(&ob); 

    // do some work here 
    printf("Hello!\n"); 

    std::cout << "nShowCmd = " << nShowCmd << std::endl; 
    std::cout << "Now making my first Windows window!" << std::endl; 


    // make sure to restore the original so we don't get a crash on close! 
    std::cout.rdbuf(sb); 

    MessageBoxW (NULL, 
       L"Hello World!", 
       L"hello", 
       MB_OK | MB_ICONINFORMATION); 

    return 0; 
} 

編輯:

同樣的東西上面一樣,但顏色的完整性:

​​3210

現在剩下的是其他方法將無法在64位與Visual Studio 2015年工作使其成爲一個完整的日誌記錄類。

5

試試看看這個代碼。

AllocConsole(); 
HANDLE stdHandle; 
int hConsole; 
FILE* fp; 
stdHandle = GetStdHandle(STD_OUTPUT_HANDLE); 
hConsole = _open_osfhandle((long)stdHandle, _O_TEXT); 
fp = _fdopen(hConsole, "w"); 

freopen_s(&fp, "CONOUT$", "w", stdout); 

printf("Hello console on\n"); 
std::cout << "Windows 10" << std::endl; 
+1

注意:'#include '和' #open_osfhandle調用需要#include

0

如果你使用G ++,你希望你的應用程序始終同時擁有一個控制檯和一個圖形用戶界面,那麼你就可以同時提供-mconsole-mwindows的連接器選項來實現這一目標。

See this answer瞭解更多詳情。

相關問題