Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays VB.NET将二进制文件读入结构数组_Arrays_Vb.net_Structure_Binaryfiles - Fatal编程技术网

Arrays VB.NET将二进制文件读入结构数组

Arrays VB.NET将二进制文件读入结构数组,arrays,vb.net,structure,binaryfiles,Arrays,Vb.net,Structure,Binaryfiles,我尝试过使用BinaryReader,但这肯定不起作用。在第一个读卡器上,它几乎是塞满了东西 所以,我改变了策略,尝试使用FILEOPEN、FILEGETOBJECT、FILEPUTOBJECT 这是第一张“唱片”,但从那时起,我开始 “试图读取或写入受保护的内存。这通常表示其他内存已损坏” 以下是迄今为止的代码: Dim iFreeFile As Integer = FreeFile() Dim iFileLength As Integer ' open the file iFreeFile

我尝试过使用BinaryReader,但这肯定不起作用。在第一个读卡器上,它几乎是塞满了东西

所以,我改变了策略,尝试使用FILEOPEN、FILEGETOBJECT、FILEPUTOBJECT

这是第一张“唱片”,但从那时起,我开始

“试图读取或写入受保护的内存。这通常表示其他内存已损坏”

以下是迄今为止的代码:

Dim iFreeFile As Integer = FreeFile()
Dim iFileLength As Integer

' open the file
iFreeFile = FreeFile()
FileOpen(iFreeFile, inFilePath, OpenMode.Binary, OpenAccess.Read, OpenShare.Default)
iFileLength = FileLen(inFilePath)

' This bit reads the data
Dim recordLength As Integer = Marshal.SizeOf(outValue)
Dim myBytes As Byte() : ReDim myBytes(recordLength)

FileGet(iFreeFile, myBytes, inRecordCount)
BuildStr(myBytes, outValue.GetType, outValue)
inRecordCount += 1
outValue是几种结构之一

''' <summary>
''' ' Marshal the Byte Array to the Structure
''' </summary>
''' <param name="Buff">Array of Bytes</param>
''' <param name="MyType">Type of the Structure</param>
''' <param name="outBuffer">Structure Object</param>
Private Sub BuildStr(ByVal Buff() As Byte,
                     ByVal MyType As System.Type,
                     ByRef outBuffer As Object)
    ' Marshal the Byte Array to the Structure

    Dim MyGC As GCHandle = GCHandle.Alloc(Buff, GCHandleType.Pinned)
    'Marshals data from an unmanaged block of memory 
    'to a newly allocated managed object of the specified type.
    outBuffer = Marshal.PtrToStructure(MyGC.AddrOfPinnedObject, MyType) ' <----- here we get the error
End Sub
“”
''将字节数组封送到结构
''' 
''字节数组
“结构类型”
''结构对象
私有子构建str(ByVal Buff()作为字节,
ByVal MyType作为System.Type,
ByRef(作为对象)
'将字节数组封送到结构
作为GCHandle=GCHandle.Alloc的Dim MyGC(Buff,GCHandleType.pinted)
'封送来自非托管内存块的数据
'到新分配的指定类型的托管对象。

exputffer=Marshal.PtrToStructure(MyGC.AddrOfPinnedObject,MyType)按照使方法易于工作的可能性降序排列:

选项1用VB6编写一个小程序,以更通用的格式(如XML)写入数据。这样,在VB.NET中阅读起来就不那么困难了

选项2在VB.NET中使用。请务必阅读该文档中的所有备注,并首先回顾VB6代码是如何编写数据的。如果FileGet不能使用Option Strict On(我假设您正在使用),那么将读取该文件的类放在一个单独的类文件中,这样您就可以对该类使用Option Strict Off

选项3一次读取一个字节数组中的文件,并对其进行解析。如果VB6以与在RAM中存储数据相同的方式将数据存储到磁盘,则可能有用


此外,您还应按如下方式组织文件流和读取器的使用,以确保所有内容都被整齐地处理:

Using oFS As New FileStream(filePath, FileMode.Open, FileAccess.Read)
    Using oBR As New BinaryReader(oFS)

    ' reading code goes here

    End Using
End Using

p.S.Comparitave->compariative.

我们是PS,是的,我知道,我转换的是其他人的代码(和拼写),他们是失语症患者,拼写有严重问题,让寻找单词的生活很难说!谢谢你的提示,我会仔细阅读文档并尝试一下。。。