message: [contour] Attaches contour.exe to parent process's console, if available.
diff: */
  #include <contour/ContourGuiApp.h>
  +#if defined(_WIN32)
  + #include <Windows.h>
  + #include <cstdio>
  + #include <fcntl.h>
  + #include <io.h>
  + #include <iostream>
  +#endif
  +
  +using namespace std;
  +
  +namespace
  +{
  +#if defined(_WIN32)
  +bool is_a_console(HANDLE h)
  +{
  + auto modeDummy = DWORD { 0 };
  + return GetConsoleMode(h, &modeDummy);
  +}
  +
  +void reopen_console_handle(DWORD std, int fd, FILE* stream)
  +{
  + HANDLE handle = GetStdHandle(std);
  + if (!is_a_console(handle))
  + return;
  + if (fd == 0)
  + freopen("CONIN$", "rt", stream);
  + else
  + freopen("CONOUT$", "wt", stream);
  +
  + setvbuf(stream, NULL, _IONBF, 0);
  +
  + // Set the low-level FD to the new handle value, since mp_subprocess2
  + // callers might rely on low-level FDs being set. Note, with this
  + // method, fileno(stdin) != STDIN_FILENO, but that shouldn't matter.
  + int unbound_fd = -1;
  + if (fd == 0)
  + unbound_fd = _open_osfhandle((intptr_t) handle, _O_RDONLY);
  + else
  + unbound_fd = _open_osfhandle((intptr_t) handle, _O_WRONLY);
  +
  + // dup2 will duplicate the underlying handle. Don't close unbound_fd,
  + // since that will close the original handle.
  + if (unbound_fd != -1)
  + dup2(unbound_fd, fd);
  +}
  +
  +template <typename... Ts>
  +void clearAll(Ts&&... streams)
  +{
  + (streams.clear(), ...);
  +}
  +
  +void tryAttachConsole()
  +{
  + if (AttachConsole(ATTACH_PARENT_PROCESS) == FALSE)
  + return;
  +
  + // We have a console window. Redirect input/output streams to that console's
  + // low-level handles, so things that use stdio work later on.
  + reopen_console_handle(STD_INPUT_HANDLE, 0, stdin);
  + reopen_console_handle(STD_OUTPUT_HANDLE, 1, stdout);
  + reopen_console_handle(STD_ERROR_HANDLE, 2, stderr);
  +
  + clearAll(cin, cout, cerr, clog, wcin, wcout, wcerr, wclog);
  +}
  +#endif
  +} // namespace
  +
  int main(int argc, char const* argv[])
  {
  +#if defined(_WIN32)
  + tryAttachConsole();
  +#endif
  +
  #if defined(CONTOUR_FRONTEND_GUI)
  contour::ContourGuiApp app;
  #else
  