使用API进行文件读写——CreateFile,ReadFile,WriteFile等
看了这个帖子:
http://www.vbgood.com/thread-99249-1-1.html
就写了一个使用API读写文件的简单类,苦力活。
演示代码在附件里。
'***********************************
'Written by D.L.
'
'2011/04/04
'***********************************
Option Explicit
'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
' lpSecurityDescriptor As Long
' bInheritHandle As Long
'End Type
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 ReadFile Lib "kernel32.dll" (ByVal hFile As Long, ByRef lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, ByRef lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function WriteFile Lib "kernel32.dll" (ByVal hFile As Long, ByRef lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, ByRef lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function FlushFileBuffers Lib "kernel32" (ByVal hFile As Long) As Long
'Private Declare Function GetFileSize Lib "kernel32.dll" (ByVal hFile As Long, ByRef lpFileSizeHigh As Long) As Long
Private Declare Function GetFileSizeEx Lib "kernel32.dll" (ByVal hFile As Long, ByRef lpFileSize As Currency) As Long
Enum DesiredAccess
GENERIC_READ = &H80000000
GENERIC_WRITE = &H40000000
GENERIC_EXECUTE = &H20000000
GENERIC_ALL = &H10000000
End Enum
Enum ShareMode
FILE_SHARE_READ = &H1
FILE_SHARE_WRITE = &H2
FILE_SHARE_DELETE = &H4
End Enum
'This parameter must be one of the following values, which cannot be combined:
Enum CreationDisposition
TRUNCATE_EXISTING = 5
OPEN_ALWAYS = 4
OPEN_EXISTING = 3
CREATE_ALWAYS = 2
CREATE_NEW = 1
End Enum
Enum FlagsAndAttributes
'attributes
FILE_ATTRIBUTE_ARCHIVE = &H20
FILE_ATTRIBUTE_COMPRESSED = &H800
FILE_ATTRIBUTE_DIRECTORY = &H10
FILE_ATTRIBUTE_HIDDEN = &H2
FILE_ATTRIBUTE_NORMAL = &H80 'The file does not have other attributes set. This attribute is valid only if used alone.
FILE_ATTRIBUTE_READONLY = &H1
FILE_ATTRIBUTE_SYSTEM = &H4
FILE_ATTRIBUTE_TEMPORARY = &H100
'flags
FILE_FLAG_BACKUP_SEMANTICS = &H2000000
FILE_FLAG_DELETE_ON_CLOSE = &H4000000
FILE_FLAG_NO_BUFFERING = &H20000000
FILE_FLAG_OVERLAPPED = &H40000000
FILE_FLAG_POSIX_SEMANTICS = &H1000000
FILE_FLAG_RANDOM_ACCESS = &H10000000
FILE_FLAG_SEQUENTIAL_SCAN = &H8000000
FILE_FLAG_WRITE_THROUGH = &H80000000
End Enum
Private Const INVALID_HANDLE_VALUE = -1
'Private Declare Function SetFilePointer Lib "kernel32.dll" (ByVal hFile As Long, ByVal lDistanceToMove As Long, ByRef lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
'Private Const INVALID_SET_FILE_POINTER = -1
'Private Declare Function SetFilePointerEx Lib "kernel32" (ByVal hFile As Long, liDistanceToMove As LARGE_INTEGER, lpNewFilePointer As LARGE_INTEGER, ByVal dwMoveMethod As Long) As Long
'Private Type LARGE_INTEGER
' Lowpart As Long
' Highpart As Long
'End Type
Private Declare Function SetFilePointerEx Lib "kernel32" (ByVal hFile As Long, ByVal liDistanceToMove As Currency, lpNewFilePointer As Currency, ByVal dwMoveMethod As Long) As Long
Enum MoveMethod
FILE_BEGIN = 0
FILE_CURRENT = 1
FILE_END = 2
End Enum
Private Declare Function SetEndOfFile Lib "kernel32" (ByVal hFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
''''''''''''''''''''''''''''''''''''''''''''''''''''
Private m_Handle As Long
Private m_FileName As String
Private Sub Class_Initialize()
Handle = INVALID_HANDLE_VALUE
FileName = ""
End Sub
Private Sub Class_Terminate()
Call FileClose
End Sub
'*******properties*******
Public Property Get Handle() As Long
Handle = m_Handle
End Property
Private Property Let Handle(ByVal Value As Long)
m_Handle = Value
End Property
Public Property Get FileName() As String
FileName = m_FileName
End Property
Private Property Let FileName(ByVal Value As String)
m_FileName = Value
End Property
'*******public functions*******
'FileOpen
'打开文件
Public Function FileOpen(ByVal FileName As String, ByVal CreateIfNotExists As Boolean) As Boolean
Dim dwCreation As Long
If (CreateIfNotExists) Then
dwCreation = OPEN_ALWAYS
Else
dwCreation = OPEN_EXISTING
End If
If (CreateFile2(FileName, GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, dwCreation, 0, 0)) Then
FileOpen = True
Else
FileOpen = False
End If
End Function
'FileSeek
'移动文件指针
Public Function FileSeek(ByVal DistanceToMove As Double, ByVal MoveMethod As MoveMethod) As Boolean
Dim lRet As Long
Dim curIn As Currency, curOut As Currency
If (Handle = INVALID_HANDLE_VALUE) Then Exit Function
curIn = dbl2cur(DistanceToMove)
lRet = SetFilePointerEx(Handle, curIn, curOut, MoveMethod)
If (lRet) Then
FileSeek = True
Else
FileSeek = False
End If
End Function
'FileWrite
'写文件
Public Function FileWrite(Buffer() As Byte) As Boolean
Dim lRet As Long
Dim lBufferLength As Long
Dim lBytesWritten As Long
If (Handle = INVALID_HANDLE_VALUE) Then Exit Function
If (IsArrayInit(Buffer()) = False) Then Exit Function
lBufferLength = UBound(Buffer) - LBound(Buffer) + 1
lRet = WriteFile(Handle, Buffer(0), lBufferLength, lBytesWritten, 0)
If (lRet And lBytesWritten = lBufferLength) Then
'lRet = FlushFileBuffers(Handle)
FileWrite = True
Else
FileWrite = False
End If
End Function
'FileRead
'读文件
Public Function FileRead(Buffer() As Byte) As Boolean
Dim lRet As Long
Dim lBufferLength
Dim lBytesRead As Long
If (Handle = INVALID_HANDLE_VALUE) Then Exit Function
If (IsArrayInit(Buffer()) = False) Then Exit Function
lBufferLength = UBound(Buffer) - LBound(Buffer) + 1
lRet = ReadFile(Handle, Buffer(0), lBufferLength, lBytesRead, 0)
If (lRet) Then
FileRead = True
Else
FileRead = False
End If
End Function
'FileClose
'关闭文件
Public Function FileClose() As Boolean
Dim lRet As Long
If (Handle = INVALID_HANDLE_VALUE) Then Exit Function
lRet = CloseHandle(Handle)
If (lRet) Then
Handle = INVALID_HANDLE_VALUE
FileName = ""
FileClose = True
End If
End Function
'CreateFile2
'创建文件,同 CreateFile API 函数,这个函数可以不暴露
Public Function CreateFile2(ByVal lpFileName As String, ByVal dwDesiredAccess As DesiredAccess, ByVal dwShareMode As ShareMode, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As CreationDisposition, ByVal dwFlagsAndAttributes As FlagsAndAttributes, ByVal hTemplateFile As Long) As Boolean
'The lpFileName string should be //./x: to open a floppy drive x or a partition x on a hard disk.For example:
'
'String Meaning
'//./A: Obtains a handle to drive A on the user's computer.
'//./C: Obtains a handle to drive C on the user's computer.
m_FileName = lpFileName
Handle = CreateFile(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile)
CreateFile2 = IIf(Handle <> INVALID_HANDLE_VALUE, True, False)
End Function
'FileGetSize
'取得文件大小(字节)
Public Function FileGetSize(Size As Double) As Boolean
Dim lRet As Long
Dim curOut As Currency
If (Handle = INVALID_HANDLE_VALUE) Then Exit Function
lRet = GetFileSizeEx(Handle, curOut)
If (lRet) Then
Size = cur2dbl(curOut)
FileGetSize = True
End If
End Function
'FileSetSize
'指定文件大小(字节)
Public Function FileSetSize(ByVal Size As Double) As Boolean
Dim lRet As Long
Dim curOut As Currency
If (Size < 0) Then Exit Function
If (Handle = INVALID_HANDLE_VALUE) Then Exit Function
lRet = SetFilePointerEx(Handle, dbl2cur(Size), curOut, FILE_BEGIN)
If (lRet) Then
lRet = SetEndOfFile(Handle)
If (lRet) Then
FileSetSize = True
End If
End If
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Function cur2dbl(cur As Currency) As Double
cur2dbl = cur * 10000
End Function
Private Function dbl2cur(dbl As Double) As Currency
dbl2cur = dbl / 10000
End Function
Private Function IsArrayInit(ByRef lpsa() As Byte) As Boolean
Dim lRet As Long
IsArrayInit = True
Err.Clear
On Error Resume Next
lRet = LBound(lpsa())
If (Err.Number) Then
Err.Clear
IsArrayInit = False
End If
End Function
复制代码
参考链接:http://hi.baidu.com/hnxyy/blog/item/e77c3f87db17612ac65cc3b3.html
---------------------
作者:dahual
来源:CSDN
原文:https://blog.csdn.net/dahual/article/details/6327998
版权声明:本文为博主原创文章,转载请附上博文链接!
使用API进行文件读写——CreateFile,ReadFile,WriteFile等的更多相关文章
- 用Windows API函数(CreateFile/ReadFile/WriteFile/CloseHandle)完成文件拷贝程序(初级版)
文件拷贝程序 程序类型:Console 参数:源文件名 目的文件名 要求:1.只能使用Windows API函数(CreateFile/ReadFile/WriteFile/CloseHandle ...
- 使用CreateFile, ReadFile, WriteFile在Windows NT/2000/XP下读写绝对扇区的方法
也就是在CreateFile的时候打开文件名指定: "\\.\Device"就可以了. 因为代码比较短, 所以我不做注释, 相信大家看代码就能明白意思了. 另外这里读写的都是软盘A ...
- MT4调用Windows API进行文件读写操作
/*导入相关函数*/ #import "kernel32.dll" int CreateDirectoryW(string directoryName,int type); int ...
- 关于Windows文件读写_暗涌_新浪博客
关于Windows文件读写_暗涌_新浪博客 这几天在研究怎么才能加快windows文件读写速度,搜了很多文章,MSDN也看了不少.稍微给大家分享一下. 限制windows文件读写速度的 ...
- 【API】文件操作编程基础-CreateFile、WriteFile、SetFilePointer
1.说明 很多黑客工具的实现是通过对文件进行读写操作的,而文件读写操作实质也是对API函数的调用. 2.相关函数 CreateFile : 创建或打开文件或I/O设备.最常用的I/O设备如下:文件,文 ...
- paip.文件读写api php java python总结.txt
paip.文件读写api php java python总结.txt 一.多种方式读文件内容. 1.按字节读取文件内容 以字节为单位读取文件,常用于读二进制文件,如图片.声音.影像等文件. ...
- 【iOS】线程安全的文件读写
前段时间看了一遍GCD(Grand Central Dispatch)多线程,GCD是苹果为多核开发提供的解决方案 多线程最常见的问题就是读写,比如数据库读写,文件读写,读取是共享的,写是互斥,允许多 ...
- [js高手之路]node js系列课程-创建简易web服务器与文件读写
web服务器至少有以下几个特点: 1.24小时不停止的工作,也就是说这个进程要常驻在内存中 2.24小时在某一端口监听,如: http://localhost:8080, www服务器默认端口80 3 ...
- android菜鸟学习笔记17----Android数据存储(一)文件读写
假如有如下需求,要求能够记录用户输入的用户名和密码,下次登录时,能直接获取之前保存的用户名密码,并在相应的EditText中显示. 要保存用户输入的数据,最先想到的应该就是文件读写了. 通过对andr ...
随机推荐
- vue使用JSEncrypt实现rsa加密及挂载方法
挂载全局方法 使用jsencrypt进行rsa加密 原文链接:Js参数RSA加密传输,jsencrypt.js的使用 - CSDN博客* https://blog.csdn.net/p31201115 ...
- Android中动态改变Listview中字体的颜色
效果如下: 账目显示用的是Listview,要实现的功能为使其根据所在Item是“收入”还是“支出”来把数字设置成绿色或红色 方法是自定义适配器,并重写其中getView()函数,实现如下: //自定 ...
- 剑指offer-面试题64-求1+2+...+n-发散思维
/* 题目: 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C) */ /* 思路: 递归. */ #incl ...
- Excel_b_1
1.Excel简介 数据处理软件,lotus兼容(文件,选项,高级,拉到底,lotus,) 2.Excel功能 数据存储,数据处理,数据分析,数据呈现 3.具体功能 重新认识了Excel,选项,高级选 ...
- opencv —— 在 VS 中的配置
添加一个新的 .cpp 文件到工程中 打开菜单栏视图中的属性管理器 选择 Debug|x64 ...
- SPFA的优化一览
目录 序 内容 嵬 序 spfa,是一个早已没人用的算法,就像那些麻木的人, 可谁有知道,他何时槃涅 一个已死的算法 ,重生 内容 关于\(NOI2018D1T1\)的惨案,为了以防spfa被卡. 关 ...
- Python3中的支持向量机SVM的使用(有实例)
https://www.cnblogs.com/luyaoblog/p/6775342.html 首先,我们需要安装scikit-learn 一.导入sklearn算法包 在python中导入sci ...
- 【pattern】设计模式(2) - 模版方法模式
前言 一晃一年又过了,还是一样的渣. 一晃2周又过去了,还是没有坚持写博客. 本来前2天说填一下SQL注入攻击的坑,结果看下去发现还是ojdbc.jar中的代码,看不懂啊.这坑暂时填不动,强迫在元旦最 ...
- 回味Ubuntu10.10致敬Gnome桌面
目录 Ubuntu10.10可用源 Ubuntu10.10更新语言包 输入法支持 浏览器选择 文件下载 压缩文件中文乱码的处理 视频播放 科学计算 搭建Lamp环境 实现文件分享 主题美化 Ubunt ...
- react-发表评论案例
评论列表组件 import React from 'react' import CMTItem from './CmtItem.jsx' import CMTBox from './CmtBox.js ...