Re: ATL and object aggregation (more)



<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


.



Relevant Pages

  • Re: In-process cross-thread (cross-apartment) marshaling
    ... interface IJingleNotify: IUnknown ... // Marshal interface pointer into worker thread ...
    (microsoft.public.win32.programmer.ole)
  • Re: Memory leak in this case?
    ... have retrieved a pointer of CX by IUnknown pointer, ... delete pCX; // memory and resource leak here? ... memory and resource leak, because in IUnknown interface, destructor is the ...
    (microsoft.public.vc.language)
  • Re: Memory leak in this case?
    ... have retrieved a pointer of CX by IUnknown pointer, ... // memory and resource leak here? ... memory and resource leak, because in IUnknown interface, destructor is the ...
    (microsoft.public.vc.language)
  • Re: CComPtr strange behaviour
    ... IUnknown that is an upcast from the interface identified by riid (so ... I didn't think I was doing _any_ casting. ... plain-old casting of COM interface pointer was allowed and when not so I ...
    (microsoft.public.vc.atl)
  • Re: C# DLL mittels COM-Interop aus C aufrufen
    ... Have u tried the IDispatch interface, which is used for those language ... which doesnot have capability of maintaining VTables. ... The __cplusplus symbol is defined automatically when you're compiling C ... appropriate function pointer type. ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)

Loading