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

后端 未结 7 1353
梦如初夏
梦如初夏 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:18

    Thanks torak for your answer. It helped me a lot.

    I needed a bigger scroll back buffer so made a few additions after taking a look at the API functions. Shared here in case it helps anybody else:

    void SetStdOutToNewConsole()
    {
        // allocate a console for this app
        AllocConsole();
    
        // redirect unbuffered STDOUT to the console
        HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
        int fileDescriptor = _open_osfhandle((intptr_t)consoleHandle, _O_TEXT);
        FILE *fp = _fdopen( fileDescriptor, "w" );
        *stdout = *fp;
        setvbuf( stdout, NULL, _IONBF, 0 );
    
        // give the console window a nicer title
        SetConsoleTitle(L"Debug Output");
    
        // give the console window a bigger buffer size
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        if ( GetConsoleScreenBufferInfo(consoleHandle, &csbi) )
        {
            COORD bufferSize;
            bufferSize.X = csbi.dwSize.X;
            bufferSize.Y = 9999;
            SetConsoleScreenBufferSize(consoleHandle, bufferSize);
        }
    }
    

    This increases the scroll back (screen buffer) height to 9999 lines.

    Tested on Windows XP and Windows 7.

    0 讨论(0)
提交回复
热议问题