On Windows/mingw, what is the equivalent of `fcntl

2019-03-27 11:26发布

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

2条回答
不美不萌又怎样
2楼-- · 2019-03-27 11:49

As far as I can tell, you cant.

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"?

查看更多
Emotional °昔
3楼-- · 2019-03-27 12:01

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.

查看更多
登录 后发表回答