Returning interface pointer from a COM component
I have a basic question about returning interface pointer from a COM
component, to caller. I provided these samples to just to be clear.
This came to light because CSimpleMap<BSTR, BSTR>, stored in innermost
object(say ILevel4) started loosing data. Then I replaced it with
std::map<wstring,wstring> and started returning interfaces pointer
doing QI(). Please see below, give your thaoughts, these questions
have been nagging me for a while. I hardly comeacross a COM example
that implements nested objects.
_ATL_INTERFACE_DEBUG ignores the interface pointer, returned to
client, generated without calling QI(),
CFirstObject :: FinalContructor()
{
//Here I create the encapsulated COM object
CComObject<CLevel1>::CreateInstance( &m_objILevel1);
}
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();
pLevel1 = (*ppL1);
b)
*ppL1 = (ILevel1*)m_objILevel1;
(*ppL1)->AddRef();
//Calling interface method from inside
a)
m_objILevel1->DoSomething();
b)
pLevel1->DoSomething();
}
class ATL_NO_VTABLE CLevel1 : public IDispatchImpl<ILevel1
{
...........
STDMETHOD(DoSomething)(BSTR);
HRESULT NonInterfaceMethod(); .......
};
class ATL_NO_VTABLE CFirstObject : public IDispatchImpl<IFirstObject
{
...
CComObject<CLevel1> m_objILevel1;
STDMETHOD(GetLevel1)(ILevel1** ppL1);
};
Thanks
Ramesh