VB6 Binary data stored in Byte variables and arrays.
Dim arrData() As Byte
VB6 Application should pass that variable to your Delphi COM as OleVariant
.
Delphi COM can convert VarArray
to TStream
and vice versa:
procedure VariantToStream(const v :OleVariant; Stream: TStream);
var
p : pointer;
lo, hi, size: Integer;
begin
lo := VarArrayLowBound(v, 1);
hi := VarArrayHighBound (v, 1);
if (lo >= 0) and (hi >= 0) then
begin
size := hi - lo + 1;
p := VarArrayLock (v);
try
Stream.WriteBuffer (p^, size);
finally
VarArrayUnlock (v);
end;
end;
end;
procedure StreamToVariant(Stream: TStream; var v: OleVariant);
var
p : pointer;
size: Integer;
begin
size := Stream.Size - Stream.Position;
v := VarArrayCreate ([0, size - 1], varByte);
if size > 0 then
begin
p := VarArrayLock (v);
try
Stream.ReadBuffer (p^, size);
finally
VarArrayUnlock (v);
end;
end;
end;
Usage in the CoClass
unit:
// HRESULT _stdcall BinaryTest([in] VARIANT BinIn, [out, retval] VARIANT * BinOut );
function TMyComClass.BinaryTest(BinIn: OleVariant): OleVariant; safecall;
var
Stream: TMemoryStream;
begin
Stream := TMemoryStream.Create;
try
VariantToStream(BinIn, Stream);
Stream.Position := 0;
// do something with Stream ...
// ... and return some Binary data to caller (* BinOut)
Stream.Position := 0;
StreamToVariant(Stream, Result);
finally
Stream.Free;
end;
end;
This is the most common approach to use SAFEARRAY
of Bytes with Binary data via COM automation.
Stuffing the data into a BSTR
(Hex strings, Base64 Encoding etc..) sound kinda ugly to me and seems more like a hack.