{
#ifdef _UCRT
- /* For UCRT, the _fdopen will try to find free stream from
- _IOB_ENTRIES (= 3), thus we can't reopen the standard handles
- with it. Using SetHandleInformation to make the handle not
- inheritable to child process is a better way. */
- SetHandleInformation (GetStdHandle(STD_INPUT_HANDLE), HANDLE_FLAG_INHERIT, 0);
- SetHandleInformation (GetStdHandle(STD_OUTPUT_HANDLE), HANDLE_FLAG_INHERIT, 0);
- SetHandleInformation (GetStdHandle(STD_ERROR_HANDLE), HANDLE_FLAG_INHERIT, 0);
-#else
+ /* The non-UCRT code below relies on MSVCRT-only behavior, whereby
+ _fdopen reuses the first unused FILE slot, whereas UCRT skips the
+ first 3 slots, which correspond to stdin/stdout/stderr. That
+ makes it impossible in the UCRT build to open these 3 streams
+ once they are closed. So we use SetHandleInformation instead,
+ which is available on all versions of Windows that have UCRT. */
+ SetHandleInformation (GetStdHandle(STD_INPUT_HANDLE),
+ HANDLE_FLAG_INHERIT, 0);
+ SetHandleInformation (GetStdHandle(STD_OUTPUT_HANDLE),
+ HANDLE_FLAG_INHERIT, 0);
+ SetHandleInformation (GetStdHandle(STD_ERROR_HANDLE),
+ HANDLE_FLAG_INHERIT, 0);
+#else /* !_UCRT */
HANDLE parent;
HANDLE stdin_save = INVALID_HANDLE_VALUE;
HANDLE stdout_save = INVALID_HANDLE_VALUE;
parent = GetCurrentProcess ();
- /* ignore errors when duplicating and closing; typically the
- handles will be invalid when running as a gui program. */
+ /* Ignore errors when duplicating and closing; typically the
+ handles will be invalid when running as a gui program. */
DuplicateHandle (parent,
GetStdHandle (STD_INPUT_HANDLE),
parent,
else
_open ("nul", O_TEXT | O_NOINHERIT | O_WRONLY);
_fdopen (2, "w");
-#endif
+#endif /* !_UCRT */
}
/* unfortunately, atexit depends on implementation of malloc */