windows_programming_notes.nbk: Home | Index | Next Page: CreateFont | Previous Page: CreateDialogParam


 CreateFile

The CreateFile function creates or opens the following objects and returns a handle that can be used to access the object:

To perform this operation as a transacted operation, use the CreateFileTransacted function.

    HANDLE WINAPI CreateFile(
      __in          LPCTSTR                 lpFileName,
      __in          DWORD                   dwDesiredAccess,
      __in          DWORD                   dwShareMode,
      __in          LPSECURITY_ATTRIBUTES   lpSecurityAttributes,
      __in          DWORD                   dwCreationDisposition,
      __in          DWORD                   dwFlagsAndAttributes,
      __in          HANDLE                  hTemplateFile
    );

Parameters

Windows 95/98/Me: This string must not exceed MAX_PATH characters.

Return Values

If the function succeeds, the return value is an open handle to the specified file. If the specified file exists before the function call and dwCreationDisposition is CREATE_ALWAYS or OPEN_ALWAYS, a call to GetLastError returns ERROR_ALREADY_EXISTS (even though the function has succeeded). If the file does not exist before the call, GetLastError returns zero.

If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.

Remarks

Use the CloseHandle function to close an object handle returned by CreateFile.

As noted above, specifying zero for dwDesiredAccess allows an application to query device attributes without actually accessing the device. This type of querying is useful, for example, if an application wants to determine the size of a floppy disk drive and the formats it supports without having a floppy in the drive.

MAPI: For more information, see Syntax and Limitations for Win32 Functions Useful in MAPI Development.

Files

If you are attempting to create a file on a floppy drive that does not have a floppy disk or a CD-ROM drive that does not have a CD, the system displays a message box asking the user to insert a disk or a CD, respectively. To prevent the system from displaying this message box, call the SetErrorMode function with SEM_FAILCRITICALERRORS.

When creating a new file, the CreateFile function performs the following actions:

Combines the file attributes and flags specified by dwFlagsAndAttributes with FILE_ATTRIBUTE_ARCHIVE. Sets the file length to zero. Copies the extended attributes supplied by the template file to the new file if the hTemplateFile parameter is specified. When opening an existing file, CreateFile performs the following actions:

Combines the file flags specified by dwFlagsAndAttributes with existing file attributes. CreateFile ignores the file attributes specified by dwFlagsAndAttributes. Sets the file length according to the value of dwCreationDisposition. Ignores the hTemplateFile parameter. Ignores the lpSecurityDescriptor member of the SECURITY_ATTRIBUTES structure if the lpSecurityAttributes parameter is not NULL. The other structure members are used. The bInheritHandle member is the only way to indicate whether the file handle can be inherited. Windows NT/2000 or later: If you rename or delete a file, then restore it shortly thereafter, Windows NT searches the cache for file information to restore. Cached information includes its short/long name pair and creation time.

Windows NT/2000 or later: Some file systems, such as NTFS, support compression or encryption for individual files and directories. On volumes formatted for such a file system, a new file inherits the compression and encryption attributes of its directory.

You cannot use the CreateFile function to set a file's compression state. Use the DeviceIoControl function to set a file's compression state.

Pipes

If CreateFile opens the client end of a named pipe, the function uses any instance of the named pipe that is in the listening state. The opening process can duplicate the handle as many times as required but, once opened, the named pipe instance cannot be opened by another client. The access specified when a pipe is opened must be compatible with the access specified in the dwOpenMode parameter of the CreateNamedPipe function. For more information about pipes, see Pipes.

Mailslots

If CreateFile opens the client end of a mailslot, the function returns INVALID_HANDLE_VALUE if the mailslot client attempts to open a local mailslot before the mailslot server has created it with the CreateMailSlot function. For more information about mailslots, see Mailslots.

Communications Resources

The CreateFile function can create a handle to a communications resource, such as the serial port COM1. For communications resources, the dwCreationDisposition parameter must be OPEN_EXISTING, and the hTemplate parameter must be NULL. Read, write, or read/write access can be specified, and the handle can be opened for overlapped I/O. For more information about communications, see Communications.

Disk Devices

Volume handles may be opened as noncached at the discretion of the file system, even when the noncached option is not specified with CreateFile. You should assume that all Microsoft file systems open volume handles as noncached. The restrictions on noncached I/O for files apply to volumes as well.

A file system may or may not require buffer alignment even though the data is noncached. However, if the noncached option is specified when opening a volume, buffer alignment is enforced regardless of the file system on the volume. It is recommended on all file systems that you open volume handles as noncached and follow the noncached I/O restrictions.

