CreateFile(createfile2于动态链接库kernel32dll)
开机弹出CreateFile错误怎么解决?
左点击开始菜单,点击“运行”输入,msconfig,回车,打开“系统配置实用程序,点”启动“项,留ctmon,与杀毒(如果有)其它全不要,去勾。
使用CreateFile创建的文件位置很奇怪
尽量避免使用相对路径, 相对路径是从运行时的“当前”路径开始的, 从图形界面运行起来的程序的当前路径就是在文档那里
为什么CreateFile无法打开驱动
应该是文件写入权限设置错误引起的,用系统管理员账号登入系统后安装试试,或在系统管理员权限下找到你项目的文件夹,右键选择"属性",然后点击"完全"选项卡,点击"添加","高级","立即查找",找到平时登入所用帐户,然后"确定",最后再为登入所用帐户户勾选"写入权限",
如果系统进程的某些文件比如注册表的SAM文件等等都是被系统独占打开的,你要能用CreateFile函数轻松打开这些文件那系统的安全成什么了?明确告诉你想要用这个函数打开被独占的文件想都别想了。如此重量级的函数微软肯定对它的安全性做了严密的测试,所以放弃吧。
c语言createfile函数用法
1、CreateFile 是一个多功能的函数,可打开或创建以下对象,并返回可访问的句柄:控制台,通信资源,目录(只读打开),磁盘驱动器,文件,邮槽,管道。
2、例程:
#include?windows.h
int?main()
{
????HANDLE?hf?=?CreateFile(TEXT("C:\\testa.bin"),?GENERIC_WRITE,?0,
????????0,?CREATE_ALWAYS,?0,?0);
????DWORD?written;
????WriteFile(hf,?"\x0f\xff\xff\xff\xff\xff",?6,?written,?0);
????CloseHandle(hf);
????hf?=?CreateFile(TEXT("c:\\testb.bin"),?GENERIC_WRITE,?0,
????????0,?OPEN_ALWAYS,?0,?0);
????SetFilePointer(hf,?0,?0,?FILE_END);
????WriteFile(hf,?"\x0f\xff\xff\xff\xff\xff",?6,?written,?0);
????CloseHandle(hf);
????return?0;
}
vb60编程中,如何用API函数中的createfile()创建一个文件?
分类: 电脑/网络 程序设计 其他编程语言
问题描述:
vb60编程中,如何用API函数中的createfile()创建一个文件?能否给出具体的代码?我要创建的是一个text文件,因为这个函数的参数太多了,看不懂?
解析:
实例
'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 = H***********
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,如果不为零,则指定一个文件句柄。新文件将从这个文件中复制扩展属性