【问题标题】:How to add console window in Win32 Project, Visual Studio 2010如何在 Win32 项目、Visual Studio 2010 中添加控制台窗口
【发布时间】:2014-06-25 02:16:40
【问题描述】:

我将在 Win32 项目、Visual Studio 2010 中添加控制台窗口。 操作系统:Windows XP(x64 位)

我将调试一些使用控制台项目开发的库。 我将此添加到我的 Win32 项目中。

是否有任何解决方案可以将控制台窗口添加到 Win32 项目中?

【问题讨论】:

  • 试过了,但我没有得到正确的发布。我知道我的问题很丑。

标签: c++ visual-studio-2010 winapi windows-console


【解决方案1】:

正如this blog post 中提到的(我通过在 Google 中输入“将控制台添加到 win32 项目”中找到的),您可以使用以下代码完成此操作:

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

    // use the console just like a normal one - printf(), getchar(), ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多