简体   繁体   English

delphi 7.0、delphi 2010 和二进制文件

[英]delphi 7.0, delphi 2010 and binary files

My software developed under Delphi 7.0 has been upgraded and developed under Delphi 2010 RAD Studio XE.我在Delphi 7.0下开发的软件已经在Delphi 2010 RAD Studio XE下升级开发。 It also saves or writes user's settings into binary files.它还将用户的设置保存或写入二进制文件。 The problem I am running into is that my Delphi 2010 application or software is expected to read binary files created by Delphi 7.0 application, but Delphi 2010 application has problem reading the binary file.我遇到的问题是我的 Delphi 2010 应用程序或软件预计会读取由 Delphi 7.0 应用程序创建的二进制文件,但 Delphi 应用程序有读取二进制文件的问题。2010 应用程序Both of these software are copy of each other.这两个软件是彼此的副本。

I did set the Record Field Alignment option for the project at 1 on Delphi 7.0 and byte on Delphi 2010.我确实将项目的记录字段 Alignment 选项设置为 Delphi 7.0 上的 1 和 Delphi 2010 上的字节。

I am always running into exception "read beyond end of line," when the software reads the binary file.当软件读取二进制文件时,我总是遇到异常“读取超出行尾”。

I read and write a binary file by doing the following.我通过执行以下操作来读写二进制文件。

Writing into a binary file:写入二进制文件:

procedure WriteUnitFile;
var
  unitFile: File;
  x:integer;
  FileHeader:TFileHeader;
begin
  if not DemoMode then
  begin
    FileHeader.id := 'UnitFile';
    FileHeader.version := 7;

    if fileexists(basedir+unitfilename) then
      BackupFile(basedir+unitfilename);

    AssignFile(unitFile,baseDir+unitfilename);

    if UnitList.Count > 0 then
    begin
      Rewrite(unitFile,1);
      BlockWrite(unitFile, FileHeader, SizeOf(FileHeader));
      for x := 0 to UnitList.Count - 1 do
      begin
        TUnit(UnitList[x]).Write(unitFile);
      end;
    end
    else
      DeleteFile(baseDir+unitfilename);

    CloseFile(unitFile);
  end;
end;

Reading from a binary file:从二进制文件中读取:

procedure ReadUnitFile;
var
  unitFile:File;
  newUnit,simUnit:TUnit;
  ut,TypeCard:TUnitType;
  New_Ver_File:TFileHeader;
  Address:SmallInt;
  State:TUnitState;
  SubAddress:SmallInt;
  MyfDefs:array[1..20] of SmallInt;
  fDefs:array[1..16] of SmallInt;
begin

  if FileExists(baseDir + unitfilename) then
  begin
    oneUnit := false;
    AssignFile(unitFile,baseDir+unitfilename);
    AssignFile(newUnitFile,baseDir+Dummyfilename);
    Reset(unitFile,1);

    BlockRead(unitFile,New_Ver_File,SizeOf(New_Ver_File));

    Reset(UnitFile,1);
    BlockRead(UnitFile,New_Ver_File,SizeOf(New_Ver_File));

    while not Eof(UnitFile) do
    begin
      BlockRead(UnitFile,ut,SizeOf(ut));
      case ut of
        tutSimulator:newUnit := TSimulator.Create;
        tutDX2202:newUnit := TDX2202.Create;
        tutDX8884CS:newUnit:=TDX8884CS.Create;
        tutDX8F44F: newUnit := TDX8F44F.Create;
        tutDX0008:newUnit := TDX0008.Create;
        tutDX0800:newUnit := TDX0800.Create;
        tutDX8000:newUnit := TDX8000.Create;
        tutDX1000:newUnit := TDX1000.Create;
        tutDX0100:newUnit := TDX0100.Create;
        tutDX4404:newUnit := TDX4404.Create;
        tutDX0020:newUnit := TDX0020.Create;
        tutDX0080:newUnit := TDX0080.Create;        
        tutDX8814:newUnit := TDX8814.Create;
        tutDX8814CS:newUnit := TDX8814CS.Create;
        tutDX8884:newUnit := TDX8884.Create;
        else
                newUnit := TUnit.Create;
      end;
      newUnit.Read(unitFile);
      if DemoMode then
      begin
        if oneUnit = true then
        begin
          simUnit := TSimulator.Create;
          simUnit.Assign(newUnit);
          newUnit.Free;
          newUnit := simUnit;
        end
        else
        begin
          oneUnit := true;
        end;
      end;
      unitList.Add(newUnit);
    end;
    CloseFile(unitfile);
    UnitsDlg.UnitGrid.RowCount := unitList.Count+1;
    if UnitsDlg.UnitGrid.RowCount > 1 then
      UnitsDlg.UnitGrid.FixedRows := 1;
    UnitsDlg.FillIn;
  end
  else
  begin
       UnitsDlg.UnitGrid.RowCount := 1;
  end;
