Examples Delphi

Title: Using IStream
Question: IStream is a Win32 interface, accessable to alot of programing languages, but is not well documented for Delphi. How do I use IStream?
Answer:
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.