4

CONTEXT

I have a vb6 software that use this particular Type:

Type Example
    A As Integer
    B As Single
    C As String * 10
    D  As Byte
    E As String
End Type

This structure is entirely saved in a binary file with a simple "put":

Dim Numfile%
Numfile = FreeFile
[...]
Put #Numfile, , example_instance
[...]

PROBLEM

Now I want to read this binary file from vb.net (.NET Framework 4). The problem is that in .net we don't have fixed strings... I've tried to write something like:

Structure Example
     Dim A As Short
     Dim B As Single 
     Dim C() As Char ' ---> This should replace String * 10.
     Dim D As Byte 
     Dim E As String 
End Structure

And then I read the file with:

Dim n as short = FreeFile()
Dim instance as Example
If FileExists(filename) Then
    FileOpen(n, filename, OpenMode.Binary, OpenAccess.Read)
    [...]        
    FileGet(n, instance)
    [...]

But, obviously, the original data was not detected correctly.

How can I read this binary file correctly in vb.net?

Calaf
  • 1,133
  • 2
  • 9
  • 22

1 Answers1

5

I'll write this as an answer, even though I'm not 100% sure it will work because it's not something I have ever done. The VBFixedString attribute exists specifically to be used in situations where VB6 would use a fixed-length String. As such I think that declaring your type like this:

Structure Example
     Dim A As Short
     Dim B As Single
     <VBFixedString(10)> Dim C As String
     Dim D As Byte
     Dim E As String
End Structure

should do the trick.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46