Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading (with Filesystem.FileGet) VB6 record file (written with Put) with C#

Tags:

c#

.net

vb6

I need to read data from a legacy database file produced by Visual Basic 6. From the legacy software I found that file was written using Put and passing sort of records as parameters to the Put function. These structures are defined as follows:

Type THE_TYPE
    FIELD_1 As Single
    FIELD_2 As String * 20
    FIELD_3(1 To 50) As Single
    FIELD_4(1 To 10) As String * 1
End Type

My Types are larger and more complex but I've put in THE_TYPE the different definitions I have in my project. I've found that importing Microsoft.VisualBasic gives me access to VB functions similar to those used to write the file, so I'm opening and closing the file with FileSystem.OpenFile() and .CloseFile(); now I need to finally read data contained and since the original function was:

Public RecordContent As THE_TYPE
[...] 
Get #1, recordNumber, RecordContent 

I suppose I can use something similar, like Microsoft.VisualBasic.FileSystem.FileGet().
So the question is, how do I define a container, I suppose a class, similar to the original VB6 Type THE_TYPE? How do I call .FileGet() to correctly fill this object?

like image 677
ccalboni Avatar asked Mar 27 '12 12:03

ccalboni


1 Answers

The key is defining attributes properly on the structure declaration in VB.NET. Provided the structure will always be initialized by FileGet, you don't need to manually initialize its fixed fields in a constructor, otherwise, you do.

Structure THE_TYPE
    Public FIELD_1 As Single
    <VBFixedString(20), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=20)> Public FIELD_2 As String
    <VBFixedArray(49)> Public FIELD_3 As Single()
    <VBFixedArray(9)> Public FIELD_4 As Char()
End Structure

Obviously, the arrays will have to start from zero, so the upper bounds are shifted down.

Reading from the file:

Dim d As System.ValueType = New THE_TYPE()

FileOpen(1, "...", OpenMode.Random, OpenAccess.Read, OpenShare.Default, 234)
FileGet(1, d, 1)
FileClose(1)

234 is the size of the structure in VB6. It's bigger in VB.NET, so you want to hardcode that.

like image 146
GSerg Avatar answered Oct 08 '22 21:10

GSerg