@@ -115,7 +115,24 @@ void RedirectStream(const char *p_file_name, const char *p_mode, FILE *p_cpp_str
115115}
116116
117117void RedirectIOToConsole () {
118+ // Save current handles.
119+ HANDLE h_stdin = GetStdHandle (STD_INPUT_HANDLE);
120+ HANDLE h_stdout = GetStdHandle (STD_OUTPUT_HANDLE);
121+ HANDLE h_stderr = GetStdHandle (STD_ERROR_HANDLE);
122+
118123 if (AttachConsole (ATTACH_PARENT_PROCESS)) {
124+ // Restore redirection (Note: if not redirected it's NULL handles not INVALID_HANDLE_VALUE).
125+ if (h_stdin != 0 ) {
126+ SetStdHandle (STD_INPUT_HANDLE, h_stdin);
127+ }
128+ if (h_stdout != 0 ) {
129+ SetStdHandle (STD_OUTPUT_HANDLE, h_stdout);
130+ }
131+ if (h_stderr != 0 ) {
132+ SetStdHandle (STD_ERROR_HANDLE, h_stderr);
133+ }
134+
135+ // Update file handles.
119136 RedirectStream (" CONIN$" , " r" , stdin, STD_INPUT_HANDLE);
120137 RedirectStream (" CONOUT$" , " w" , stdout, STD_OUTPUT_HANDLE);
121138 RedirectStream (" CONOUT$" , " w" , stderr, STD_ERROR_HANDLE);
@@ -173,10 +190,6 @@ void OS_Windows::initialize() {
173190 add_error_handler (&error_handlers);
174191#endif
175192
176- #ifndef WINDOWS_SUBSYSTEM_CONSOLE
177- RedirectIOToConsole ();
178- #endif
179-
180193 FileAccess::make_default<FileAccessWindows>(FileAccess::ACCESS_RESOURCES);
181194 FileAccess::make_default<FileAccessWindows>(FileAccess::ACCESS_USERDATA);
182195 FileAccess::make_default<FileAccessWindows>(FileAccess::ACCESS_FILESYSTEM);
@@ -1521,10 +1534,10 @@ void OS_Windows::unset_environment(const String &p_var) const {
15211534}
15221535
15231536String OS_Windows::get_stdin_string () {
1524- WCHAR buff[1024 ];
1537+ char buff[1024 ];
15251538 DWORD count = 0 ;
1526- if (ReadConsoleW (GetStdHandle (STD_INPUT_HANDLE), buff, 1024 , &count, nullptr )) {
1527- return String::utf16 ((const char16_t *)buff, count);
1539+ if (ReadFile (GetStdHandle (STD_INPUT_HANDLE), buff, 1024 , &count, nullptr )) {
1540+ return String::utf8 ((const char *)buff, count);
15281541 }
15291542
15301543 return String ();
@@ -1908,6 +1921,13 @@ String OS_Windows::get_system_ca_certificates() {
19081921OS_Windows::OS_Windows (HINSTANCE _hInstance) {
19091922 hInstance = _hInstance;
19101923
1924+ #ifndef WINDOWS_SUBSYSTEM_CONSOLE
1925+ RedirectIOToConsole ();
1926+ #endif
1927+
1928+ SetConsoleOutputCP (CP_UTF8);
1929+ SetConsoleCP (CP_UTF8);
1930+
19111931 CoInitializeEx (nullptr , COINIT_APARTMENTTHREADED);
19121932
19131933#ifdef WASAPI_ENABLED
0 commit comments