Search code examples
delphicominterfacedelphi-7istream

How to convert IStream to TStreamAdapter?


How to convert IStream to TStreamAdapter on Delphi 7?

On Delphi XE2 I may write:

var
  aStream: IStream;
  aStreamAdapter: TStreamAdapter;
begin
  ...
  aStreamAdapter := aStream as TStreamAdapter;
  ...
end;

But Delphi 7 writes:

Error: Operator not applicable to this operand type

Solution

  • Sample code:

    var
      aFileStream: TFileStream;
      iStr: TStreamAdapter;
      iRes , iRes1, iRes2: Largeint;
      aStreamStat: TStatStg;
      aStreamContent: IStream;
    begin
      aFileStream := TFileStream.Create('<...>', fmCreate);
      try
        aStreamContent := <...> as IStream;
        aStreamContent.Seek(0, 0, iRes);
        iStr := TStreamAdapter.Create(aFileStream, soReference);
        aStreamContent.Stat(aStreamStat, 1);
        aStreamContent.CopyTo(iStr, aStreamStat.cbSize , iRes1, iRes2);
      finally
        aFileStream.Free;
      end;
    end;