end;

UPDATE: Here is the user-defined datatypes:更新:这是用户定义的数据类型:

  TFileHeader = record
    id:string[32];
    version:SmallInt;
  end;

TUnitType = (tutUnused,tutSimulator,tutDX2202,tutDX0008,tutDX0800,tutDX8000,
                 tutDX1000,tutDX4404,tutDX0020,tutDX8814,tutDX8814CS,
                 tutDX8884,tutDX8884CS,tutDX8f44f,tutDX0080,tutDX0100,tutLast);

TUnitState = (tusDisabled,tusEnabled);

TUnit = class(TObject)
  changeLink:TUnit;
  Address: SmallInt;
  SubAddress:SmallInt;
  State:TUnitState;
  uType:TUnitType;
  RegCnt:integer;
  fRegs:array[1..20] of SmallInt;
  fDefs:array[1..20] of SmallInt;
  MsgCnt:LongWord;
  RxCnt:LongWord;
  BreakCnt: LongWord;
  LineErrCnt: LongWord;
  OtherErrCnt:LongWord;
  TimeoutCnt: LongWord;
  BadMsgErrCnt:LongWord;
  XSumErrCnt:LongWord;
  OutsFlag:Boolean;
  CommBits:LongWord;
  OfflineCnt:integer;
  Online:Boolean;
  CurReg:integer;
  selectedonce,usedIP:Boolean;
  LastDigitalOut,LastDigitalIn,
  CurRegOut,umsglen,urmsglen,
  dummycount,Unitlocation,
  CommLocation:integer;
private

public
   followed by list of procedures and functions...
end;

Is there something I am missing or not understanding very well?有什么我遗漏或不太了解的东西吗?

One thing I did forget to mention is that Delphi 2010 application reads and writes into binary files just fine without making any changes to the code, but it has problem only when reading files created by Delphi 7.0.我确实忘记提及的一件事是 Delphi 2010 应用程序可以很好地读取和写入二进制文件,而无需对代码进行任何更改,但只有在读取由 Delphi 7.0 创建的文件时才会出现问题。 application.应用。

Obviously, this question is related to Record Field Alignment.显然,这个问题与记录字段 Alignment 有关。 So, what should I set it on my Delphi 2010 (Byte, Word, Double Word, Quad Word), if Delphi 7.0 is set at 1.那么,如果 Delphi 7.0 设置为 1,我应该在我的 Delphi 2010(字节、字、双字、四字)上设置什么。

Thanks in advance.提前致谢。

The key is in the TFileHeader declaration (and maybe the TUnit and TUnitType declaration).关键在于TFileHeader声明(也可能是TUnitTUnitType声明)。 You should add these definitions to your question.您应该将这些定义添加到您的问题中。

If you define a pure string in either of these records, you need to use ShortString instead of String in Delphi 2010 to make it work.如果您在其中任一记录中定义纯字符串,则需要使用ShortString而不是 Delphi 2010 中的String才能使其工作。

String in Delphi 7 = 1 byte per char. Delphi 7 中的字符串 = 每个字符 1 个字节。
String in Delphi 2010 = 2 bytes per char. Delphi 2010 中的字符串 = 每个字符 2 个字节。

Update:更新:

Your last update doesn't really reveal any new information, but the fDefs variable in the ReadUnitFile procedure is defined as an array[1..16] of SmallInt;您的最后一次更新并没有真正揭示任何新信息,但ReadUnitFile过程中的fDefs变量被定义为array[1..16] of SmallInt; , but in your TUnit class, fDefs is an array[1..20] of SmallInt; ,但在您的TUnit class 中, fDefsarray[1..20] of SmallInt; ? ?

The fDefs variable doesn't seem to be used, but this might be the difference that causes the EOL error?似乎没有使用fDefs变量,但这可能是导致 EOL 错误的差异?

You should define arrays like this as a common Type , anyway, to ensure that they can be passed around and assigned to each other as compatible types (eg. as parameters in methods).无论如何,您应该像这样将 arrays 定义为通用Type ,以确保它们可以作为兼容类型传递并相互分配(例如,作为方法中的参数)。

Yet an update:还有一个更新:

The error is not in the strings, but in the Size of TObject .错误不在字符串中,而是在TObject的大小中。

In Delphi 2009, TObject increased the size from 4 bytes to 8 bytes.在 Delphi 2009 中, TObject将大小从 4 字节增加到 8 字节。
The first 4 bytes is a pointer to the objects VMT (as it has been for a long time), and in the last 4 bytes (IIRC), it is possible to contain a reference to a synchronization monitor.前 4 个字节是指向对象VMT的指针(长期以来一直如此),在最后 4 个字节 (IIRC) 中,可以包含对同步监视器的引用。

