How to view printf output in a Win32 application on Visual Studio 2010?

后端 未结 7 1496
梦如初夏
梦如初夏 2021-02-01 05:48

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

7条回答
  •  别跟我提以往
    2021-02-01 06:11

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

提交回复
热议问题