易语言API学习CREATEFILEA 用法

函数原型:

HANDLE CreateFileA(
  LPCSTR                lpFileName,
  DWORD                 dwDesiredAccess,
  DWORD                 dwShareMode,
  LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  DWORD                 dwCreationDisposition,
  DWORD                 dwFlagsAndAttributes,
  HANDLE                hTemplateFile
);

参数

  lpFileName String 要打开的文件的名字

  dwDesiredAccess Long 如果为 GENERIC_READ 表示允许对设备进行读访问;如果为 GENERIC_WRITE 表示允许对设备进行写访问(可组合使用);如果为零,表示只允许获取与一个设备有关的信息

  dwShareMode Long, 零表示不共享; FILE_SHARE_READ 和/或 FILE_SHARE_WRITE 表示允许对文件进行共享访问

  lpSecurityAttributes SECURITY_ATTRIBUTES, 指向一个SECURITY_ATTRIBUTES结构的指针,定义了文件的安全特性(如果操作系统支持的话)

  dwCreationDisposition Long,下述常数之一: 

This parameter must be one of the following values, which cannot be combined:
 
Value	Meaning
CREATE_ALWAYS
2
Creates a new file, always.
If the specified file exists and is writable, the function overwrites the file, the function succeeds, and last-error code is set to ERROR_ALREADY_EXISTS (183).
 
If the specified file does not exist and is a valid path, a new file is created, the function succeeds, and the last-error code is set to zero.
 
For more information, see the Remarks section of this topic.
 
CREATE_NEW
1
Creates a new file, only if it does not already exist.
If the specified file exists, the function fails and the last-error code is set to ERROR_FILE_EXISTS (80).
 
If the specified file does not exist and is a valid path to a writable location, a new file is created.
 
OPEN_ALWAYS
4
Opens a file, always.
If the specified file exists, the function succeeds and the last-error code is set to ERROR_ALREADY_EXISTS (183).
 
If the specified file does not exist and is a valid path to a writable location, the function creates a file and the last-error code is set to zero.
 
OPEN_EXISTING
3
Opens a file or device, only if it exists.
If the specified file or device does not exist, the function fails and the last-error code is set to ERROR_FILE_NOT_FOUND (2).
 
For more information about devices, see the Remarks section.
 
TRUNCATE_EXISTING
5
Opens a file and truncates it so that its size is zero bytes, only if it exists.
If the specified file does not exist, the function fails and the last-error code is set to ERROR_FILE_NOT_FOUND (2).
 
The calling process must open the file with the GENERIC_WRITE bit set as part of the dwDesiredAccess parameter.

  dwFlagsAndAttributes Long, 一个或多个下述常数 

  FILE_ATTRIBUTE_ARCHIVE 标记归档属性

  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

FILE_ATTRIBUTE_ARCHIVE
32 (0x20)
The file should be archived. Applications use this attribute to mark files for backup or removal.
FILE_ATTRIBUTE_ENCRYPTED
16384 (0x4000)
The file or directory is encrypted. For a file, this means that all data in the file is encrypted. For a directory, this means that encryption is the default for newly created files and subdirectories. For more information, see File Encryption.
This flag has no effect if FILE_ATTRIBUTE_SYSTEM is also specified.
 
This flag is not supported on Home, Home Premium, Starter, or ARM editions of Windows.
 
FILE_ATTRIBUTE_HIDDEN
2 (0x2)
The file is hidden. Do not include it in an ordinary directory listing.
FILE_ATTRIBUTE_NORMAL
128 (0x80)
The file does not have other attributes set. This attribute is valid only if used alone.
FILE_ATTRIBUTE_OFFLINE
4096 (0x1000)
The data of a file is not immediately available. This attribute indicates that file data is physically moved to offline storage. This attribute is used by Remote Storage, the hierarchical storage management software. Applications should not arbitrarily change this attribute.
FILE_ATTRIBUTE_READONLY
1 (0x1)
The file is read only. Applications can read the file, but cannot write to or delete it.
FILE_ATTRIBUTE_SYSTEM
4 (0x4)
The file is part of or used exclusively by an operating system.
FILE_ATTRIBUTE_TEMPORARY
256 (0x100)
The file is being used for temporary storage.
For more information, see the Caching Behavior section of this topic.
 
 
Flag	Meaning
FILE_FLAG_BACKUP_SEMANTICS
0x02000000
The file is being opened or created for a backup or restore operation. The system ensures that the calling process overrides file security checks when the process has SE_BACKUP_NAME and SE_RESTORE_NAME privileges. For more information, see Changing Privileges in a Token.
You must set this flag to obtain a handle to a directory. A directory handle can be passed to some functions instead of a file handle. For more information, see the Remarks section.
 