Windows NT/2000 or later: You can use the CreateFile function to open a disk drive or a partition on a disk drive. The function returns a handle to the disk device; that handle can be used with the DeviceIOControl function. The following requirements must be met in order for such a call to succeed:

The caller must have administrative privileges for the operation to succeed on a hard disk drive. The lpFileName string should be of the form \\.\PHYSICALDRIVEx to open the hard disk x. Hard disk numbers start at zero. For example: String Meaning \\.\PHYSICALDRIVE2 Obtains a handle to the third physical drive on the user's computer.

For an example showing how to open a physical drive, see Calling DeviceIoControl on Windows NT/2000.

The lpFileName string should be \\.\x: to open a floppy drive x or a partition x on a hard disk. For example: String Meaning \\.\A: Obtains a handle to drive A on the user's computer. \\.\C: Obtains a handle to drive C on the user's computer.

There is no trailing backslash in a drive name. The string "\\.\c:\" refers to the root directory of drive C.

On Windows 2000 or later, you can also open a volume by referring to its unique volume name. In this case also, there should be no trailing backslash on the unique volume name.

Note that all I/O buffers should be sector aligned (aligned on addresses in memory that are integer multiples of the volume's sector size), even if the disk device is opened without the FILE_FLAG_NO_BUFFERING flag. Depending the disk, this requirement may not be enforced.

Windows 95/98/Me: This technique does not work for opening a logical drive. Specifying a string in this form causes CreateFile to return an error.

The dwCreationDisposition parameter must have the OPEN_EXISTING value. When opening a floppy disk or a partition on a hard disk, you must set the FILE_SHARE_WRITE flag in the dwShareMode parameter.

Tape Drives

Windows NT/2000 or later: You can open tape drives using a file name of the form \\.\TAPEx where x is a number indicating which drive to open, starting with tape drive 0. To open tape drive 0 in C, use the file name "\\\\.\\TAPE0". For more information on manipulating tape drives for backup or other applications, see Tape Backup.

Consoles

The CreateFile function can create a handle to console input (CONIN$). If the process has an open handle to it as a result of inheritance or duplication, it can also create a handle to the active screen buffer (CONOUT$). The calling process must be attached to an inherited console or one allocated by the AllocConsole function. For console handles, set the CreateFile parameters as follows.

Parameters Value

lpFileName Use the CONIN$ value to specify console input and the CONOUT$ value to specify console output. CONIN$ gets a handle to the console's input buffer, even if the SetStdHandle function redirected the standard input handle. To get the standard input handle, use the GetStdHandle function. CONOUT$ gets a handle to the active screen buffer, even if SetStdHandle redirected the standard output handle. To get the standard output handle, use GetStdHandle. dwDesiredAccess GENERIC_READ | GENERIC_WRITE is preferred, but either one can limit access. dwShareMode If the calling process inherited the console or if a child process should be able to access the console, this parameter must be FILE_SHARE_READ | FILE_SHARE_WRITE. lpSecurityAttributes If you want the console to be inherited, the bInheritHandle member of the SECURITY_ATTRIBUTES structure must be TRUE. dwCreationDisposition You should specify OPEN_EXISTING when using CreateFile to open the console. dwFlagsAndAttributes Ignored. hTemplateFile Ignored.

The following list shows the effects of various settings of fwdAccess and lpFileName.

lpFileName fwdAccess Result CON GENERIC_READ Opens console for input. CON GENERIC_WRITE Opens console for output. CON GENERIC_READ GENERIC_WRITE Windows 95/98/Me: Causes CreateFile to fail; GetLastError returns ERROR_PATH_NOT_FOUND. Windows NT/2000 or later: Causes CreateFile to fail; GetLastError returns ERROR_FILE_NOT_FOUND.

Directories

An application cannot create a directory with CreateFile; it must call CreateDirectory or CreateDirectoryEx to create a directory.

Windows NT/2000 or later: You can obtain a handle to a directory by setting the FILE_FLAG_BACKUP_SEMANTICS flag. A directory handle can be passed to some functions in place of a file handle.

Some file systems, such as NTFS, support compression or encryption for individual files and directories. On volumes formatted for such a file system, a new directory inherits the compression and encryption attributes of its parent directory.

You cannot use the CreateFile function to set a directory's compression state. Use the DeviceIoControl function to set a directory's compression state.

For example, see Creating and Opening Files.

Requirements


windows_programming_notes.nbk: Home | Index | Next Page: CreateFont | Previous Page: CreateDialogParam


Notebook exported on Monday, 7 July 2008, 18:56:50 PM Eastern Daylight Time