How do I use istream?

Source: Internet
Author: User

istream is a Win32 interface, accessable to alot of programing languages, but is not well formed ented 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 cocould be a little tricky. there may be a potentiality that the memory cocould 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.