CreateFile函数详解
CreateFile
The CreateFile function creates or opens the following objects and returns a handle that can be used to access
the object:
files
pipes
mailslots
communications resources
disk devices(Windows NT only)
consoles
directories(open only)
CreateFile函数创建或打开下列对象,并返回⼀个可以⽤来访问这些对象的句柄。
⽂件
pipes
邮槽
通信资源
磁盘驱动器(仅适⽤于windowsNT)
控制台
⽂件夹(仅⽤于打开)
HANDLE CreateFile( LPCTSTR lpFileName, // 指向⽂件名的指针 DWORD dwDesiredAccess, // 访问模式(写/读) DWORD dwShareMode, // 共享模式LPSECURITY_ATTRIBUTES lpSecurityAttributes, //指向安全属性的指针 DWORD dwCreationDisposition, // 如何创建 DWORD dwFlagsAndAttributes, // ⽂件属性HANDLE hTemplateFile // ⽤于复制⽂件句柄 );
access是什么意思啊了
Parametes
参数列表
参数类型及说明
lpFileName String,要打开的⽂件的名字
dwDesiredAccess Long,如果为GENERIC_READ 表⽰允许对设备进⾏读访问;如果为GENERIC_WRITE 表⽰允许对设备进⾏写访问(可组合使⽤);如果为零,表⽰只允许获取与⼀个设备有关的信息
dwShareMode Long,零表⽰不共享;FILE_SHARE_READ 和/或FILE_SHARE_WRITE 表⽰允许对⽂件进⾏共享访问
lpSecurityAttributes SECURITY_ATTRIBUTES,指向⼀个SECURITY_ATTRIBUTES结构的指针,定义了⽂件的安全特性(如果操作系统⽀持的话)
dwCreationDisposition Long,下述常数之⼀:
CREATE_NEW创建⽂件;如⽂件存在则会出错CREATE_ALWAYS创建⽂件,会改写前⼀个⽂件OPEN_EXISTING⽂件必须已经存在。由设备提出要求OPEN_ALWAYS如⽂件不存在则创建它TRUNCATE_EXISTING讲现有⽂件缩短为零长度
Long,⼀个或多个下述常数
FILE_ATTRIBUTE_ARCHIVE标记归档属性
dwFlagsAndAttributes FILE_ATTRIBUTE_COMPRESSED
将⽂件标记为已压缩,或者标记为⽂件在⽬录中的默
认压缩⽅式
FILE_ATTRIBUTE_NORMAL默认属性
FILE_ATTRIBUTE_HIDDEN隐藏⽂件或⽬录
FILE_ATTRIBUTE_READONLY⽂件为只读
FILE_ATTRIBUTE_SYSTEM⽂件为系统⽂件
FILE_FLAG_WRITE_THROUGH操作系统不得推迟对⽂件的写操作
FILE_FLAG_OVERLAPPED允许对⽂件进⾏重叠操作
FILE_FLAG_NO_BUFFERING
禁⽌对⽂件进⾏缓冲处理。⽂件只能写⼊磁盘卷的扇
区块
FILE_FLAG_RANDOM_ACCESS针对随机访问对⽂件缓冲进⾏优化
FILE_FLAG_SEQUENTIAL_SCAN针对连续访问对⽂件缓冲进⾏优化
FILE_FLAG_DELETE_ON_CLOSE
关闭了上⼀次打开的句柄后,将⽂件删除。特别适合
临时⽂件
也可在Windows NT下组合使⽤下述常数标记:
SECURITY_ANONYMOUS,SECURITY_IDENTIFICATION,SECURITY_IMPERSONATION,SECURITY_DELEGATION,SECURITY_CONTEXT_TRACKING,SECURITY_EFFECTIVE_ONLY
hTemplateFile Long,如果不为零,则指定⼀个⽂件句柄。新⽂件将从这个⽂件中复制扩展属性
返回值
如执⾏成功,则返回⽂件句柄。INVALID_HANDLE_VALUE表⽰出错,会设置。即使函数成功,但若⽂件存在,且指定了CREATE_ALWAYS 或OPEN_ALWAYS,也会设为ERROR_ALREADY_EXISTS
lpFileName
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.
如果lpFileName的对象是⼀个路径,则有⼀个最⼤字符数的限制。不能超过常量(MAX_PATH).这个限制指⽰了
CreateFile函数如何解析路径.
dwDesiredAccess
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
values
指定对象的访问⽅式,程序可以获得读访问权,写访问权,读写访问权或者是询问设备("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组合
成为"读写访问权".
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
指定写访问权.可以从⽂件中写⼊数据,并且移动⽂件指针.可以和GENERIC_READ组合
成为"读写访问权".
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.
设置位标志指明对象如休共享.如果参数是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.
后续的仅仅请求写访问权的打开操作将会成功.
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.
指向⼀个SECURITY_ATTRIBUTES 结构的指针⽤于确定如何在⼦进程中继承这个句柄.如果这个参数是NULL,则该句柄不可继承.
dwCreationDisposition
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.
打开⼀个⽂件,如果⽂件不存在函数将会失败.
如查你使⽤CreateFile函数为设备装载控制台.请查看批注中的"为什么使⽤
OPEN_EXISTING标志"的部分.
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.
打开⼀个⽂件,每次打开,⽂件将被截⾄0字节.调⽤进程必须⽤GENERIC_WRITE访问模式打开⽂件.如果⽂件不存在则函数就会失败.
dwFlagsAndatributes
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.
该参数可以接收下列属性的任意组合.除⾮其它所有的⽂件属性忽略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
listing.
⽂件被隐藏,它不会在⼀般⽂件夹列表中被装载.
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.
dwFlagAndAttributes可以接受下列标志的任意组合。
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_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.
指⽰系统不使⽤快速缓冲区或缓存,当和FILE_FLAG_OVERLAPPED组合,该标志给出最
⼤的异步操作量, 因为I/O不依赖内存管理器的异步操作.然⽽,⼀些I/O操作将会运⾏
得长⼀些,因为数据没有控制在缓存中.
An application must meet certain requirements when working with files
opened with FILE_FLAG_NO_BUFFERING:
当使⽤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.
⽂件存取的字节数必须是扇区尺⼨的整倍数.例如,如果扇区尺⼨是512字节
程序就可以读或者写512,1024或者2048字节,但不能够是335,981或者7171
字节.

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。