Is it possible to implment 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 xml lite. it said that I must use a class that implements the IStream interface. It said to declare and instantiate my class that extends Istre开发者_如何学运维am and use CComPtr when declaring the varible. 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 . 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???
if CComptr is used to pull the xml. why do I have to extend . 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 implment 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.
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.
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
精彩评论