1

I have a binary file written by VB6 application and now would like to use C# application to read the VB6 exported binary file. I have used the Microsoft.VisualBasic.dll into my C# project.

However there are some data inconsistency in C# application but I have check it in VB.net and it works nicely as well. (I convert from VB6 to VB.net, then VB.net to C#)

enter image description here

The screenshot represents the result from reading binary file by using C# and VB.Net application. VB.Net is my expected result and now my C# application was showing inconsistency result

Both are double values in C# and VB.NET, and based on my observation, the int, string values looks fine.

In C# I was using statement shown as below, BinaryDetails is struct and inside have few double variables

ValueType DetailsValueType = (ValueType)BinaryDetails;
FileSystem.FileOpen(FileNumber, FileName, OpenMode.Binary, OpenAccess.Read);
FileSystem.FileGet(FileNumber, ref DetailsValueType);

I have change the data type in C# from double to float, still not my expected result: enter image description here

user1219310
  • 732
  • 1
  • 9
  • 23
  • I am lost :( IMHO, the question needs more clarity.. but not even sure what to suggest as improvement.. For starters, what are these screenshots to show? – Vikas Gupta Dec 09 '14 at 02:05
  • the screenshot represents the result from reading binary file by using C# and VB.Net application. VB.Net is my expected result and now my C# application was showing inconsistency result – user1219310 Dec 09 '14 at 02:13
  • Why on earth are you casting to ValueType? – Nathan Tuggy Dec 09 '14 at 03:44
  • Is there anyway you can add to your question one record from the file? – Black Frog Dec 09 '14 at 04:22
  • You are saying you were successful on getting it to read in VB.NET, right? So just use an online VB.NET to C# converter to get it into C# code. – Icemanind Dec 09 '14 at 06:07
  • 2
    It would be very helpful if you could post what the struct you are writing to disk looks like. I guess that the issue is with VB and C# compiling the same struct to have different memory alignment. More information on how struct layouting can be influenced in C# can be found here: http://www.developerfusion.com/article/84519/mastering-structs-in-c/ – Oliver Ulm Dec 09 '14 at 07:46
  • See also http://stackoverflow.com/questions/7290976/vb6-how-are-binary-files-encoded-using-put-statement and http://stackoverflow.com/questions/8043167/how-to-read-and-write-interoperable-strings-to-a-binary-file-between-net-and-vb – MarkJ Dec 09 '14 at 13:26

1 Answers1

4

You can reverse-engineer this kind of mishap with a little test program:

class Program {
    static void Main(string[] args) {
        var value1 = 3.49563395756763E-310;
        var bytes1 = BitConverter.GetBytes(value1);
        Console.WriteLine(BitConverter.ToString(bytes1));
        var value2 = 101.325;
        var bytes2 = BitConverter.GetBytes(value2);
        Console.WriteLine(BitConverter.ToString(bytes2));
    }
}

Output:

CC-CC-CC-54-59-40-00-00
CD-CC-CC-CC-CC-54-59-40

Note how you are definitely on the right track, you are reading correct byte values from the file. Those doubles have CC-54-59-40 in common. It is just that you read the data misaligned. You started reading too late, off by 2 bytes.

That's because your BinaryDetails is not an exact match with the data in the file. Do keep in mind that you have to assume the file contains VB6 data types. They are slightly different from C# types:

  • VB6 file data is tightly packed, you need [StructLayout(LayoutKind.Sequential, Pack = 1)]
  • A VB6 Integer is a C# short
  • A VB6 Long is a C# int
  • A VB6 Boolean is a C# short with -1 = True, 0 = False;
  • VB6 strings have a fixed width, you need to read them as byte[]

Ought to be enough to solve the problem. And of course keep in mind that it is very simple to use a VB.NET assembly from a C# program.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536