我想在D:\haha下创建一个只读的纯文本文件,用createfile来实现,但lpSecurityAttributes参数不知道怎么设置,高手帮帮啊。
最好解释一下为什么,别让我换别的,我是在学习怎么用这个。
我用过null和byval 0&,都不行。
我要的不是这个函数的参数列表……而且这个对我问题没帮助……
声明Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
说明
这是一个全功能的例程,可打开和创建文件、管道、邮槽、通信服务、设备以及控制台
返回值
Long,如执行成功,则返回文件句柄。INVALID_HANDLE_VALUE表示出错,会设置GetLastError。即使函数成功,但若文件存在,且指定了CREATE_ALWAYS 或 OPEN_ALWAYS,GetLastError也会设为ERROR_ALREADY_EXISTS
参数表
参数 类型及说明
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 讲现有文件缩短为零长度
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
hTemplateFile Long,如果不为零,则指定一个文件句柄。新文件将从这个文件中复制扩展属性
注解
打开一个通信端口时(如COM1),无论如何都要设置成 OPEN_EXISTING
这个函数代替了lOpen 和 lCreate函数,应该是我们的首选
希望这个对你有帮助,把这个API函数的使用方法完全告诉了你,你可以对照自己的需要进行使用!
说明
这是一个全功能的例程,可打开和创建文件、管道、邮槽、通信服务、设备以及控制台
返回值
Long,如执行成功,则返回文件句柄。INVALID_HANDLE_VALUE表示出错,会设置GetLastError。即使函数成功,但若文件存在,且指定了CREATE_ALWAYS 或 OPEN_ALWAYS,GetLastError也会设为ERROR_ALREADY_EXISTS
参数表
参数 类型及说明
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 讲现有文件缩短为零长度
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
hTemplateFile Long,如果不为零,则指定一个文件句柄。新文件将从这个文件中复制扩展属性
注解
打开一个通信端口时(如COM1),无论如何都要设置成 OPEN_EXISTING
这个函数代替了lOpen 和 lCreate函数,应该是我们的首选
希望这个对你有帮助,把这个API函数的使用方法完全告诉了你,你可以对照自己的需要进行使用!
温馨提示:答案为网友推荐,仅供参考
第1个回答 推荐于2016-01-29
试着参考下面的代码
Public Const GENERIC_READ = &H80000000
Const FILE_ATTRIBUTE_NORMAL = &H80
Const CREATE_ALWAYS = 2
Const OPEN_ALWAYS = 4
Const INVALID_HANDLE_VALUE= -1
Const FILE_NAME = "TEST.DAT" 'This can be any file that does not
'currently exist.
Type MyType
value As Integer
End Type
Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, _
lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, _
lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" ( _
ByVal hObject As Long) As Long
Private Declare Function WriteFile Lib "kernel32" ( _
ByVal hFile As Long, lpBuffer As Any, _
ByVal nNumberOfBytesToWrite As Long, _
lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function CreateFile Lib "kernel32" _
Alias "CreateFileA" (ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, _
ByVal lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) _
As Long
Declare Function FlushFileBuffers Lib "kernel32" ( _
ByVal hFile As Long) As Long
Sub fillArray(anArray() As MyType)
Dim x As Integer
For x = 0 To UBound(anArray)
anArray(x).value = x
Next x
End Sub
Sub Main()
Dim fHandle As Integer
Dim T(1000) As MyType 'Define a large array of data
Dim S(1000) As MyType 'Define another large array
fillArray T 'Fill the array with some values
writearray FILE_NAME, T 'Write the entire array to disk
readArray FILE_NAME, S 'Read into a different array
End Sub
Sub readArray(Fname As String, anArray() As MyType)
Dim fHandle As Long
Dim fSuccess As Long
Dim sTest As String
Dim lBytesRead As Long
Dim BytesToRead As Long
'Get size of data to write
BytesToRead = (UBound(anArray) + 1) * LenB(anArray(0))
'Get a handle to a file Fname.
fHandle = CreateFile(Fname, GENERIC_WRITE Or GENERIC_READ, _
0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
'Here you should test to see if you get a file handle or not.
'CreateFile returns INVALID_HANDLE_VALUE if it fails.
If fhandle <> INVALID_HANDLE_VALUE Then
fSuccess = ReadFile(fHandle, anArray(LBound(anArray)), _
BytesToRead, lBytesRead, 0)
'ReadFile returns a non-zero value if it is successful.
'Now you just close the file.
fSuccess = CloseHandle(fHandle)
End If
End Sub
Sub writearray(Fname As String, anArray() As MyType)
Dim fHandle As Long
Dim fSuccess As Long
Dim sTest As String
Dim lBytesWritten As Long
Dim BytesToWrite As Long
'Get the length of data to write
BytesToWrite = (UBound(anArray) + 1) * LenB(anArray(0))
'Get a handle to a file Fname.
fHandle = CreateFile(Fname, GENERIC_WRITE Or GENERIC_READ, _
0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
'Here you should test to see if you get a file handle or not.
'CreateFile returns INVALID_HANDLE_VALUE if it fails.
If fhandle <> INVALID_HANDLE_VALUE Then
fSuccess = WriteFile(fHandle, anArray(LBound(anArray)), _
BytesToWrite, lBytesWritten, 0)
'Check to see if you were successful writing the data
If fSuccess <> 0 Then
'Flush the file buffers to force writing of the data.
fSuccess = FlushFileBuffers(fHandle)
'Close the file.
fSuccess = CloseHandle(fHandle)
End If
End If
End Sub
第2个回答 推荐于2016-09-06
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Type SECURITY_ATTRIBUTES
nLength As Long
bInheritHandle As Long
lpSecurityDescriptor As Long
End Type
Const GENERIC_WRITE = &H40000000
Const CREATE_NEW = 1
Const FILE_ATTRIBUTE_READONLY = &H1
Private Sub Form_Load()
Dim temp As SECURITY_ATTRIBUTES
MsgBox (CreateFile("C:\abc.txt", GENERIC_WRITE, 0, temp, CREATE_NEW, FILE_ATTRIBUTE_READONLY, 0))
End Sub本回答被提问者采纳
Private Type SECURITY_ATTRIBUTES
nLength As Long
bInheritHandle As Long
lpSecurityDescriptor As Long
End Type
Const GENERIC_WRITE = &H40000000
Const CREATE_NEW = 1
Const FILE_ATTRIBUTE_READONLY = &H1
Private Sub Form_Load()
Dim temp As SECURITY_ATTRIBUTES
MsgBox (CreateFile("C:\abc.txt", GENERIC_WRITE, 0, temp, CREATE_NEW, FILE_ATTRIBUTE_READONLY, 0))
End Sub本回答被提问者采纳