Re: ATL and object aggregation (more)
- From: "Olivier" <toon@xxxxxxxxxxxxx>
- Date: Thu, 25 Aug 2005 11:40:42 +0200
<sharachov@xxxxxxxxxxx> a écrit dans le message de news:
1124959749.506193.183740@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi,
> 1. I deal never with such implemenation of interface's method:
>
> IUnknownPtr InObj2::createInstance( IUnknownPtr outer ) {...}
>
> Is it correct? I ask it because the method should have the __stdcall
> convention and, therefore, should Release() the "outer" which is [in]
> parameter. It's not correct.
>
I don't think you should call Release() on an [in] interface's pointer. You
should call AddRef()/Release() only if you want to keep a reference on it in
your class.
In fact, it is not the implementation of the COM interface's method, it is
an helper method that is called by the 'true' COM interface method
implementation. We try to not mix 'standard' C++ code with COM code. In
general we delegate request from COM interface method to an helper method or
(better) to a method of a C++ class instance stored as a member class. We
consider COM object as a wrapper (adapter) over a C++ object (it is a kind
of separation of concerns at the method level if you want).
So the:
STDMETHODIMP InObj3::CreateInstance( [in] IUnknown* Outer, [out, retval]
IUnknown** Object ) method
delegates to
IUnknownPtr InObj3::createInstance( IUnknownPtr outer ) method
We currently use standard / common proprietary code and macros in the
STDMETHODIMP methods (for instance, to check in-out or out-retval pointers
or catch and convert C++ exception to IErrorInfo / HRESULT code and so
on...)
> 2.
> InObj3Ptr mInObj3;
> ...
> mInObj3 = inObj2->CreateInstance( GetControllingUnknown() );
>
> It's wrong because the IUnknown interface will be lost during the Query
> for InObj3 interface. You should accept the IUnknown interface firstly.
>
I'm not sure to understand what you are saying... because our
IInObj2::CreateInstance() method returns an AddRef'ed IUnknown interface
pointer. InObj3Ptr smart-pointer uses IUnknown.QueryInterface() to get
IInObj3 interface
>From _com_ptr_t MSDN documentation:
a.. _com_ptr_t( p ) Constructs a smart pointer from a different smart
pointer type or from a different raw interface pointer. QueryInterface is
called to find an interface pointer of this smart pointer's type. If
QueryInterface fails with an E_NOINTERFACE error, a NULL smart pointer is
constructed.
Here the IInObj2::CreateInstance() smart-pointer code wrapper:
inline IUnknownPtr IInObj2::CreateInstance ( IUnknown * Outer ) {
IUnknown * _result;
HRESULT _hr = raw_CreateInstance(Outer, &_result);
if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
return IUnknownPtr(_result, false);
}
IUnknownPtr takes ownership on _result (look at the 'false' argument in the
IUnknownPtr constructor), then the IInObj3Ptr InObj3Ptr constructor is
called to build the InObj3Ptr inObj3 from the temporary returned value
IUnknownPtr
Is it ok, or am I really wrong?
Olivier
.
- Follow-Ups:
- Re: ATL and object aggregation (more)
- From: sharachov
- Re: ATL and object aggregation (more)
- From: sharachov
- Re: ATL and object aggregation (more)
- References:
- ATL and object aggregation
- From: Olivier
- Re: ATL and object aggregation
- From: Igor Tandetnik
- Re: ATL and object aggregation (more)
- From: Olivier
- Re: ATL and object aggregation (more)
- From: sharachov
- ATL and object aggregation
- Prev by Date: Re: ATL and object aggregation (more)
- Next by Date: forward declaration of __interface
- Previous by thread: Re: ATL and object aggregation (more)
- Next by thread: Re: ATL and object aggregation (more)
- Index(es):
Relevant Pages
|
Loading