First some backginformation - why the loop does not work under Windows:
In Linux the handles have numbers 0...n. The handles passed to "close()" and similar functions are directly passed to the operating system:
void close(int handle)
{
syscall(SYS_CLOSE,handle);
}
However in Windows the operating system uses its own handles that are similar to pointer addresses and the C library uses some kind of translation table:
void close(int handle)
{
CloseHandle(table[handle]);
table[handle]=NULL;
}
If handles remain open the C library does not know (not STDIN, STDOUT, STDERR) these handles will not be in the table.
Now about the actual question:
Unlike Linux Windows mixes up different kinds of handles (File handles, Memory handles, Process handles, ...). If you could get a list of all handles you'll have to treat dem differently.
Another point is that Windows libraries use some handles internally. If you simply close all handles inherited by the parent process you'll risk a crash of your program because the Windows libraries may depend on some of these handles.
So it is definitely up to the parent application to ensure that no handles remain open. By default handles are not inherited in Windows. However the C library wrappers (e.g. "fopen()") will set the "inherit handle" flag so the handles will be inherited.