Re: Returning interface pointer from a COM component
asnowfall@gmail.com wrote:
CFirstObject :: FinalContructor()
{
//Here I create the encapsulated COM object
CComObject<CLevel1>::CreateInstance( &m_objILevel1);
Be aware that CComObject::CreateInstance creates an object with initial
reference count of zero. If you want to keep the pointer long term, it's
a good idea to AddRef it. Otherwise, the object may be destroyed
prematurely.
STDMETHODIMP CFirstObject :: GetLevel1( ILevel1** ppL1)
{
//Returning to Interface pointer: Which one is among these is
correct.
a)
ILevel* pLevel1;
m_objILevel1->QueryInterface(IID_ILevel1, (void**)ppL1 );
(*ppL1)->AddRef();
QueryInterface already AddRef's.
pLevel1 = (*ppL1);
What's that supposed to achieve?
b)
*ppL1 = (ILevel1*)m_objILevel1;
(*ppL1)->AddRef();
Both a) and b) would work, once you remove an extraneous AddRef in a).
//Calling interface method from inside
a)
m_objILevel1->DoSomething();
b)
pLevel1->DoSomething();
Both would work if DoSomething is a method on ILevel1 interface. a) has
the advantage that you can call any method on CLevel1 class, including
those not exposed via COM.
class ATL_NO_VTABLE CLevel1 : public IDispatchImpl<ILevel1
{
...........
STDMETHOD(DoSomething)(BSTR);
When you were calling DoSomething above, you weren't passing any
parameters.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925