【问题标题】:Output to console from a Win32 GUI application on Windows 10从 Windows 10 上的 Win32 GUI 应用程序输出到控制台
【发布时间】:2015-11-18 01:35:19
【问题描述】:

我尝试将这段代码输出到控制台:

#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?

但我无法在 WinMain 中的 Windows 10 上使用 AllocConsole() 创建的控制台获得任何输出。 注意:我实际上并没有创建任何真正的 Window。 Window 10 中是否进行了某些更改,导致上述解决方案无法正常工作,或者是否有我可能遗漏的内容(编译器标志或其他内容)?

你怎么看?

【问题讨论】:

  • 请不要在站外链接中提问。请显示您尝试过的代码。您期望发生的事情,以及实际发生的事情。就目前而言,这个问题是题外话。
  • 当我这样做时,我会得到 50 个答案,这个答案已经 [重复]。
  • 为什么要截断句柄? Windows 上的 long 不够大,无法存储 intptr_t 值。

标签: c++ winapi


【解决方案1】:

如果您使用的是 g++,并且希望您的应用程序始终同时具有控制台和 GUI,那么您可以提供 -mconsole 和 -mwindows 的链接器标志来实现此目的。

See this answer 了解更多详情。

【讨论】:

    【解决方案2】:

    试试这个代码。

    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;
    

    【讨论】:

    • 注意:_open_osfhandle 调用需要 #include &lt;io.h&gt; 和 #include &lt;fcntl.h&gt;
    • freopen_s的第一个参数是out参数,可以去掉前面三行代码。
    【解决方案3】:

    基于 ProXicT 链接接受的答案,并进行了一些修改。以下代码适用于 std::cout。其他方法不适用于 Visual Studio 2015 的 64 位:

    #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;
    }
    

    编辑:

    与上面相同的东西,但为了完整性而使用颜色:

    #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");
    
        }
    
        // set std::cout to use my custom streambuf
        outbuf ob;
        std::streambuf *sb = std::cout.rdbuf(&ob);
    
        // do some work here
    
        printf("Hello!\n");
    
        HANDLE stdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
        CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
        WORD defaultConsoleTextAttributes;
        GetConsoleScreenBufferInfo(stdHandle, &consoleInfo);
        defaultConsoleTextAttributes = consoleInfo.wAttributes;
        WORD currentConsoleTextAttributes = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED;
        SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
        std::cout << "nShowCmd = " << nShowCmd << std::endl;
        currentConsoleTextAttributes = FOREGROUND_GREEN;
        SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
        std::cout << "Now making my first Windows window!" << std::endl;
        currentConsoleTextAttributes = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
        SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
        std::cout << "Now making my first Windows window!" << std::endl;
        currentConsoleTextAttributes = FOREGROUND_BLUE;
        SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
        std::cout << "Now making my first Windows window!" << std::endl;
        currentConsoleTextAttributes = FOREGROUND_BLUE | FOREGROUND_INTENSITY;
        SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
        std::cout << "Now making my first Windows window!" << std::endl;
        currentConsoleTextAttributes = FOREGROUND_RED;
        SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
        std::cout << "Now making my first Windows window!" << std::endl;
        currentConsoleTextAttributes = FOREGROUND_RED | FOREGROUND_INTENSITY;
        SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
        std::cout << "Now making my first Windows window!" << std::endl;
        currentConsoleTextAttributes = FOREGROUND_GREEN | FOREGROUND_BLUE;
        SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
        std::cout << "Now making my first Windows window!" << std::endl;
        currentConsoleTextAttributes = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
        SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
        std::cout << "Now making my first Windows window!" << std::endl;
        currentConsoleTextAttributes = FOREGROUND_GREEN | FOREGROUND_RED;
        SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
        std::cout << "Now making my first Windows window!" << std::endl;
        currentConsoleTextAttributes = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
        SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
        std::cout << "Now making my first Windows window!" << std::endl;
        currentConsoleTextAttributes = FOREGROUND_BLUE | FOREGROUND_RED;
        SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
        std::cout << "Now making my first Windows window!" << std::endl;
        currentConsoleTextAttributes = FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY;
        SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
        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);
    
        Sleep(5000);
        ShowWindow(GetConsoleWindow(), SW_HIDE);
        FreeConsole();
    
        MessageBoxW (NULL,
                     L"Hello World!",
                     L"hello",
                     MB_OK | MB_ICONINFORMATION);
    
        return 0;
    }
    

    现在剩下的就是把它变成一个完整的日志类。

    【讨论】:

      猜你喜欢
      • 2011-03-10
      • 1970-01-01
      • 2011-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-14
      • 2015-10-19
      • 1970-01-01
      相关资源
      最近更新 更多