从Windows 10上的Win32 GUI应用程序输出到控制台 [英] Output to console from a Win32 GUI application on Windows 10

查看:58
本文介绍了从Windows 10上的Win32 GUI应用程序输出到控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I tried this code to output to the console:

#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!");
}

控制台打开,但没有任何输出.该代码有什么问题?

the console opens but nothing is outputted to it. What's wrong with that code?

我尝试了所有这些建议:

I tried all these suggestions:

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中进行了某些更改以阻止上述解决方案起作用,或者我可能会缺少某些内容(编译器标志或其他内容)?

but I couldn't get any output to the console created with AllocConsole() on Windows 10 in WinMain. Note: I actually didn't create any real Window. Was something changed in Window 10 that prevents the above solutions from working or is there something that I might be missing (compiler flags or something)?

您怎么看?

推荐答案

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

Based on the accepted answer from ProXicT link with a few modifications. The following code works for std::cout. The other methods won't work on 64bit with Visual Studio 2015:

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

与上面相同的东西,但具有完整性的颜色:

The same stuff as above but with colors for completeness:

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

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

All that remains now is to make it into a complete logging class.

这篇关于从Windows 10上的Win32 GUI应用程序输出到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