MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / bindStreamToStdHandle

Function bindStreamToStdHandle

app/src/Platform/AppPlatform.cpp:197–222  ·  view source on GitHub ↗

* @brief Wires a CRT stream to the inherited std handle so a GUI-subsystem build honors * redirection. */

Source from the content-addressed store, hash-verified

195 * redirection.
196 */
197static void bindStreamToStdHandle(DWORD stdHandle, FILE* stream)
198{
199 Q_ASSERT(stream != nullptr);
200
201 const HANDLE src = GetStdHandle(stdHandle);
202 if (src == nullptr || src == INVALID_HANDLE_VALUE)
203 return;
204
205 const HANDLE self = GetCurrentProcess();
206 HANDLE dup = nullptr;
207 if (!DuplicateHandle(self, src, self, &dup, 0, FALSE, DUPLICATE_SAME_ACCESS))
208 return;
209
210 const int fd = _open_osfhandle(reinterpret_cast<intptr_t>(dup), _O_TEXT);
211 if (fd == -1) {
212 CloseHandle(dup);
213 return;
214 }
215
216 if (_dup2(fd, _fileno(stream)) == 0) {
217 clearerr(stream);
218 (void)setvbuf(stream, nullptr, _IONBF, 0);
219 }
220
221 (void)_close(fd);
222}
223
224/**
225 * @brief Attaches the application to the parent console and redirects stdout/stderr.

Callers 1

attachToConsoleFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected