CreateFile()

Purpose
The CreateFile function creates or opens the following objects and returns a handle that can be used to access the object
 
Library   Transcribed by Date of page Updated on
Kernell32.dll - J.Paris 01.08.02 -
 
Restrictions on use
-
 
Declare 32-bit

 
Declare Function CreateFile Lib "kernel32.dll" Alias "CreateFileA" (

ByVal lpFileName As String, ByVal dwDesiredAccess As Integer,

ByVal dwShareMode As Integer, lpSecurityAttributes As SECURITY_ATTRIBUTES, 

ByVal dwCreationDisposition As Integer,

ByVal dwFlagsAndAttributes As Integer,

ByVal hTemplateFile As Integer

) As Integer

 
Required Type definition
SECURITY_ATTRIBUTES
 
Parameters
lpFileName Points to a null-terminated string that specifies the name of the object  to create or open.

If *lpFileName is a path, there is a default string size limit of MAX_PATH characters. Special situation for NT

dwDesiredAccess Specifies the type of access to the object.  This parameter can be any combination of the following values. 

0

0

Specifies device query access to the object. An application can query device attributes without accessing the device.

GENERIC_READ

&H80000000

Specifies read access to the object. Data can be read from the file and the file pointer can be moved. Combine with GENERIC_WRITE for read-write access.

GENERIC_WRITE

&H40000000

Specifies write access to the object. Data can be written to the file and the file pointer can be moved. Combine with GENERIC_READ for read-write access.
dwShareMode Set of bit flags that specifies how the object can be shared. If dwShareMode is 0, the object cannot be shared. Subsequent open operations on the object will fail, until the handle is closed. 

FILE_SHARE_DELETE

(?)

Windows NT only: Subsequent open operations on the object will succeed only if delete access is requested. 

FILE_SHARE_READ

&H1

Subsequent open operations on the object will succeed only if read access is requested. 

GENERIC_WRITE

&H2

Subsequent open operations on the object will succeed only if write access is requested. 
lpSecurityAttributes Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpSecurityAttributes is NULL, the handle cannot be inherited. 

Windows NT: The lpSecurityDescriptor member of the structure specifies a security descriptor for the object. If lpSecurityAttributes is NULL, the object gets a default security descriptor. The target file system must support security on files and directories for this parameter to have an effect on files.
Windows 95: The lpSecurityDescriptor member of the structure is ignored
dwCreationDistribution Specifies which action to take on files that exist, and which action to take when files do not exist.

CREATE_NEW

1

Creates a new file. The function fails if the specified file already exists

CREATE_ALWAYS 

2

Creates a new file. The function overwrites the file if it exists.

OPEN_EXISTING

3

Opens the file. The function fails if the file does not exist.

OPEN_ALWAYS

4

Opens the file, if it exists. If the file does not exist, the function creates the file as if dwCreationDistribution were CREATE_NEW.

TRUNCATE_EXISTING 

5

Opens the file. Once opened, the file is truncated so that its size is zero bytes. The calling process must open the file with at least GENERIC_WRITE access. The function fails if the file does not exist.
dwFlagsAndAttributes Specifies the file attributes and flags for the file

Any combination of the following attributes is acceptable, except all other file attributes override FILE_ATTRIBUTE_NORMAL. 

FILE_ATTRIBUTE_ARCHIVE

&H20

The file should be archived. Applications use this attribute to mark files for backup or removal.

FILE_ATTRIBUTE_COMPRESSED

(?)

The file or directory is compressed. For a file, this means that all of the data in the file is compressed. For a directory, this means that compression is the default for newly created files and subdirectories

FILE_ATTRIBUTE_HIDDEN

(?)

The file is hidden. It is not to be included in an ordinary directory listing.

FILE_ATTRIBUTE_NORMAL

&H80

The file has no other attributes set. This attribute is valid only if used alone.

FILE_ATTRIBUTE_OFFLINE

(?)

The data of the file is not immediately available. Indicates that the file data has been physically moved to offline storage.

FILE_ATTRIBUTE_READONLY

&H1

The file is read only. Applications can read the file but cannot write to it or delete it.

FILE_ATTRIBUTE_SYSTEM

&H4

The file is part of or is used exclusively by the operating system.

FILE_ATTRIBUTE_TEMPORARY

(?)

The file is being used for temporary storage. File systems attempt to keep all of the data in memory for quicker access rather than flushing the data back to mass storage. A temporary file should be deleted by the application as soon as it is no longer needed.

 

