What data type is suitable for processing binary data in the ActiveX method?

I am writing an ActiveX element for my friend who is supposed to encapsulate encryption procedures. It will be used mainly from VB6. What type of data should I choose for binary data such as encryption key, initialization vector, input and output data so that my friend can use it from VB6?

I use Delphi 7 to write this ActiveX, if that matters. One option is to use hexadecimal strings. What could be different?

+5
source share
1 answer

VB6 Binary data stored in byte variables and arrays.

Dim arrData() As Byte

VB6 Delphi COM OleVariant. Delphi COM VarArray TStream :

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;

CoClass:

// 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;

SAFEARRAY COM.
BSTR (Hex-, Base64 Encoding ..) .

+4

All Articles