I am trying to optimize a legacy application written in C which makes uses of regular stdio.h resp. fcntl.h functions. For special uses, I would like to inject a few extra hints from
https://learn.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants to newly opened file handles.
But those are available only to low-level Win32 APIs. Is there a way to pass the FILE_ATTRIBUTE_... options to the open() call somehow? Or is there somewhere a sane wrapper which would open a file handle "win32 way" with custom options and hand it over as integer file descriptor just like open() would do it?
There are two functions that convert between Win32 file handles and file descriptors:
intptr_t _get_osfhandle(int fd);will get you aHANDLEfrom a givenfdint _open_osfhandle(intptr_t osfhandle, int flags);will get you anfdfrom aHANDLENote that for both:
If you want to set the attributes at creation time, you would need to use
CreateFileand then convert to a fd, because afaics there is no API to set attributes on a givenHANDLE, there's onlySetFileAttributeswhich operates on a name.If you just wanna query the attributes, you can use
GetFileInformationByHandle.