Search code examples
cwinapistdio

GetFileInformationByHandle() for stdio file handles


Is there any way to use GetFileInformationByHandle() with stdio file handles allocated by fopen()? Using fstat() with fileno() is not sufficient for my purposes because I need to get some Windows-specific information that is not part of struct stat so I'm wondering whether there is any way to feed an stdio FILE* handle to GetFileInformationByHandle()?


Solution

  • Use _fileno() to get the file number (no I don't know why Microsoft added the _, other than for "ISO C++ conformance") and then _get_osfhandle() to go from file number to HANDLE. I don't remember offhand why the msvcrt uses file numbers instead of tying a FILE directly to a HANDLE (but I think it's for POSIX compatibility?).