将 Delphi 指针和 stream.Read 转换为 C#

Converting Delphi pointer and stream.Read to c#

我正在尝试将旧的 Delphi 应用程序转换为 C#。它通过 packed records 编写的 binary files 做了一些事情,我已经放在下面了。但是,只有 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 将文件的其余部分读入该数组,然后只检查它的大小。不确定这是不是最好的方法?

这是一块任意大小的内存。除了字节数组之外,我没有看到你有很多选择。是的,您可以分配非托管内存(例如使用 Marshal.AllocHGlobal),但这并不方便。

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