How to view printf output in a Win32 application o

2020-05-19 04:07发布

How can you view printf output in a Win32 application (entering with a WinMain) in Visual Studio 2010?

7条回答
SAY GOODBYE
2楼-- · 2020-05-19 04:32

I know that I have done this in the past using the AllocConsole function, but I also recall that it was just a little trickier than I expected.

A quick Google search on AllocConsole yields what is apparently a Windows Developer Journal article that seems relevant. From there, the following seems similar to what I recall, vague as it is.

void SetStdOutToNewConsole()
{
    int hConHandle;
    long lStdHandle;
    FILE *fp;

    // Allocate a console for this app
    AllocConsole();

    // Redirect unbuffered STDOUT to the console
    lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
    hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
    fp = _fdopen(hConHandle, "w");
    *stdout = *fp;

    setvbuf(stdout, NULL, _IONBF, 0);
}
查看更多
登录 后发表回答