是用Variant
给你两个函数:(注:需 uses DSIntf)
function StmToVariant(Stm:TStream):OleVariant;
var
Size: Integer;
DataPtr: Pointer;
VarBound: TVarArrayBound;
Pkt: TDataPacket;
begin
Size := Stm.Size - Stm.Position;
if Size > 0 then begin
FreeDataPacket(Pkt);
FillChar(VarBound, SizeOf(VarBound), 0);
VarBound.ElementCount := Size;
Pkt := TDataPacket(SafeArrayCreate(varByte, 1, VarBound));
try
SafeArrayAccessData(Pkt, DataPtr);
try
Stm.Read(DataPtr^, Size);
finally
SafeArrayUnAccessData(Pkt);
end;
except
FreeDataPacket(Pkt);
raise;
end;
end;
DataPacketToVariant(Pkt, Result);
end;
procedure VariantToStm(const V:OleVariant;Stm:TStream);
var
Size: Integer;
DataPtr: Pointer;
Pkt: TDataPacket;
begin
FreeDataPacket(Pkt);
if not VarIsNull(V) then begin
SafeArrayCopy(VarToDataPacket(V), Pkt);
end;
if Assigned(Pkt) then begin
Size := DataPacketSize(Pkt);
SafeArrayAccessData(Pkt, DataPtr);
try
Stm.Write(DataPtr^, Size);
finally
SafeArrayUnAccessData(Pkt);
end;
FreeDataPacket(Pkt);
end;
end;