* @brief Wires a CRT stream to the inherited std handle so a GUI-subsystem build honors * redirection. */
| 195 | * redirection. |
| 196 | */ |
| 197 | static 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. |