简体   繁体   English

在C#中读取VB 6二进制文件

[英]Read VB 6 binary file in C#

I have a binary file written by VB6 application and now would like to use C# application to read the VB6 exported binary file. 我有一个由VB6应用程序编写的二进制文件,现在想使用C#应用程序读取VB6导出的二进制文件。 I have used the Microsoft.VisualBasic.dll into my C# project. 我已经在Microsoft C#项目中使用了Microsoft.VisualBasic.dll。

However there are some data inconsistency in C# application but I have check it in VB.net and it works nicely as well. 但是,C#应用程序中存在一些数据不一致的地方,但是我已经在VB.net中对其进行了检查,并且效果很好。 (I convert from VB6 to VB.net, then VB.net to C#) (我从VB6转换为VB.net,然后从VB.net转换为C#)

在此处输入图片说明

The screenshot represents the result from reading binary file by using C# and VB.Net application. 屏幕截图表示使用C#和VB.Net应用程序读取二进制文件的结果。 VB.Net is my expected result and now my C# application was showing inconsistency result VB.Net是我的预期结果,现在我的C#应用​​程序显示了不一致的结果

Both are double values in C# and VB.NET, and based on my observation, the int, string values looks fine. 两者都是C#和VB.NET中的双精度值,根据我的观察,int字符串值看起来不错。

In C# I was using statement shown as below, BinaryDetails is struct and inside have few double variables 在C#中,我使用的语句如下所示,BinaryDetails是struct,内部几乎没有双精度变量

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: 我将C#中的数据类型从double更改为float,仍然不是我的预期结果: 在此处输入图片说明

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. 那些双打的共同点是CC-54-59-40。 It is just that you read the data misaligned . 只是您读取的数据未对齐 You started reading too late, off by 2 bytes. 您开始阅读太晚了,减少了2个字节。

That's because your BinaryDetails is not an exact match with the data in the file. 那是因为您的BinaryDetails与文件中的数据不完全匹配。 Do keep in mind that you have to assume the file contains VB6 data types. 请记住,您必须假定文件包含VB6数据类型。 They are slightly different from C# types: 它们与C#类型略有不同:

  • VB6 file data is tightly packed, you need [StructLayout(LayoutKind.Sequential, Pack = 1)] VB6文件数据紧密打包,您需要[StructLayout(LayoutKind.Sequential, Pack = 1)]
  • A VB6 Integer is a C# short VB6整数是C#的short
  • A VB6 Long is a C# int VB6 Long是C# int
  • A VB6 Boolean is a C# short with -1 = True, 0 = False; VB6布尔值是C# short ,其-1 = True,0 = False;
  • VB6 strings have a fixed width, you need to read them as byte[] VB6字符串的宽度固定,您需要将其读取为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. 当然,请记住,使用C#程序中的VB.NET程序集非常简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM