从 VB6/VBA/VBScript 读取大型二进制文件

Reading large binary file from VB6/VBA/VBScript

我正在尝试让旧式 VB6 应用程序读取大型(大于 2GB)二进制文件。我将缓冲区声明为:

Dim TCBuffer as String
TCPBuffer = String(4096, Chr(0))

并使用以下循环读取数据:

Get #FileNum, , TCPBuffer

但是一旦当前位置达到 2GB,上面的行就会引发错误。

Run-time error '63':

Bad record number

所以我想知道是否改用 Scripting.FileSystemObject。但是,这个对象似乎非常有限。一方面,您似乎只能创建和打开文本文件。

关于如何从 VB6 读取大型二进制文件有什么建议吗?

更新:

再考虑一下,另一种选择是使用 API 函数。但是这里 VB6 的真正限制之一是缺少无符号数据类型。所以即使使用 API 函数也是一个技巧。

您最好的选择可能是使用 Windows API 来内存映射文件,使用 CreateFileCreateFileMappingMapViewOfFile 函数.请参阅有关“Creating a File Mapping object" for more information on how the API works, though obviously you'll need to translate the specifics to VB. I did a little searching and found some old articles still around with some pointers, and an archive of sample code from Visual Basic Programmer's Journal(请参阅 "The Persistence Of Memory (July 1996)")的 Microsoft 文档,因此您不是唯一需要执行此操作的人,而且这似乎是一项既定技术。请记住,对于文件在 32 位进程中那么大,您一次只需要将文件的一部分映射到您的地址 space.

我最终创建了一个内部使用 Windows API 的文件 class。

任何接受或returns文件大小或偏移量的函数都属于Double类型。一小段代码将该值与无符号长整数相互转换,即使它在传递给 API 时必须存储在有符号 Long 中(因为 VB6/VBA 不支持无符号长整数).