> > As for the code in question, if I understand it correctly, the current 
> > Windows idiom would be simply (I assume _open_osfhandle clears the flag?):
> >
> > SetHandleInformation (GetStdHandle (STD_INPUT_HANDLE), HANDLE_FLAG_INHERIT,
> > 0);
>
> I'm not sure you can do this without closing the stream, since the
> program has already started by that time. And SetHandleInformation is
> only available since Windows 2K, so we'd need to call it through a
> function pointer.
I think using the SetHandleInformation here should be appropriate.
The implementation of _fdopen in ucrt is similar to Wine, it starts
looking from ` _IOB_ENTRIES` (== 3) stream. Here is the extracted source
code https://github.com/huangqinjin/ucrt/blob/master/stdio/stream.cpp#L12

So, once we close the `stdin/out/err`, there's noone who get assigned
back to those streams, and then the final close in `sys_dep.c` will fail
for UCRT.

I have tested with my own UCRT64 build of Emacs at
https://github.com/kiennq/emacs-build/releases/tag/v31.276.20240805.2332f50,
With the patch of
https://github.com/kiennq/emacs-build/edit/main/patches/0001-init_winntproc-Use-SetHandleInformation-to-set-NOINH.patch

The SetHandleInformation returns no error even without streams being
closed.

---
Kien