gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:41:21 27 4
gpt4 key购买 nike

我正在尝试将旧的 Delphi 应用程序转换为 C#。它对通过packed records 编写的二进制文件 做了一些事情,我已经放在下面了。但是,只有 BInfoRec record 保证在文件中并首先出现。其他人可能在也可能不在那里,而且他们的顺序是未知的。我在使用一种方法时遇到了问题。它获取通过 FileStream.Read 读取的字节数,并将这些读取到一个 packed records 中。然后,在第一个 if 语句中(在 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 将文件的其余部分读入该数组,然后检查其大小。不确定这是否是最佳方式?

最佳答案

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

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

关于c# - 将 Delphi 指针和 stream.Read 转换为 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38897989/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com