How to implement my own IStream derived object

Posted on 2004-04-19
Last Modified: 2012-05-04
I want to implement a class derived from the IStream interface?
I want this to be used as the stream of an IStorage object .


Question by:romanm
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3

Expert Comment

ID: 10864888
do you mean istream? (not capital IStream)

if so, then check out this site.
it's a good (and the only one i can find online) guide to how the iostream works, and how to derive a class from it

Author Comment

ID: 10865125
No, IStream an interface used by structured storage.

Accepted Solution

_ys_ earned 125 total points
ID: 10871576
Of course it's possible. Albeit quite a bit of work.

You need to:
- [obviously] create your component and implement IStorage.
- roll out your own IMoniker implementation (typically on your class factory)
- implement IMoniker::BindToStorage to specifically return your custom IStorage implementation

Of course, anyone using your component would then have know how to use monikers properly in order to create your component.

Implementations of IMoniker and IStorate et al. are non-trivial. What's wrong with the default implementation? You just playing around?

I presented a write up on monikers before, although you need to know the basics well to read it:

Author Comment

ID: 10871926
What I am trying to do is to make a source/write filter for msxml,
I want to create an encrypted form of a file that I could use as a source file for SAX parsing.

Author Comment

ID: 10872853
// I have found a simple solution to my problem:

class CMyXMLSourceFilter : public CUnknown, public ISequentialStream
      long __stdcall Read(void* pv, unsigned long cb, unsigned long* pcbRead);
      long __stdcall Write(const void* pv, unsigned long cb, unsigned long* pcbRead);
      static HRESULT CreateInstance(IUnknown * pUnknownOuter, CUnknown ** ppNewComponent);
      DECLARE_IUNKNOWN      // IUnknown

      void open(bool b);
      CMyXMLSourceFilter(IUnknown* pUnknpwnOuter);
      virtual ~CMyXMLSourceFilter() {};
      FILE*      f;
      // INondelegatingUnknown
      virtual HRESULT __stdcall  NondelegatingQueryInterface( const IID& iid, void ** ppv);
      virtual void FinalRelease();

CMyXMLSourceFilter::CMyXMLSourceFilter(IUnknown* pUnknpwnOuter) : CUnknown(pUnknpwnOuter)

void CMyXMLSourceFilter::open(bool b)
      if( b )
            f = fopen("C:\\test.stg", "wb" );
            f = fopen("C:\\test.stg", "rb" );

HRESULT CMyXMLSourceFilter::CreateInstance(IUnknown * pUnknownOuter, CUnknown ** ppNewComponent)
      *ppNewComponent = new CMyXMLSourceFilter(pUnknownOuter);

      return S_OK;

// INondelegatingUnknown
HRESULT CMyXMLSourceFilter::NondelegatingQueryInterface( const IID& iid, void ** ppv)
      if(iid == __uuidof(ISequentialStream))
            return FinishQI(static_cast<ISequentialStream*>(this),ppv);
            return CUnknown::NondelegatingQueryInterface(iid,ppv);

void CMyXMLSourceFilter::FinalRelease()

long CMyXMLSourceFilter::Read(void* pv, unsigned long cb, unsigned long* pcbRead)
      size_t l = fread(pv, 1, cb, f);
      if( pcbRead != NULL )
            *pcbRead = l;
      return 0;
long CMyXMLSourceFilter::Write(const void* pv, unsigned long cb, unsigned long* pcbRead)
      size_t l = fwrite(pv, 1, cb, f);
      if( pcbRead != NULL )
            *pcbRead = l;
      return 0;

// And the usage:

      ISAXXMLReaderPtr pSax;
      CMyXMLSourceFilter* cc1;
      CMyXMLSourceFilter::CreateInstance(NULL, (CUnknown**)&cc1);
      _variant_t var1(pcc);
      pcc = NULL;

I'll award the points for effort ;)

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dynamically allocate memory 9 76
How to split this in C++ 4 130
Unable to start eclipse ? 17 175
operator overload: incompatible type and too many params 5 99
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question