CreateFile function Explanation

Source: Internet
Author: User

HANDLE CreateFile (

LPCTSTR lpFileName,//Pointer to file name

DWORD dwdesiredaccess,//access mode (write/Read)

DWORD dwShareMode,//Shared mode

lpsecurity_attributes lpsecurityattributes,//pointers to security properties

DWORD dwcreationdisposition,//How to create

DWORD dwflagsandattributes,//file properties

HANDLE htemplatefile//For copying file handles

);

parameter list:
lpFileName

Points to a null-terminated string. This parameter specifies the object used to create or open a handle. If the lpFileName object is a path, there is a limit of the maximum number of characters. Cannot exceed constant (MAX_PATH). This limit indicates how the CreateFile function parses the path.

dwdesiredaccess

Specifies how the object is accessed, and the program can gain read access, write access, read-write access, or query device ("Device query") access.

This parameter can be any combination of the following values:

0//Specify inquiry access. The program can query the properties of the device without directly accessing the device.

Generic_read//Specifies read access. You can read data from a file and move the file pointer. You can combine with Generic_write to read and write access. Generic_write//Specifies write access. You can write data from a file and move the file pointer. Can be combined with generic_read to be read-write access.

dwShareMode
//Set bit flags to indicate that objects such as Hugh Share. If the parameter is 0, the object cannot be shared. Subsequent open object operations will fail until the object's handle is closed.

use a combination of one or more of the following values to share an object:

file_share_delete//windows NT: Subsequent open operations that simply request the removal of access will succeed.

File_share_read//Subsequent open operations that simply request read access will succeed.

file_share_write//Subsequent open operations that simply request write access will succeed.

lpSecurityAttributes

//Pointer to a SECURITY_ATTRIBUTES structure to determine how to inherit the handle in a child process. If this parameter is NULL, the handle is not inheritable.

dwcreationdisposition

//Specifies how to act when a file exists or does not exist. For more information on this parameter, refer to the comment section. This parameter must be one or more of the following values:

create_new//Create a new file. If the file already exists, the function fails.

create_always//Create a new file. If the file already exists, the function overwrites the existing file and clears the existing file properties

open_existing//Open a file if the file does not exist the function will fail. If you use the CreateFile function to load the console for your device. See the section "Why use the Open_existing flag" in comments .

open_always//If the file exists, open the file. If the file does not exist and the parameter has the CREATE_NEW flag, the file is created.

truncate_existing//Open a file, each time it opens, the file will be up to 0 bytes. The calling process must open the file in Generic_write access mode. If the file does not exist, the function fails.

dwflagsandatributes
//Specify attributes and flag bits for the file

This parameter can receive any combination of the following properties. Unless all other file attributes are ignored File_attribute_normal.


file_attribute_archive//files will be archived and the program uses this property to flag files for backup or removal

File_attribute_hidden//files are hidden and will not be loaded in the General Folder List.

file_attribute_normal//file is not set any properties.

the data for the File_attribute_offline//file is not immediately available. Indicates that the file is being used offline.

file_attribute_readonly//This file is readable only. The program can read the file, but it cannot write to it or delete it.

File_attribute_system//files are part of the system or system-specific.

after the file_attribute_temporary//file is used, the file system will endeavour to maintain a piece of memory for all the data that is being accessed (by the file). Temporary files should be deleted in a timely manner when the program is not used.

Dwflagandattributes can accept any combination of the following flags.
File_flag_write_through//indicates that the system is directly written to disk via a fast cache.

file_flag_overlapped//Indicates the system initialization object, which sets a reference count for the process and returns error_io_pending. When processing is complete, the specified object is set to the signal state.

When you specify file_flag_overlapped, the function that reads and writes the file must specify a overlapped structure.

also, when file_flag_overlapped is specified, the program must perform overlapping parameters (pointing to the overlapped structure) to read and write the file.

This flag can also have more than one operation to perform.

file_flag_no_buffering//indicates that the system does not use fast buffers or caches, and when combined with file_flag_overlapped, this flag gives the maximum number of asynchronous operations, because I/O does not depend on the asynchronous operation of the memory manager. However, some i/ The O operation will run longer because the data is not in the cache.

When you use file_flag_no_buffering to open a file for work, the program must meet the following requirements:

The byte offset at the beginning of the file's access must be an integer multiple of the sector size.

the number of bytes of file access must be an integer multiple of the sector size. For example, if the sector size is a 512-byte program you can read or write 512,1024 or 2048 bytes, but not 335,981 or 7171 bytes.

The address of the read and write operation must be in the aligned position of the sector, and the address aligned in memory is the integer multiple of the sector size.

one way to align buffers with sector dimensions is to use the VirtualAlloc function. It allocates memory addresses that are aligned to the full multiples of the operating system's memory page size. Because the memory page size and sector size--2 are all their powers. This block is also aligned with the integer multiples of the sector size .

The program can determine the size of the sector by calling GetDiskFreeSpace.

file_flag_random_access//Specifies that files are randomly accessed, this flag allows the system to optimize the buffering of files.

File_flag_sequential_scan//Specifies that the file will be accessed continuously from beginning to end. This flag can prompt the system to optimize file buffering. If the program moves the file pointer in a random-access file, the optimization may not occur; However, the correct operation can still be guaranteed

specifying this flag can improve the program's ability to read large files in sequential access mode, and performance improvements are noticeable when many programs read large sequential files. But there may be a small range of bytes missing.

File_flag_delete_on_close instructs the system to delete files immediately after all open handles to the file have been closed. No, only you can specify File_flag_delete_on_close.

If you do not use File_share_delete, subsequent requests to open the file will fail.

file_flag_backup_semantics//windows NT: Instructs the system to perform a backup or restore operation for the opening or creation of a file. The system guarantees that the calling process ignores the security options for the file, if it must have a privilege. The associated privilege is the Se_ Backup_name and Se_restore_name.

You can also use this flag to get a handle to a folder, a folder handle like a file handle

passed to certain Win32 functions as well. File_flag_posix_semantics//Indicates that the file is POSIX compliant. This is the standard under MS-DOS with 16-bit Windows.

File_flag_open_reparse_point//Specifies that this flag restricts NTFS partition pointers. This flag cannot be used with creat_always.

File_flag_open_no_recall//indicates that file data is required but will continue to be received from remote Storage. It does not store data in local storage. This flag is used by a remote Storage system or a hierarchical Storage Manager system.

hTemplateFile

Specifies a handle to the template file for Generic_read access mode. The template file provides file attributes and extended properties after the file has been created.

return value

//If the function succeeds, returns a handle to the specified file that is opened. If the specified file already exists before the function call and the dwcreation parameter is create_always or Open_always, the call GetLastError will return ERROR_ Already_exists (indicates a successful function). If the function file does not exist before the call, it returns 0.

//If the function fails, the return value will be invalid_handle_value. More error messages can be obtained by calling GetLastError .

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

CreateFile function Explanation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.