从Windows 10上的Win32 GUI应用程序输出到控制台

津甘

我尝试将以下代码输出到控制台:

#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

如何在Win32应用程序中打印到调试输出窗口?

但是我无法在WinMain中的Windows 10上获得任何输出到使用AllocConsole()创建的控制台。注意:实际上我没有创建任何真实的Window。是否在Window 10中进行了某些更改以阻止上述解决方案起作用,或者我可能会缺少某些内容(编译器标记或其他内容)?

你怎么看?

津甘

基于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;
}

现在剩下的就是使其成为一个完整的日志记录类。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

可以打开Windows的Win32控制台应用程序

如何从Win32 C ++应用程序输出到父控制台窗口?

控制台应用程序如何在 Windows 10 核心输出包中添加?

Windows 8的Visual Studio 2012不显示Win32控制台应用程序选项

如何在具有Windows应用程序输出类型的C#程序上显示控制台

从Windows 10的Bash运行Win32程序

如何阅读Windows GUI应用程序的控制台输出?

Win32 控制台应用程序

Windows GUI以及控制台应用程序

Qt Windows应用程序打开控制台输出

Windows XP上的Inno Setup安装程序显示“无效的Win32应用程序”

如何在Win32应用程序中检测Windows 10亮/暗模式?

在Windows Server 2008上构建时,“不是有效的Win32应用程序”

在通用 Windows 应用程序 C++ 上使用 win32 DLL 库

从我的应用程序中关闭Windows 10控制台的“标记”模式

UWP 控制台应用程序是否适用于 Windows 10 IoT

Yii2-使用Scheduler在Windows上运行控制台应用程序

C#:从控制台应用程序更新 Windows 窗体上的标签文本

.NET Core控制台应用程序无法在Windows Server上运行

在Mac或Windows上运行.NET Core控制台应用程序

.NET Core 3.1控制台应用程序将无法在Windows 7上运行

Clang 8.0.1总是在Windows上创建控制台应用程序

Mac / Linux上的Windows控制台应用程序?C ++

Windows 10上的Win32支持

将DOS应用程序转换为Win32控制台应用程序?

无法通过Windows 8.1和Windows 10上的Win32 API从控制面板中删除打印机

在Win32应用程序中使用Windows窗体

执行32位C#控制台应用程序,该应用程序连接到Windows 2003 R2 Server上的Oracle 9.2

Windows上的控制台GUI Haskell