HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
console_win32.hpp
1// Copyright Take Vos 2023.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
8#include "../macros.hpp"
9#include <cstdio>
10
11hi_export_module(hikogui.utility.console);
12
13hi_export namespace hi {
14inline namespace v1 {
15
21inline void start_console() noexcept
22{
23 auto out_handle = GetStdHandle(STD_OUTPUT_HANDLE);
24
25 if (out_handle == NULL) {
26 // The stdout is not set, which means our parent process has
27 // not set it. This is the most likely case on Windows 10.
28
29 if (AttachConsole(ATTACH_PARENT_PROCESS)) {
30 // Our parent process is a console, like cmd and powershell.
31 // After attaching to the console we need re-open stdin,
32 // stdout, stderr using the original device names.
33
34 // Since stdin, stdout, stderr are macro's make sure we get pointers
35 // which can be modified by freopen_s().
36 FILE *fpstdin = stdin;
37 FILE *fpstdout = stdout;
38 FILE *fpstderr = stderr;
39
40 freopen_s(&fpstdin, "CONIN$", "r", stdin);
41 freopen_s(&fpstdout, "CONOUT$", "w", stdout);
42 freopen_s(&fpstderr, "CONOUT$", "w", stderr);
43
44 // Also set the win32 standard handles, so that this function can
45 // be executed multiple times.
46 auto stdin_handle = CreateFileW(L"CONIN$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
47 SetStdHandle(STD_INPUT_HANDLE, stdin_handle);
48
49 auto stdout_handle = CreateFileW(L"CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
50 SetStdHandle(STD_OUTPUT_HANDLE, stdout_handle);
51 SetStdHandle(STD_ERROR_HANDLE, stdout_handle);
52 }
53
54 } else {
55 // stdout is already working, this happens when a UNIX-like shell
56 // as setup stdin, stdout, stderr. For example when the application
57 // is started from git-bash.
58 // Since everything is already working, don't do anything.
59 }
60}
61
62} // namespace v1
63}
Rules for working with win32 headers.
The HikoGUI namespace.
Definition array_generic.hpp:20
void start_console() noexcept
Start the console.
Definition console_win32.hpp:21
DOXYGEN BUG.
Definition algorithm_misc.hpp:20