FILE_FLAG_DELETE_ON_CLOSE
0x04000000
The file is to be deleted immediately after all of its handles are closed, which includes the specified handle and any other open or duplicated handles.
If there are existing open handles to a file, the call fails unless they were all opened with the FILE_SHARE_DELETE share mode.
 
Subsequent open requests for the file fail, unless the FILE_SHARE_DELETE share mode is specified.
 
FILE_FLAG_NO_BUFFERING
0x20000000
The file or device is being opened with no system caching for data reads and writes. This flag does not affect hard disk caching or memory mapped files.
There are strict requirements for successfully working with files opened with CreateFile using the FILE_FLAG_NO_BUFFERING flag, for details see File Buffering.
 
FILE_FLAG_OPEN_NO_RECALL
0x00100000
The file data is requested, but it should continue to be located in remote storage. It should not be transported back to local storage. This flag is for use by remote storage systems.
FILE_FLAG_OPEN_REPARSE_POINT
0x00200000
Normal reparse point processing will not occur; CreateFile will attempt to open the reparse point. When a file is opened, a file handle is returned, whether or not the filter that controls the reparse point is operational.
This flag cannot be used with the CREATE_ALWAYS flag.
 
If the file is not a reparse point, then this flag is ignored.
 
For more information, see the Remarks section.
 
FILE_FLAG_OVERLAPPED
0x40000000
The file or device is being opened or created for asynchronous I/O.
When subsequent I/O operations are completed on this handle, the event specified in the OVERLAPPED structure will be set to the signaled state.
 
If this flag is specified, the file can be used for simultaneous read and write operations.
 
If this flag is not specified, then I/O operations are serialized, even if the calls to the read and write functions specify an OVERLAPPED structure.
 
For information about considerations when using a file handle created with this flag, see the Synchronous and Asynchronous I/O Handles section of this topic.
 
FILE_FLAG_POSIX_SEMANTICS
0x0100000
Access will occur according to POSIX rules. This includes allowing multiple files with names, differing only in case, for file systems that support that naming. Use care when using this option, because files created with this flag may not be accessible by applications that are written for MS-DOS or 16-bit Windows.
FILE_FLAG_RANDOM_ACCESS
0x10000000
Access is intended to be random. The system can use this as a hint to optimize file caching.
This flag has no effect if the file system does not support cached I/O and FILE_FLAG_NO_BUFFERING.
 
For more information, see the Caching Behavior section of this topic.
 
FILE_FLAG_SESSION_AWARE
0x00800000
The file or device is being opened with session awareness. If this flag is not specified, then per-session devices (such as a device using RemoteFX USB Redirection) cannot be opened by processes running in session 0. This flag has no effect for callers not in session 0. This flag is supported only on server editions of Windows.
Windows Server 2008 R2 and Windows Server 2008:  This flag is not supported before Windows Server 2012.
 
FILE_FLAG_SEQUENTIAL_SCAN
0x08000000
Access is intended to be sequential from beginning to end. The system can use this as a hint to optimize file caching.
This flag should not be used if read-behind (that is, reverse scans) will be used.
 
This flag has no effect if the file system does not support cached I/O and FILE_FLAG_NO_BUFFERING.
 
For more information, see the Caching Behavior section of this topic.
 
FILE_FLAG_WRITE_THROUGH
0x80000000
Write operations will not go through any intermediate cache, they will go directly to disk.
For additional information, see the Caching Behavior section of this topic.

返回值

  如执行成功,则返回文件句柄。

易语言调用:

.版本 2
 
.DLL命令 CreateFileA, 整数型, "kernel32", "CreateFileA"
    .参数 lpFileName, 文本型, , 0//指向文件名的指针
    .参数 dwDesiredAccess, 整数型, , 0//访问模式(写/读)
    .参数 dwShareMode, 整数型, , 0 //共享模式
    .参数 lpSecurityAttributes, 整数型, , 0//指向安全属性的指针
    .参数 dwCreationDisposition, 整数型, , 0//如何创建
    .参数 dwFlagsAndAttributes, 整数型, , 0 //文件属性
    .参数 hTemplateFile, 整数型, , 0//用于复制文件句柄

CreateFileA function (fileapi.h) | Microsoft Docs
https://docs.microsoft.com/zh-cn/windows/desktop/api/fileapi/nf-fileapi-createfilea


发布日期:

所属分类: 易语言 标签: