Title: FileToVariant / VariantToFile
Question: How to convert a file in an OleVariant for transfering
it with DCOM or MIDAS over the net.
Answer:
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;