什么数据类型适合处理二进制数据在ActiveX方法? [英] What data type is suitable to handle binary data in ActiveX method?
问题描述
我正在使用Delphi 7编写这个ActiveX,如果这很重要。一个选择是使用十六进制字符串。什么可以是另一个?
VB6存储在字节变量和数组中的二进制数据。
Dim arrData()As Byte
VB6应用程序应将该变量传递给您的Delphi COM为 OleVariant
。
Delphi COM可以将 VarArray
转换为 TStream
,反之亦然:
procedure VariantToStream(const v:OleVariant; Stream:TStream);
var
p:pointer;
lo,size:整数;
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);
结束
结束
结束
procedure StreamToVariant(Stream:TStream; var v:OleVariant);
var
p:pointer;
size:整数;
begin
size:= Stream.Size - Stream.Position;
v:= VarArrayCreate([0,size-1],varByte);
如果size> 0然后
begin
p:= VarArrayLock(v);
try
Stream.ReadBuffer(p ^,size);
finally
VarArrayUnlock(v);
结束
结束
结束
在 CoClass
中使用单位: p>
// HRESULT _stdcall BinaryTest([in] VARIANT BinIn,[out,retval] VARIANT * BinOut);
函数TMyComClass.BinaryTest(BinIn:OleVariant):OleVariant;安全套
var
流:TMemoryStream;
begin
Stream:= TMemoryStream.Create;
try
VariantToStream(BinIn,Stream);
Stream.Position:= 0;
//使用Stream ...执行某些操作
// ...并将一些二进制数据返回给调用者(* BinOut)
Stream.Position:= 0;
StreamToVariant(Stream,Result);
finally
Stream.Free;
结束
结束
这是使用 SAFEARRAY
通过COM自动化使用二进制数据的字节。
将数据填充到 BSTR
(十六进制字符串,Base64 Encoding等..)听起来对我来说很丑,似乎更像是一个黑客。
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?
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.
这篇关于什么数据类型适合处理二进制数据在ActiveX方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!