c++visual-c++com

Is it possible to implement a COM interface without creating a custom interface?


I am learning C++ and I am trying to better understand it. I was reading the MSDN documents on how to use XmlLite. It said that I must use a class that implements the IStream interface. It said to declare and instantiate my class that extends IStream and use CComPtr when declaring the variable. Then it showed me the following:

CComPtr<IStream> pFileStream;
CComPtr<IXmlReader> pReader;

I am a tad bit confused. If CComPtr is used to pull the XML, why do I have to extend <IStream>? Why not just have CComPtr already implement IStream and just call CComPtr? Or does CComPtr already have IStream and the only way for IStream to be effective is to extend like above?


Solution

  • If CComPtr is used to pull the XML, why do I have to extend <IStream>? Why not just have CComPtr already implement IStream and just call CComPtr?

    IStream is an interface — saying "I want some class which implements this interface" does not tell how you want to actually get the data. CComPtr is only a pointer to a coclass which implements an interface — it does not actually implement any interface itself.

    Is it possible to implement a COM interface without creating a custom interface?

    I'm not 100% positive here, but I don't believe you need to implement an interface. You do however need to implement the interface itself in a coclass.