1

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?

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

You're right. It's quite common in fact. For instance, all Shell Extensions implement the COM interfaces useb by Explorer.exe. Few add custom interfaces; Explorer isn't going to use them so they'd be for internal use only.
Ok, I am use to languages where if a class implements a interface. you can just call the class. You don't have to do class<interface>. I am under the impression that in c++, in order for the compiler to take notice of the interface you must call it like class<interface>. correct me if I am wrong.
@numerical25 It's not a problem of the C++ language. It's COM's. COM is not part of C++. It's a Microsoft convention at the binary level. You can implement COM objects in C as well. en.wikipedia.org/wiki/Component_Object_Model
@numerical25: To put the previous comment another way -- you're mixing up C++ classes and COM CoClasses. The two are completely unrelated. A CoClass implements a colection of interfaces, and can be written in any COM supported language. Even languages like C which have no concept of classes or objects.
1

CComPtr<> is a smart pointer used to automate managing the object lifetime. It is more or less the same as the Interface* where Interface is the CComPtr<> template parameter (IStream* or IXmlReader* in this example), but provides some additional features that don't influence how the object pointed to function.

So CComPtr<IStream> has an IStream* inside and an overloaded operator ->() which redirects calls to that IStream*. The same applies to CComPtr<IXmlReader> - it has IXmlReader* inside.

Comments

-1

This question requires a complex answer.

COM interfaces are not part of C++ language. They can be implemented using different languages. C++ is just one of them.

Every COM interface inherits from the IUnknown interface, that implements QueryInterface() AddRef() and Release() methods

QueryInterface() must be used to request the COM object interfaces. Since every COM interface inherits from IUnknown, it can be called on any interface.

AddRef() and Release() must be called to manage the object lifetime.

CComPtr<> is a template class, implemented in Microsoft ATL library, to wrap any COM interface, that automatically calls QueryInterface(), AddRef() and Release() when needed.

In your example, CComPtr pFileStream can be used to access IStream interface members of an object.

http://msdn.microsoft.com/en-us/library/ezzw7k98%28VS.80%29.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.