原创 2007年10月08日 16:29:00

The CreateFile function creates or opens the following objects and returns a handle that can be used to access
the object:
 communications resources
 disk devices(Windows NT only)
 directories(open only)


HANDLE CreateFile(
 LPCTSTR lpFileName,    //指向文件名的指针
 DWORD dwDesiredAccess,    //访问模式(写/读)
 DWORD dwShareMode,    //共享模式
 LPSECURITY_ATTRIBUTES lpSecurityAttributes, //指向安全属性的指针
 DWORD dwCreationDisposition,   //如何创建
 DWORD dwFlagsAndAttributes,   //文件属性
 HANDLE hTemplateFile    //用于复制文件句柄


 Pointer to a null-terminated string that specifies the name of the object(file, pipe, mailslot,
 communications resource, disk device, console, or directory) to create or open.

 if *lpFileName is a path, there is a default string size limit of MAX_PATH characters, This limit is
 related to how the CreateFile function parses paths.

 Specifies the type of access to the object. An application can obtain read access, write access,
 read-write access, or device query access, This parameter can be any combination of the following
 指定对象的访问方式,程序可以获得读访问权,写访问权,读写访问权或者是询问设备("device query") 访问权.
 Value(值)  Meaning(含义)
 0   Specifies device query access to the object. An application can query device
    attributes without accessing the device.

 GENERIC_READ  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  specifies write access to the object. Data can be written to the file and the
    file pointer can be moved. Combine with GENERIC_READ fro read-write access

 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.
 设置位标志指明对象如休共享.如果参数是0, 对象不能够共享. 后续的打开对象的操作将会失败,直到该对象的句

 To share the object, use a combination of one or more of the following values:
 Value(值)  Meaning(含义)
 FILE_SHARE_DELETE WindowsNT: Subsequent open operations on the object will succeed only if
    delete access is requested.
    WINDOWS NT:后续的仅仅请求删除访问权的打开操作将会成功.

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

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

 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.
 指向一个 SECURITY_ATTRIBUTES 结构的指针用于确定如何在子进程中继承这个句柄.如果这个参数是NULL,

 Specifies which action to take on files that exist, and which action to take when files do not exist.
 For more information about this parameter, see the remarks section. This parameter must be one of the
 following values

 VALUE(值)  Neaning(含义)
 CREATE_NEW  Creates a new file. The function fails if the specified file already exists
    创建一个新文件. 如果该文件已经存在函数则会失败.
 CREATE_ALWAYS  Creates a new file. If the file exsts, the function overwrites the file and
    clears the existing attributes.

 OPEN_EXISTING  Opens the file. The function fails if the file does not exist.
    See the Remarks section for a discussion of why you should use the
    OPEN_EXISTING flag if you are using the CreateFile function for devices,
    including the console.
 OPEN_ALWAYS  Opens the file, if it exsts. If the file does not exist, the function creates
    the file as if dwCreationDisposition were CREATE_NEW.
    如果文件存在,打开文件. 如果文件不存在,并且参数中有CREATE_NEW标志,则创建文件.

 TRUNCATE_EXISTING 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.

 Specifies the file attributes and flags for the file.

 Any combination of the following attributes is acceptable for the dwFlagsAndAttributes parameter,
 except all other file attributes override FILE_ATTRIBUTE_NORMAL.
 Attribute(属性)   Meaning(标志)
 FILE_ATTRIBUTE_ARCHIVE  The ifle should be archived. Application use this attribute to mark
     files for backup or removal.

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

 FILE_ATTRIBUTE_NORMAL  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  The file is read only.Applications can read the file but cannot write
     to it or delete it

 FILE_ATTRIBUTE_SYSTEM  The file is part of or is used exclusively by the operation 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.

 Any combination of the following flags is acceptable for the dwFlagsAndAttributes parameter.

 FLAG(标志)   Meaning(含义)
 FILE_FLAG_WRITE_THROUGH  Instructs the system to write through any intermediate cache and go
     directly to disk. The system can still cache write operations, but
     cannot lazily flush them.

 FILE_FLAG_OVERLAPPED  Instructs the system to initialize the object, so that operations that
     take a significant amount of time to process return ERROR_IO_PENDING.
     When the operation is finished, the specified event is set to the
     signaled state.
     指示系统初始化对象, 此操作将对进程设置一个引用计数并返回ERROR_IO_PENDING.
     处理完成后, 指定对象将被设置为信号状态.

     When you specify FILE_FLAG_OVERLAPPED, the file read and write functions
     must specify an OVERLAPPED structure. That is, when FILE_FLAG_OVERLAPPED
     is specified, an application must perform overlapped parameter(pointing
     to an OVERLAPPED structure)to the file read and write 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_OVERLAPPED被指定, 程序必须执行重叠参数(指向OVERLAPPED

 FILE_FLAG_NO_BUFFERING  Instructs the system to open the file with no intermediate buffering or
     caching.When combined with FILE_FLAG_OVERLAPPED, the flag gives maximum
     asynchronous performance, because the I/O does not rely on the synchronous
     operations of the memory manager. However, some I/O operations will take
     longer, because data is not being held in the cache.
     大的异步操作量, 因为I/O不依赖内存管理器的异步操作.然而,一些I/O操作将会运行

     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 7171bytes.

      buffer addresses for read and write operations must be sector
      aligned(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. Because 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.
     一个将缓冲区与扇区尺寸对齐的途径是使用VirtualAlloc函数. 它分配与操作系统

     An application can determine a volume's sector size by calling the
     GetDiskFreeSpace function

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

     Indicates that the file is to be accessed sequentially from beginning to
     end. The system 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 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:Indicates that the file is being opened or created for a backup
     or restore operation.The system ensures that the calling process overrides
     file security checks, provided it has the necessary privileges. The
     relevant privileges are SE_BACKUP_NAME and SE_RESTORE_NAME.
     WINDOWS NT:指示系统为文件的打开或创建执行一个备份或恢复操作. 系统保证调

     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 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 or 16-bit Windows.

 FILE_FLAG_OPEN_REPARSE_POINT Specifying this flag inhibits the reparse behavior of NTFS reparse points.
     When the file is opened, a file handle is returned, whether the filter that
     controls the reparse point is operational or not. This flag cannot be used
     with the CREATE_ALWAYS flag.

 FILE_FLAG_OPEN_NO_RECALL Indicates that the file data is requested,but it should continue to reside in
     remote storage. It should not be transported back to local storage. This flag
     is intended for use by remote storage systems or the Hierarchical Storage
     Management system.

 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.

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 dwCreation 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.
如果函数成功,返回一个打开的指定文件的句柄.如果指定文件在函数调用前已经存在并且dwCreation参数是CREATE_ALWAYS 或者
OPEN_ALWAYS,调用GetLastError就会返回ERROR_ALREADY_EXISTS(表示函数成功). 如果函数文件在调用前不存在则会返回0.

If the function fails, the return value is INVALID_HANDLE_VALUE.To get extended error information, call GetLastError.
如果函数失败,返会值会是INVALID_HANDLE_VALUE. 更多的错误信息可以调用GetLastError来获得.


微软提供了强大的文件读写操作的编程接口,所以可以通过调用API函数实现文件的读写操作。这里通过CreateFile函数来实现。 要对文件进行读写操作,首先要调用CreateFile函数打开或者创建文...
  • u012877472
  • u012877472
  • 2015年11月07日 10:38
  • 7538


在32位的windows系统中,串口和其他通信设备是作为文件处理的。对串口的操作和对文件的操作是完全一样的。通信以调用CreateFile()开始。      函数原型如下: HANDLE...
  • b5w2p0
  • b5w2p0
  • 2014年04月06日 08:54
  • 1535


原文:simple and easy——API文件读写类看了这个帖子: http://www.vbgood.c...
  • dahual
  • dahual
  • 2011年04月16日 17:21
  • 4390


文件内核对象各种设备及其常见用途: 设备 常见用途 文件 永久存储任何数据 目录 属性和文件压缩的设置 逻辑磁盘驱动器 格式化驱动器 物理磁盘驱动器 访问分区表 ...
  • bxsec
  • bxsec
  • 2017年08月02日 06:58
  • 450


  • lz465350
  • lz465350
  • 2014年06月06日 10:43
  • 351


看到了一个讲CreateFile函数的文章,详细而精炼,转过来收藏了。 出处: Cr...
  • spiderlily
  • spiderlily
  • 2012年12月06日 14:38
  • 1186


CreateFile函数创建或打开下列对象,并返回一个可以用来访问这些对象的句柄。 文件 pipes 邮槽 通信资源 磁盘驱动器(仅适用于windowsNT) 控制台 文件夹(仅用于打开)HANDLE...
  • enginetanmary
  • enginetanmary
  • 2007年04月29日 11:22
  • 728


CreateFile函数可以用来打开许多设备,如:文件、目录、逻辑磁盘驱动器、物理磁盘驱动器、串口、并口、邮件槽、命名管道等;  HANDLE  CreateFile(PCTSTR  pszName...
  • yonger7
  • yonger7
  • 2012年01月11日 12:25
  • 895

CreateFile 函数详解

HANDLE CreateFile( LPCTSTR lpFileName, //指向文件名的指针 DWORD dwDesiredAccess, //访问模式(写/读) DWORD dwS...
  • ypist
  • ypist
  • 2011年11月18日 10:43
  • 652

C/C++ 文件操作之CreateFile、ReadFile和WriteFile

1. CreateFile  这个函数的功能是创建或者打开一个文件或者I/O设备,通常使用的I/O形式有文件、文件流、目录、物理磁盘、卷、终端流等。如执行成功,则返回文件句柄。 INVALID_HAN...
  • Jeanphorn
  • Jeanphorn
  • 2015年04月10日 17:20
  • 19393