[求助]在VB中利用内存映射文件读取大数据文件

fa7274 2004-08-11 04:57:39
最好有源代码。
...全文
257 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
fa7274 2004-08-13
  • 打赏
  • 举报
回复
已经搞定,谢谢两位。
不过还有个疑问,我用二进制方式打开一个100MB的文件,用了两分钟。但用内存映射用了两分半钟。不知是为什么,难道用内存映射还不如普通二进制打开吗?
用二进制打开,时间主要在REDIM数组和GET上面。
用内存映射,时间主要在REDIM数组和COPYMEMORY上面。
感觉COPYMEMORY还不如GET啊。
我的PC:CPU 500 ,内存 128 ,系统 WINME
fa7274 2004-08-12
  • 打赏
  • 举报
回复
兄弟,代码不全啊。
fa7274 2004-08-12
  • 打赏
  • 举报
回复
谢谢楼上两位,回去试一下。
James0001 2004-08-11
  • 打赏
  • 举报
回复
一点建议,

'hFile = CreateFile(StrFileName, GENERIC_WRITE Or GENERIC_READ, _
' FILE_SHARE_WRITE, 0, CREATE_ALWAYS, _
' 0, 0)
'改为
hFile = CreateFile(StrFileName, GENERIC_WRITE Or GENERIC_READ, _
FILE_SHARE_WRITE, 0, OPEN_EXISTING, _
0, 0)

'hMapSend = CreateFileMapping(hFile, 0, 2, 0, 0, VarPtr(strMapName))
'改为
hMapSend = CreateFileMapping(hFile, 0, 2, 0, 0, vbnullstring)

'If hAddr Then UnmapViewOfFile hAddr
'改为
If hAddr Then UnmapViewOfFile byval hAddr
落伍者 2004-08-11
  • 打赏
  • 举报
回复
标准模块
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Public 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
Public Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, ByVal lpBuffer As String, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long

Public Declare Function CreateFileMapping Lib "kernel32" Alias "CreateFileMappingA" (ByVal hFile As Long, ByVal lpFileMappigAttributes As Long, ByVal flProtect As Long, ByVal dwMaximumSizeHigh As Long, ByVal dwMaximumSizeLow As Long, ByVal lpName As String) As Long
Public Declare Function MapViewOfFileEx Lib "kernel32" (ByVal hFileMappingObject As Long, ByVal dwDesiredAccess As Long, ByVal dwFileOffsetHigh As Long, ByVal dwFileOffsetLow As Long, ByVal dwNumberOfBytesToMap As Long, lpBaseAddress As Any) As Long
Public Declare Function OpenFileMapping Lib "kernel32" Alias "OpenFileMappingA" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal lpName As String) As Long
Public Declare Function UnmapViewOfFile Lib "kernel32" (lpBaseAddress As Any) As Long


窗体
Dim hFile As Long
Dim hFileMap As Long
Dim StrFileName As String
Dim strMapName As String
Dim hAddr As Long
Dim strbuffer As String
Dim lngWrited As Long

StrFileName = App.Path & "\MapTest.aab"
hFile = CreateFile(StrFileName, GENERIC_WRITE Or GENERIC_READ, _
FILE_SHARE_WRITE, 0, CREATE_ALWAYS, _
0, 0)

If (hFile < 0) Then
Debug.Print GetLastError
Exit Sub
End If
hMapSend = CreateFileMapping(hFile, 0, 2, 0, 0, VarPtr(strMapName))
If (hMapSend = 0) Then Debug.Print GetLastError
hAddr = MapViewOfFile(hFileMap, FILE_MAP_WRITE, 0, 0, 0)
Debug.Print hAddr
'CopyMemory hAddr, ByVal "abc", 3 'haddr是在内存中的地址,读数据用copymemory
If hAddr Then UnmapViewOfFile hAddr
CloseHandle hMapSend
CloseHandle hFile

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