Have a look at this nice article: http://blogs.teamb.com/craigstuntz/2009/03/25/38138/看看这篇不错的文章: http://blogs.teamb.com/craigstuntz/2009/03/25/38138/

Mark all your records you write to file in blocks with packed keyword both in Delphi 7 and 2010.在 Delphi 7 和 2010 中,使用packed关键字将您写入文件的所有记录标记为块。

I don't know the answer, but I can give you some directions on how to properly identify the problem yourself.我不知道答案,但我可以为您提供一些有关如何自己正确识别问题的指导。 Once the problem is identified, the solution would usually immediately follow.一旦确定了问题,解决方案通常会立即出现。

How to debug the issue如何调试问题

Since you're getting "read beyond end of line file" errors, you're obviously dealing with changing record size.由于您收到"read beyond end of file"错误,因此您显然正在处理更改记录大小。 A number of things can cause that:许多事情可能导致:

  • Alignment changes. Alignment 发生变化。 Unless the record is defined as packed record the compiler assumes it's only used internally so it's free to change it's alignment.除非记录被定义为packed record ,否则编译器假定它仅在内部使用,因此可以自由更改它的 alignment。 Since CPU's (and OS'es) changed allot in the D7 - D2010 time frame, is reasonable to expect changes in alignment between the two compilers.由于 CPU(和操作系统)在 D7 - D2010 时间范围内更改了分配,因此可以合理地预期两个编译器之间 alignment 的更改。
  • Underlying data type size changes.基础数据类型大小更改。 Delphi 2009 made the change to Unicode, so D2009+ (including D2010) has two-bytes Chars. Delphi 2009 更改为 Unicode,所以 D2009+(包括 D2010)有两个字节的字符。 If your record contains Char data, that can cause the change in record size.如果您的记录包含Char数据,则可能会导致记录大小发生变化。

Back to debugging.回到调试。 You can write a bit of trivial code that displays the size of the record itself, the size of each of it's fields and the offset for each of the fields.您可以编写一些简单的代码来显示记录本身的大小、每个字段的大小以及每个字段的偏移量。 Run this code from both D7 and D2010, make a note of all the numbers and address the possible differences:从 D7 和 D2010 运行此代码,记下所有数字并解决可能的差异:

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type

  TTestRecord = record
    IntField: Integer;
    ShortStrField1: string[6];
    ShortStrField2: string[5];
    D: Double;
  end;

var R: TTestRecord;

begin
  WriteLn('TTestRecord size: ', SizeOf(R));
  WriteLn('IntField size: ', SizeOf(R.IntField), ', offset: ', Integer(@R.IntField) - Integer(@R));
  WriteLn('ShortStrField1 size: ', SizeOf(R.ShortStrField1), ', offset: ', Integer(@R.ShortStrField1) - Integer(@R));
  WriteLn('ShortStrField2 size: ', SizeOf(R.ShortStrField2), ', offset: ', Integer(@R.ShortStrField2) - Integer(@R));
  WriteLn('D size: ', SizeOf(R.D), ', offset: ', Integer(@R.D) - Integer(@R));
  ReadLn;
end.

Once you get this information you can change the record so it looks the same on both Delphi 7 and Delphi 2010. I'd start on the Delphi 7 platform;获得此信息后,您可以更改记录,使其在 Delphi 7 和 Delphi 2010 上看起来相同。我将从 Delphi 7 平台开始; I'd first change the definition to packed record and then add extra padding bytes to the record to maintain field offsets.我首先将定义更改为packed record ,然后在记录中添加额外的填充字节以维护字段偏移量。 The new type definition would look like this:新的类型定义如下所示:

TTestRecord = packed record
  IntField: Integer;
  ShortStrField1: string[6];
  ShortStrField2: string[5];
  _DummyPadding: array[0..6] of Byte; // Added to fix D's alignment after adding "packed"
  D: Double;
end;

Once this is done move to Delphi 2010, keeping the packed modifier and all the manually added padding.完成后移至 Delphi 2010,保留packed的修饰符和所有手动添加的填充。 Run the code that displays field size and alignment, pay special attention to the size of the individual fields: if you've got any Char fields in your record, you'll need to change them to AnsiChar .运行显示字段大小和 alignment 的代码,特别注意各个字段的大小:如果您的记录中有任何Char字段,则需要将它们更改为AnsiChar Fortunately both Delphi 7 and Delphi 2010 know about AnsiChar and consider it to be 1 byte long.幸运的是 Delphi 7 和 Delphi 2010 都知道AnsiChar并认为它是 1 字节长。

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

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