Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What data type is suitable to handle binary data in ActiveX method?

I'm writing an ActiveX control for my friend, that should encapsulate encryption routines. It will be used from VB6 primarily. What data type should I choose for binary data like encryption key, initialization vector, input and output data so that it would be convenient for my friend to use it from VB6?

I'm using Delphi 7 to write this ActiveX, if that matters. One choice is to use hexadecimal strings. What can be the other?

like image 291
Vladislav Rastrusny Avatar asked Feb 13 '12 18:02

Vladislav Rastrusny


People also ask

Which data type is used to store a list of binary data?

Binary data can be stored in a table using the data type bytea or by using the Large Object feature which stores the binary data in a separate table in a special format and refers to that table by storing a value of type oid in your table.

Why do we need binary data type?

Since the binary data types are unstructured types, they can store many different types of information, for example, IP addresses, MAC addresses, or device identification numbers from RFID tags. The binary data types can also store encrypted data in binary format, which saves disk space.


1 Answers

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.

like image 161
kobik Avatar answered Sep 28 '22 19:09

kobik