如何在Win32 Project,Visual Studio 2010中添加控制台窗口

Cha*_*hee 2 c++ winapi visual-studio-2010 windows-console

我将在Win32 Project,Visual Studio 2010中添加控制台窗口.操作系统:Windows XP(x64位)

我要调试一些用console项目开发的库.我将这个添加到我的Win32项目中.

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

Ste*_*eve 5

正如本博文中所提到(我通过在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(), ...
}
Run Code Online (Sandbox Code Playgroud)