Reading a binary file created with VB6

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mr X

    #1

    Reading a binary file created with VB6

    One of my employer's products, written in VB6, reads and writes binary
    files. We are shortly going to port to VB.NET (framework version 2.0) and
    will need to be able to read and write these binary files from .NET. The
    file format cannot be changed, so the .NET code will need to mimic the
    read/write VB6 stuff.

    I am am using a BinaryReader to read the data in .NET. In VB6, the data is
    read with "Get" and retrieved with "Put", in a file opened/created in binary
    mode.

    I am having trouble working out how VB6 stores certain types of data. Below
    is some VB6 code that mimics the write behaviour:

    Option Explicit

    Private Type MyAttrib
    sId As String
    End Type

    Private Type MyType
    Attribs() As MyAttrib
    End Type

    Private Sub Form_Load()
    Dim ff As Integer
    ff = FreeFile()

    On Error Resume Next
    Kill "C:\test.tx t"
    On Error GoTo 0

    Open "C:\test.tx t" For Binary As ff

    Dim count As Integer
    count = 2

    Dim mt As MyType
    ReDim mt.Attribs(coun t)

    Dim i As Integer
    For i = 0 To count - 1
    mt.Attribs(i).s Id = "Identifier " & CStr(i)
    Next

    Put #ff, , mt

    Close ff
    End Sub


    Note that the MyType type has an array of "MyAttrib"s . There can be any
    number of "MyAttrib"s .

    Question: how does VB6 store this array?

    Looking at a hex editor gives this:

    01 00 03 00 00 00 00 00 00 00 0C 00 49 64 65 6E 74 69 66 69 65 72 20 30 0C
    00 49 64 65 6E 74 69 66 69 65 72 20 31 00 00

    The strings "Identifier 0" starts with the 49 64 65 sequence. It is
    preceded by two bytes that indicate the length of the string.

    Presumably the "01 00 03 00 00 00 00 00 00 00" sequence tells VB6 how long
    the array is.

    How can I use this information from .NET to work out the length of the
    array?

    Perhaps:

    Bytes 2 and 3 (03 00) less bytes 0 and 1 (01 00) = 2 = number of members of
    array.

    Any help would be appreciated. Sorry for the length of this post.


  • AMDRIT

    #2
    Re: Reading a binary file created with VB6

    Perhaps you can make use of this article:
    http://www.codeproject.com/csharp/fa...yfileinput.asp and modify it to
    your needs.



    "Mr X" <someone@somewh ere.com> wrote in message
    news:Om1HgfuiGH A.4660@TK2MSFTN GP03.phx.gbl...[color=blue]
    > One of my employer's products, written in VB6, reads and writes binary
    > files. We are shortly going to port to VB.NET (framework version 2.0) and
    > will need to be able to read and write these binary files from .NET. The
    > file format cannot be changed, so the .NET code will need to mimic the
    > read/write VB6 stuff.
    >
    > I am am using a BinaryReader to read the data in .NET. In VB6, the data
    > is read with "Get" and retrieved with "Put", in a file opened/created in
    > binary mode.
    >
    > I am having trouble working out how VB6 stores certain types of data.
    > Below is some VB6 code that mimics the write behaviour:
    >
    > Option Explicit
    >
    > Private Type MyAttrib
    > sId As String
    > End Type
    >
    > Private Type MyType
    > Attribs() As MyAttrib
    > End Type
    >
    > Private Sub Form_Load()
    > Dim ff As Integer
    > ff = FreeFile()
    >
    > On Error Resume Next
    > Kill "C:\test.tx t"
    > On Error GoTo 0
    >
    > Open "C:\test.tx t" For Binary As ff
    >
    > Dim count As Integer
    > count = 2
    >
    > Dim mt As MyType
    > ReDim mt.Attribs(coun t)
    >
    > Dim i As Integer
    > For i = 0 To count - 1
    > mt.Attribs(i).s Id = "Identifier " & CStr(i)
    > Next
    >
    > Put #ff, , mt
    >
    > Close ff
    > End Sub
    >
    >
    > Note that the MyType type has an array of "MyAttrib"s . There can be any
    > number of "MyAttrib"s .
    >
    > Question: how does VB6 store this array?
    >
    > Looking at a hex editor gives this:
    >
    > 01 00 03 00 00 00 00 00 00 00 0C 00 49 64 65 6E 74 69 66 69 65 72 20 30 0C
    > 00 49 64 65 6E 74 69 66 69 65 72 20 31 00 00
    >
    > The strings "Identifier 0" starts with the 49 64 65 sequence. It is
    > preceded by two bytes that indicate the length of the string.
    >
    > Presumably the "01 00 03 00 00 00 00 00 00 00" sequence tells VB6 how long
    > the array is.
    >
    > How can I use this information from .NET to work out the length of the
    > array?
    >
    > Perhaps:
    >
    > Bytes 2 and 3 (03 00) less bytes 0 and 1 (01 00) = 2 = number of members
    > of array.
    >
    > Any help would be appreciated. Sorry for the length of this post.
    >
    >[/color]


    Comment

    Working...