FILE_FLAG_WRITE_THROUGH

&H80000000

Instructs the operating system to write through any intermediate cache and go directly to disk. The operating system can still cache write operations, but cannot lazily flush them.

FILE_FLAG_OVERLAPPED

&H40000000

Instructs the operating system to initialize the object, so ReadFile, WriteFile, ConnectNamedPipe, and TransactNamedPipe operations that take a significant amount of time to process return ERROR_IO_PENDING. When the operation is finished, an event is set to the signaled state.
When you specify FILE_FLAG_OVERLAPPED, the ReadFile and WriteFile functions must specify an OVERLAPPED structure. That is, when FILE_FLAG_OVERLAPPED is specified, an application must perform overlapped reading and writing.
When FILE_FLAG_OVERLAPPED is specified, the operating system does not maintain the file pointer. The file position must be passed as part of the lpOverlapped parameter (pointing to an OVERLAPPED structure) to the ReadFile and WriteFile functions.
This flag also enables more than one operation to be performed simultaneously with the handle (a simultaneous read and write operation, for example).

FILE_FLAG_NO_BUFFERING

&H20000000

 
Instructs the operating system to open the file with no intermediate buffering or caching. This can provide performance gains in some situations. An application must meet certain requirements when working with files opened with FILE_FLAG_NO_BUFFERING:· File access must begin at byte offsets within the file that are integer multiples of the volume's sector size. · File access must be for numbers of bytes that are integer multiples of the volume's sector size. For example, if the sector size is 512 bytes, an application can request reads and writes of 512, 1024, or 2048 bytes, but not of 335, 981, or 7171 bytes. · Buffer addresses for read and write operations must be aligned on addresses in memory that are integer multiples of the volume's sector size. One way to align buffers on integer multiples of the volume sector size is to use VirtualAlloc to allocate the buffers. It allocates memory that is aligned on addresses that are integer multiples of the operating system's memory page size. Since both memory page and volume sector sizes are powers of 2, this memory is also aligned on addresses that are integer multiples of a volume's sector size. An application can determine a volume's sector size by calling the GetDiskFreeSpace function. 

FILE_FLAG_RANDOM_ACCESS

&H10000000

Indicates that the file is accessed randomly. Windows can use this as a hint to optimize file caching.

FILE_FLAG_SEQUENTIAL_SCAN

&H8000000

Indicates that the file is to be accessed sequentially from beginning to end. Windows can use this as a hint to optimize file caching. If an application moves the file pointer for random access, optimum caching may not occur; however, correct operation is still guaranteed.
Specifying this flag can increase performance for applications that read large files using sequential access. Performance gains can be even more noticeable for applications that read large files mostly sequentially, but occasionally skip over small ranges of bytes.

FILE_FLAG_DELETE_ON_CLOSE 

&H4000000

Indicates that the operating system is to delete the file immediately after all of its handles have been closed, not just the handle for which you specified FILE_FLAG_DELETE_ON_CLOSE. Subsequent open requests for the file will fail, unless FILE_SHARE_DELETE is used. 

FILE_FLAG_BACKUP_SEMANTICS

(?)

Windows NT only: Indicates that the file is being opened or created for a backup or restore operation. The operating system ensures that the calling process overrides file security checks, provided it has the necessary permission to do so. The relevant permissions are SE_BACKUP_NAME and SE_RESTORE_NAME.You can also set this flag to obtain a handle to a directory. A directory handle can be passed to some Win32 functions in place of a file handle.

FILE_FLAG_POSIX_SEMANTICS

&H1000000

Indicates that the file is to be accessed according to POSIX rules. This includes allowing multiple files with names, differing only in case, for file systems that support such naming. Use care when using this option because files created with this flag may not be accessible by applications written for MS-DOS, Windows, or Windows NT.
hTemplateFile Specifies a handle with GENERIC_READ access to a template file. The template file supplies file attributes and extended attributes for the file being created. 
Windows 95: This value must be NULL. If you supply a handle under Windows 95, the call fails and GetLastError returns ERROR_NOT_SUPPORTED
 
Return value
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 dwCreationDistribution 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 (-1). To get extended error information, call GetLastError. 
 
Example
-
 
Comments
Use the CloseHandle function to close an object handle returned by CreateFile. 

There are many details concerning Security and the impact of that function on the different types of objects to be found in the original documents.

 
See also
CreateDirectoryEx(), CreateFile(), RemoveDirectory()