vb中createfile返回-1
发布网友
发布时间:2022-04-12 20:34
我来回答
共2个回答
热心网友
时间:2022-04-12 22:04
把这段放在Command1_Click试试,我试了,可以通过
dim lngHandle As Long
lngHandle = CreateFile("C:\email126.txt", GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0) '创建文件
实例
'This project needs a Common Dialog box, named CDBox.
' (To add the Common Dialog Box to your tools menu, go to Project->Components (or press CTRL-T)
' and select Microsoft Common Dialog control)
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
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
Private Declare Function SetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
Private Sub Form_Load()
Dim m_Date As Date, lngHandle As Long
Dim udtFileTime As FILETIME
Dim udtLocalTime As FILETIME
Dim udtSystemTime As SYSTEMTIME
m_Date = Format(Now, "DD-MM-YY")
'Set the dialog's title
CDBox.DialogTitle = "Choose a file ..."
'Set the dialog's filter
CDBox.Filter = "All Files (*.*)|*.*"
'Show the 'Open File'-dialog
CDBox.ShowOpen
udtSystemTime.wYear = Year(m_Date)
udtSystemTime.wMonth = Month(m_Date)
udtSystemTime.wDay = Day(m_Date)
udtSystemTime.wDayOfWeek = WeekDay(m_Date) - 1
udtSystemTime.wHour = Hour(m_Date)
udtSystemTime.wSecond = Second(m_Date)
udtSystemTime.wMilliseconds = 0
' convert system time to local time
SystemTimeToFileTime udtSystemTime, udtLocalTime
' convert local time to GMT
LocalFileTimeToFileTime udtLocalTime, udtFileTime
' open the file to get the filehandle
lngHandle = CreateFile(CDBox.Filename, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
' change date/time property of the file
SetFileTime lngHandle, udtFileTime, udtFileTime, udtFileTime
' close the handle
CloseHandle lngHandle
MsgBox "The date of the file '" + CDBox.Filename + "' has been changed to" + Str$(m_Date), vbInformation + vbOKOnly, App.Title
End Sub
附:
【VB声明】
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
【别名】
CreateFileA
【说明】
这是一个全功能的例程,可打开和创建文件、管道、邮槽、通信服务、设备以及控制台
【返回值】
Long,如执行成功,则返回文件句柄。INVALID_HANDLE_VALUE表示出错,会设置GetLastError。即使函数成功,但若文件存在,且指定了CREATE_ALWAYS
或 OPEN_ALWAYS,GetLastError也会设为ERROR_ALREADY_EXISTS
【备注】
打开一个通信端口时(如COM1),无论如何都要设置成
OPEN_EXISTING
这个函数代替了lOpen 和 lCreate函数,应该是我们的首选
【参数表】
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,如果不为零,则指定一个文件句柄。新文件将从这个文件中复制扩展属性
热心网友
时间:2022-04-12 23:22
因为你用了 OPEN_EXISTING,前提必须要有c:\email126.txt这个文件才能成功,如果你第一次运行,肯定是-1,如果只是创建一个新文件,要用 CREATE_ALWAYS 或 OPEN_ALWAYS
如:
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const CREATE_ALWAYS As Long = 2
Private Const OPEN_ALWAYS As Long = 4
lngHandle = CreateFile("C:\email126.txt", GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, CREATE_ALWAYS, 0, 0) '创建文件
Call CloseHandle(lngHandle) '关闭lngHandle