
Return a pointer to a nested COM object from a COM object method
Quote:
> I have a function which I have declared:
> STDMETHOD(OpenPipe)(/*[in]*/ long swPipe, /*[out, retval]*/ IPipe
> **ppIPipe);
> Within this function, I want to instantiate a new nested COM object
> (to which I can store in a map<int,???> and call functions of the C++
> class (which are not exposed COM methods) at a later stage.
> How do I get hold of the actual "CAtlObj" C++ object after calling
> CoCreateInstance or whatever?
I assume the object is implemented in the same module that implements
OpenPipe. In this case, don't use CoCreateInstance. Create your object
this way:
CComObject<CPipe>* pPipe;
CComObject<CPipe>::CreateInstance(&pPipe);
// At this point, pPipe is the pointer to raw C++ class
// You can store this pointer somewhere for latter use
// Be aware that the object is created with 0 refcount
// You may want to call AddRef if you don't want the object
// to die on you
// retrieve an interface pointer and return it to the caller
pPipe->QueryInterface(ppIPipe);
// This call implicitly AddRef's
--
With best wishes,
Igor Tandetnik
"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken