mad
mad

Reputation: 1047

read from 32bit record in 64bit project

I have got an old project to refactor. This is Delphi5 project in 32bit.

The new project must use 64bit architecture.

Actually my point is to read old data format from a file.

The file contains records with lot of fields.

At the moment I have problem with dynamic array of integers.

TCObject = record
  Name:string[205];
  roomindex:array of integer;

SizeOf(roomindex) = 4 in old project and SizeOf(roomindex) = 8 in my 64bit project.

So I have a problem to read data from file as

stream.Read(buf, SizeOf(TCObject))

Could you show me way how to say Delphi to use size=4? :)

Thanks a lot.

UPDATE: as David said it is better to provide real code that reads data.

procedure ParseCDatabase(pBuff: Pointer; pSize: Integer);
var
  curr:^TCObject;
  i, n: Integer;
  curri:^Integer;
begin
  curri:= pBuff;
  n:=curri^;
  Inc(curri);
  curr:=pointer(curri);
  for i:=0 to n-1 do 
  begin
    if Curr^.Name<>'' then 
    begin
      COAddObject();
      curro:=pointer(curr);
      curro.RoomIndex:=integer(nil);
      Fcodb[af][ai]:=curr^;
      SetLength(Fcodb[af][ai].RoomIndex,0);
      Inc(ai);
    end;
    inc(curr);
  end;
end;

I provide code as is... So we see here that roomindex is read but reset to 0. So actualy we do not need it at all.

Problem is to read whole data via pointers because size of point to dynamic array is 8 now but not 4.

Upvotes: 0

Views: 249

Answers (1)

Dsm
Dsm

Reputation: 6013

roomindex is a pointer, which in a 32 bit project is 4 bytes and in a 64 bit project is 8 bytes. You can't change that realistically. I am amazed that you save a pointer to a file, and load it back. How can that work, unless the pointer is discarded? Assuming you save the pointer but discard it you can do this:

TCObjectOldFormat = record
  Name:string[205];
  roomindex: Int32;
end;

TCObject = record
  Name:string[205];
  roomindex:array of integer; 
end;

var
  AObject : TCObject;
  AObjectOldFormat :TCObjectOldFormat;
...
stream.ReadBuffer(AObjectOldFormat, SizeOf(TCObjectOldFormat));
AObject.Name := AOldObject.Name;

obvious you can adapt to your needs using buf. This just shows more explicitly the idea.

Upvotes: 2

Related Questions