gpt4 book ai didi

delphi 将 MemoryStream 复制到动态数组

转载 作者:行者123 更新时间:2023-12-02 01:50:08 24 4
gpt4 key购买 nike

您好,我有这张打包的记录:

type
TMyRecord = packed record
BufSize: Word;
TargetUser:array[0..80] of char;
StreamHolder: Byte;
end;
PMyRecord = ^TMyRecord;

//我想将 MemoryStream 保存到 StreamHolder 中请参阅我的以下过程:

Procedure AddToRec(ATargetUser:String);
var
MyRecord: PMyRecord;
Strm:TMemoryStream;
Size: Integer;
begin
Strm:=TMemoryStream.Create;
try
Strm.LoadFromFile('myFile.dat');
Strm.position:=0;
Size:=Strm.size;
GetMem(MyRecord,Size);
ZeroMemory(MyRecord,Size);
MyRecord.BufSize := Size;
StrCopy(MyRecord.TargetUser,PChar(ATargetUser));

// here how could i copy the Strm into the StreamHolder ?

//SendMyBuffer(MyRecord,Size);

finally
Strm.free;
end;
end;

请问我如何将 Strm 复制到 StreamHolder ?

非常感谢

最佳答案

您似乎想要将整个流复制到@MyRecord.StreamHolder上。这样做:

Strm.ReadBuffer(MyRecord.StreamHolder, Size);

您还需要更改 GetMem 以分配足够的内存。

GetMem(MyRecord, Size + SizeOf(MyRecord^) - SizeOf(MyRecord.StreamHolder));

或者也许更优雅:

GetMem(MyRecord, Size + Integer(@PMyRecord(nil)^.StreamHolder));

就目前情况而言,您的代码不会考虑出现在 StreamHolder 之前的记录部分。

关于delphi 将 MemoryStream 复制到动态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17412825/

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