2016-08-11 97 views
2

我試圖將舊的Delphi應用程序轉換爲C#。它做了一些binary files通過packed records我寫下的東西。但是,只有BInfoRecrecord保證在文件中並首先出現。其他人可能或不可能在那裏,他們的順序是未知的。特別是我遇到了一種方法的麻煩。它通過FileStream.Read獲取正在讀入的字節數,並將其讀入packed records之一。然後,在第一個if語句中(在delphi版本中),它在堆上分配內存,並執行與之前相同的操作,但將其讀入指針。我試圖找出解決這個問題的最佳方法,但我絕不是Delphi的專家。轉換德爾福指針和stream.Read到C#

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在讀然後只是檢查文件的大小。不知道這是否是最好的方法?

回答

1

這是一塊任意大小的內存塊。我沒有看到除了字節數組以外還有很多選項。是的,你可以分配非託管內存(例如Marshal.AllocHGlobal),但這很難方便。

所以,是的,如果我是你,我會分配一個字節數組,並將內容讀入它。