function FileToVariant(FileName: String): OleVariant;
var
AStream: TFileStream;
MyBuffer: Pointer;
begin
AStream:=TFileStream.create(FileName,fmOpenRead);
try
aStream.Seek(0, soFromBeginning);
Result:=VarArraycreate([0, AStream.size-1], VarByte);
MyBuffer:=VarArrayLock(Result);
AStream.ReadBuffer(MyBuffer^, AStream.Size);
VarArrayUnlock(Result);
finally
AStream.Free;
end;
end;
procedure VariantToFile(FileName: String; var AVariant: OleVariant);
var
AStream: TFileStream;
MyBuffer: Pointer;
Size: Integer;
begin
AStream:=TFileStream.create(FileName,fmCreate);
try
aStream.Seek(0, soFromBeginning);
Size:=VarArrayHighBound(AVariant,1)+VarArrayLowBound(AVariant,1)+1;
MyBuffer:=VarArrayLock(AVariant);
AStream.WriteBuffer(MyBuffer^, Size);
VarArrayUnlock(AVariant);
finally
AStream.Free;
end;
end;
文件和Variant之间的转换
最新推荐文章于 2023-08-18 16:53:27 发布