1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85:
| OpenFile
The OpenFile function creates, opens, reopens, or deletes a file.
Note This function is provided only for compatibility with 16-bit versions of Windows. New applications should use the CreateFile function.
HFILE OpenFile( LPCSTR lpFileName, LPOFSTRUCT lpReOpenBuff, UINT uStyle );
Parameters lpFileName [in] Pointer to a null-terminated string that names the file to be opened. The string must consist of characters from the Windows character set. The OpenFile function does not support Unicode file names. Also, OpenFile does not support opening named pipes. lpReOpenBuff [out] Pointer to the OFSTRUCT structure that receives information about the file when it is first opened. The structure can be used in subsequent calls to the OpenFile function to see the open file. The OFSTRUCT structure contains a pathname string member whose length is limited to OFS_MAXPATHNAME characters. OFS_MAXPATHNAME is currently defined to be 128. Because of this, you cannot use the OpenFile function to open a file whose path length exceeds 128 characters. The CreateFile function does not have such a path length limitation.
uStyle [in] Action to take. This parameter can be one or more of the following values. Value Meaning OF_CANCEL Ignored. To produce a dialog box containing a Cancel button, use OF_PROMPT. OF_CREATE Creates a new file. If the file already exists, it is truncated to zero length. OF_DELETE Deletes the file. OF_EXIST Opens the file and then closes it. Used to test for a file's existence. OF_PARSE Fills the OFSTRUCT structure but carries out no other action. OF_PROMPT Displays a dialog box if the requested file does not exist. The dialog box informs the user that the system cannot find the file, and it contains Retry and Cancel buttons. Choosing the Cancel button directs OpenFile to return a file-not-found error message. OF_READ Opens the file for reading only. OF_READWRITE Opens the file for reading and writing. OF_REOPEN Opens the file using information in the reopen buffer. OF_SHARE_COMPAT For MS-DOS–based file systems, opens the file with compatibility mode, allowing any process on a specified computer to open the file any number of times. Other efforts to open with any other sharing mode fail. This flag is mapped to the CreateFile function's FILE_SHARE_READ | FILE_SHARE_WRITE flags. OF_SHARE_DENY_NONE Opens the file without denying read or write access to other processes. On MS-DOS-based file systems, if the file has been opened in compatibility mode by any other process, the function fails. This flag is mapped to the CreateFile function's FILE_SHARE_READ | FILE_SHARE_WRITE flags. OF_SHARE_DENY_READ Opens the file and denies read access to other processes. On MS-DOS-based file systems, if the file has been opened in compatibility mode or for read access by any other process, the function fails. This flag is mapped to the CreateFile function's FILE_SHARE_READ | FILE_SHARE_WRITE flags. OF_SHARE_DENY_WRITE Opens the file and denies write access to other processes. On MS-DOS-based file systems, if the file has been opened in compatibility mode or for write access by any other process, the function fails. This flag is mapped to the CreateFile function's FILE_SHARE_READ | FILE_SHARE_WRITE flags. OF_SHARE_EXCLUSIVE Opens the file with exclusive mode, denying both read and write access to other processes. If the file has been opened in any other mode for read or write access, even by the current process, the function fails. OF_VERIFY Verifies that the date and time of the file are the same as when it was previously opened. This is useful as an extra check for read-only files. OF_WRITE Opens the file for writing only.
Return Values If the function succeeds, the return value specifies a file handle.
If the function fails, the return value is HFILE_ERROR. To get extended error information, call GetLastError.
Remarks If the lpFileName parameter specifies a file name and extension only, this function searches for a matching file in the following directories, in the order shown:
The directory from which the application loaded. The current directory. The Windows system directory. Use the GetSystemDirectory function to get the path of this directory. The 16-bit Windows system directory. There is no function that retrieves the path of this directory, but it is searched. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory. The directories that are listed in the PATH environment variable.
The lpFileName parameter cannot contain wildcard characters.
The OpenFile function does not support the OF_SEARCH flag supported by the 16-bit Windows OpenFile function. The OF_SEARCH flag directs the system to search for a matching file even when the file name includes a full path. To search for a file, use the SearchPath function.
A sharing violation will occur if an attempt is made to open a file or directory for deletion on a remote machine when the value of the uStyle parameter is the OF_DELETE access flag OR'ed with any other access flag, and the remote file or directory has not been opened with FILE_SHARE_DELETE share access. To avoid the sharing violation in this scenario, open the remote file or directory with OF_DELETE access only or call DeleteFile without first opening the file or directory for deletion.
Windows Me/98/95: This remark does not apply.
Requirements Client: Included in Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, and Windows 95. Server: Included in Windows Server 2003, Windows 2000 Server, and Windows NT Server. Header: Declared in Winbase.h; include Windows.h. Library: Use Kernel32.lib.
See Also File Management Functions, CreateFile, GetSystemDirectory, GetWindowsDirectory, OFSTRUCT, SearchPath |