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

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

    For MinGW use "_A_SYSTEM" instead "_O_TEXT". So ported Quintin Willison answer is as follows:

    #include 
    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, _A_SYSTEM);
      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);
      }
    }
    

提交回复
热议问题