On Windows/mingw, what is the equivalent of `fcntl(fd, F_GETFL) | O_ACCMODE`?

微笑、不失礼 提交于 2019-12-04 21:09:30

问题


I am compiling a program on Windows with Mingw. How can I get the access mode for an open file descriptor?


回答1:


According to Win32.hlp, the API supplies the function BOOL GetFileInformationByHandle(HANDLE hFile, LPBY_HANDLE_FILE_INFORMATION lpFileInformation) in KERNEL32. LPBY_HANDLE_FILE_INFORMATION is a BY_HANDLE_FILE_INFORMATION*, where BY_HANDLE_FILE_INFORMATION is as follows:

typedef struct _BY_HANDLE_FILE_INFORMATION { // bhfi  
    DWORD    dwFileAttributes; 
    FILETIME ftCreationTime; 
    FILETIME ftLastAccessTime; 
    FILETIME ftLastWriteTime; 
    DWORD    dwVolumeSerialNumber; 
    DWORD    nFileSizeHigh; 
    DWORD    nFileSizeLow; 
    DWORD    nNumberOfLinks; 
    DWORD    nFileIndexHigh; 
    DWORD    nFileIndexLow; 
} BY_HANDLE_FILE_INFORMATION;

After calling said function, if it returns true, the BY_HANDLE_FILE_INFORMATION contains data pertinent to your file. dwFileAttributes may contain the FILE_ATTRIBUTE_READ_ONLY flag.

If you want more than that, there is also:

BOOL GetKernelObjectSecurity(
 HANDLE Handle,                             // handle of object to query
 SECURITY_INFORMATION RequestedInformation, // requested information
 PSECURITY_DESCRIPTOR pSecurityDescriptor,  // address of security descriptor
 DWORD nLength,                             // size of buffer for security descriptor 
 LPDWORD lpnLengthNeeded                    // address of required size of buffer
);

The API reference is necessarily vague on what a SECURITY_DESCRIPTOR is, but you can call a bunch of other functions using its address as a parameter to get specific properties. The SECURITY_INFORMATION is just a DWORD constant specifying which of these functions you plan to call. You can find more info at http://msdn.microsoft.com/en-us/library/aa446641%28VS.85%29.aspx

Edit - the second code section keeps coming out looking screwy, but the link to the API reference will lead you where you need to go if you dig around a bit.




回答2:


As far as I can tell, you cant.

https://web.archive.org/web/20161107234935/http://www.zemris.fer.hr/predmeti/os1/misc/Unix2Win.htm is a good guide for unix-to-windows porting.

Maybe you could use the Cygwin POSIX "emulation"?



来源:https://stackoverflow.com/questions/4636875/on-windows-mingw-what-is-the-equivalent-of-fcntlfd-f-getfl-o-accmode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!