转换Delphi指针和流.读到c#

本文关键字:读到 Delphi 指针 转换 | 更新日期: 2023-09-27 18:02:11

我正试图将旧的Delphi应用程序转换为C#。它用binary files写了一些东西通过packed records,我把它写在下面。但是,只有BInfoRec record保证在文件中并首先出现。其他的可能在那里,也可能不在那里,它们的顺序是未知的。有一种方法我遇到了麻烦。它通过FileStream.Read读取字节数,并将其读入packed records之一。然后,在第一个if语句中(在delphi版本中),它在堆上分配内存,并做与之前相同的事情,但将其读入指针。我正在努力找出解决这个问题的最佳方法,但我绝不是Delphi的专家。

Delphi代码:

StartP = packed record
    x:SmallInt;
    y:SmallInt;
end;
InfoP = packed record
 Ycoord:double;       
 Xcoord:double;              
 //other vars here
end;
HeadP = packed record
    NumP:DWORD;
    SizeStruct:DWORD;
    SizePoStruct:DWORD;
    //other vars here
end;
BInfoRec = packed record
    StructNum : WORD ;
    in_size : WORD ;    
    //other variables here
end;
var
  tStream:TFileStream;
  bInfo:BInfoRec;
  RestOfBFile:Pointer;
  sizeofRest:Integer;
Function LoadBFile(FileName:String):Boolean;
var
   sizeread:Integer;
begin
  Try
     LoadBFile:=False;
     tStream:=TFileStream.Create(Filename,fmOpenRead );
     sizeofRest:=tStream.Size-Sizeof(bInfo);
     sizeread:=tStream.Read(bInfo,Sizeof(bInfo));
     if sizeread = Sizeof(bInfo) then
     begin                                         //best way to convert this?
          RestOfBFile:=AllocMem(sizeofRest);
          sizeread:=tStream.Read(RestOfBFile^,sizeofRest);
          if SizeofRest= SizeRead then
             LoadBFile:=True;
     end;
     tStream.Free;
   except
        LoadBFile:=False;
        tStream.Free;
   end;
end;

c#(目前为止我所拥有的):

[Serializable()]
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public struct StartP
{
    public short x;
    public short y;
}
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public struct InfoP
{
    public double Ycoord;
    public double Xcoord;
    //other vars here
}
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public struct HeadP
{
    public UInt32 NumP;
    public UInt32 SizeStruct;
    public UInt32 SizePoStruct;
    //other vars here
}
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public struct BInfoRec
{
    public ushort StructNum;
    public ushort in_size;          
}
BInfoRec bInfo;
int sizeOfRest;
private Boolean LoadBFile(string fileName)
{
    int sizeRead;
    byte[] buffer = new byte[Marshal.SizeOf(bInfo)];
    try
    {
        using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
        {
            sizeOfRest = (int)stream.Length - Marshal.SizeOf(typeof(BInfoRec));
            sizeRead = stream.Read(buffer, 0, Marshal.SizeOf(typeof(BInfoRec)));
            if (sizeRead == Marshal.SizeOf(typeof(BInfoRec)))
            {
                //what goes here??
                if (sizeOfRest == sizeRead)
                {
                    return true;
                }
            }
        }
    }
    catch (Exception ex)
    {
        return false;
    }
}

我正在考虑创建一个未知大小的新字节数组,并使用BinaryReader将文件的其余部分读取到该数组,然后只是检查该数组的大小。不确定这是不是最好的方法?

转换Delphi指针和流.读到c#

这是一个任意大小的内存块。除了字节数组,我看不出你还有其他选择。是的,你可以分配非托管内存(例如使用Marshal.AllocHGlobal),但这很难方便。

所以,是的,如果我是你,我会分配一个字节数组,并读入其中的内容