@@ -113,25 +113,57 @@ int Platform::defaultBackEnd() {
113113}
114114
115115#if (BOOST_OS_WINDOWS)
116+ // std::ofstream Platform::conOutStream;
117+ std::unique_ptr<FILE, decltype (&fclose)> Platform::stdErrStream{nullptr , fclose};
118+ // std::unique_ptr<FILE, decltype(&fclose)> Platform::stdOutFd{nullptr, fclose};
119+ // std::unique_ptr<FILE, decltype(&fclose)> Platform::oldStdOutFd{nullptr, fclose};
116120int Platform::stderr_fd;
121+ // int Platform::stdout_file;
122+
123+ std::unique_ptr<HANDLE, decltype (&CloseHandle)> Platform::stdOutHandle{nullptr , CloseHandle};
117124
118125void Platform::initWindowsConsole () {
119126 if (AttachConsole (ATTACH_PARENT_PROCESS) == 0 || fileno (stdout) == -2 || fileno (stderr) == -2 ) {
120127 auto ptr = stdErrStream.get ();
121128 freopen_s (&ptr, " NUL" , " w" , stderr);
122129 }
123130 else {
124- freopen_s ((FILE**)stdout, " CONOUT$" , " w" , stdout);
125131 freopen_s ((FILE**)stderr, " CONOUT$" , " w" , stderr);
126- std::ios::sync_with_stdio (true );
132+ freopen_s ((FILE**)stdout, " CONOUT$" , " w" , stdout);
133+ HANDLE hStdout = CreateFile (
134+ " CONOUT$" , GENERIC_READ | GENERIC_WRITE,
135+ FILE_SHARE_READ | FILE_SHARE_WRITE, NULL ,
136+ OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
137+ );
138+ stdOutHandle.reset (&hStdout);
139+ SetStdHandle (STD_OUTPUT_HANDLE, *stdOutHandle);
140+ // int conOutHandle = _open_osfhandle((intptr_t) GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
141+ // if (conOutHandle != -1) {
142+ // stdOutFd.reset(_fdopen(conOutHandle, "w"));
143+ // if (stdOutFd) {
144+ // oldStdOutFd.reset(stdout);
145+ // *stdout = *stdOutFd;
146+ // }
147+ // }
148+ // else {
149+ // fmt::print("Error in call to _open_osfhandle.");
150+ // }
127151 std::setvbuf (stdout, nullptr , _IONBF, 0 );
128- std::setvbuf (stderr, nullptr , _IONBF, 0 );
129152 std::cout.clear ();
130- std::cerr.clear ();
131153 std::wcout.clear ();
132- std::wcerr.clear ();
154+ std::cout << " Testing cout, derp." << std::endl;
155+ // conOutStream = std::ofstream{"CONOUT$", std::ios::out};
156+ // std::cout.rdbuf(conOutStream.rdbuf());
157+ // std::setvbuf(stdout, nullptr, _IONBF, 0);
158+
159+ // std::ios::sync_with_stdio(true);
160+ // std::setvbuf(stderr, nullptr, _IONBF, 0);
161+
162+ // std::cerr.clear();
163+ // std::wcerr.clear();
133164 }
134165 stderr_fd = fileno (stderr);
166+ // stdout_file = fileno(stdout);
135167}
136168
137169extern " C" {
0 commit comments