How do I use IStream?

IStream is a Win32 interface, accessable to alot of programing languages, but is not well documented for Delphi. How do I use IStream?


  There are alot of articles here and elsewhere showing how to convert just about everything to a variant in order to pass it through a COM Interface.  However, most of the VCL has methods and properties already set up to work with streams.  It's much eaiser to work with IStreams.  The trick is using TOLEStream and TStreamAdapter.
  IStream is defined in the ActiveX unit, TStreamAdapter is in Classes, and TOLEStream is in Axctrls.  You can use TStreamAdapter to convert a TStream decendant ( such as TMemoryStream ) to an IStream interface and TOLEStream converts a IStream to a TStream.  The following class wrapps this all together for you.  (WARNING : do not try to convert these functions to a property of type IStream.  There may be a way to do it, but if you do, you may be tempted to use that property to call the IStream methods, which won't really work - or at least, I get access violations)

Uses Classes, ActiveX, Axctrls;

Type TInterfaceStream = Class ( TMemoryStream )
  Public
    Procedure LoadFromIStream(Source : IStream);
    Function GetIStream : IStream;
end;

Procedure TInterfaceStream.LoadFromIStream(Source : IStream);
var
  Adapt : TOLEStream;
  Buff : Byte;
  I : Integer;
begin
ADapt := TOLEStream.Create(Source);
Adapt.Position := 0;
Self.Clear;
Self.Position := 0;
For I := 0 to Adapt.Size do
  begin
    Adapt.Read(Buff, 1);
    Self.Write(Buff, 1);
  end;
Self.Position := 0;
end;

Function TInterfaceStream.GetIStream : IStream;
var
  Adapt : TStreamAdapter;
  tPos : Int64;
begin
Adapt := TStreamAdapter.Create(Self);
Adapt.Seek(0, 0, tPos);
Result := Adapt as IStream;
end;

Now it's simple to use IStream.  For instance, if you have a method of a COM object that your building that needs to return a IStream, simply declare a TInterfaceStream as a private member of the object( we'll call it FStream here), Create it on initialize, and write your method like this

Function TSampleCOMObj.Mehtod1 : IStream
begin
// Here's where you load whatever actually goes into the stream
result := FStream.GetIStream;
end;

Making it a local variable to the method could be a little tricky.  There may be a potentiality that the memory could be deallocated prior to the application using this object gets around to reading the contents.  Making it a private member is safer.

On the application side, simply do the reverse:

Procedure Form1.Button1OnClick(Sender : TObject);
var
  Server : ISampleCOMObj;
  temp : IStream;
  ResultStream : TInterfaceStream;
begin
Server := CreateCOMObject(Class_TSampleCOMObj) as ISampleComObj;
temp := Server.method1;
ResultStream := TinterfaceStream.Create;
ResultStream.Clear;
resultStream.Position := 0;
resultstream.LoadFromIStream(Temp);
// do whatever it is you want with the data in the stream;
end;

This is also a great way to move TStrings around.  IStrings is Delphi specific, but a TStrings saved to a IStream isn't.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值