Reputation: 197
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?
Upvotes: 1
Views: 66
Reputation: 38941
There are two functions that convert between Win32 file handles and file descriptors:
intptr_t _get_osfhandle(int fd);
will get you a HANDLE
from a given fd
int _open_osfhandle(intptr_t osfhandle, int flags);
will get you an fd
from a HANDLE
Note that for both:
The _open_osfhandle call transfers ownership of the Win32 file handle to the file descriptor. To close a file opened by using _open_osfhandle, call _close
To close a file whose operating system (OS) file handle is obtained by _get_osfhandle, call _close on the file descriptor fd. Never call CloseHandle on the return value of this function. ...
If you want to set the attributes at creation time, you would need to use CreateFile
and then convert to a fd, because afaics there is no API to set attributes on a given HANDLE
, there's only SetFileAttributes
which operates on a name.
If you just wanna query the attributes, you can use GetFileInformationByHandle
.
Upvotes: 0