Return a pointer to a nested COM object from a COM object method 
Author Message
 Return a pointer to a nested COM object from a COM object method

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?



Sat, 09 Apr 2005 02:06:38 GMT  
 Return a pointer to a nested COM object from a COM object method
Hi,


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?

Check older posts an read: don't do that!

Its seldom a good idea to rely on the C++ implementation when you are working with
a COM interface pointer as you might only be using a proxy to the real object so a
type cast won't do in all cases.

If you really, really, really must (which I doubt ;-) ) : be sure to only use the
C++ object pointer inproc and avoid marshalling (so make you COM object BOTH as
apartment model). So if your interfaces are directly inherited and implemented
from you c++ class you should be able to type cast back to your C++ class.
To make this hack more clear you could also use COM_INTERFACE_ENTRY_IID(IID_NULL,
CAtlObj) and then call:

CAtlObj* pObj = NULL;
HRESULT hr = QueryInterface(IID_NULL, (void**)&pObj);

BUT the better is to implement a second interface where you put your methods you
would like to call from you code - so you keep it the COM way... What good reason
would there be not to do so?

HTH,
Sven



Sat, 09 Apr 2005 03:21:47 GMT  
 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



Sat, 09 Apr 2005 03:58:00 GMT  
 Return a pointer to a nested COM object from a COM object method
Thanks Igor,

Exactly what I needed!

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



Sat, 09 Apr 2005 18:07:45 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. COM Object returning COM Object Reference

2. COM/ATL novice:passing object pointers through methods on a COM interface

3. passing COM object pointers to another COM object in ATL

4. Invoking COM object from COM object

5. COM object in a COM object

6. Passing COM objects as parameters to other COM objects

7. C++ COM-Object with COM-Object as parameter

8. Creating COM objects from COM objects

9. Problem with scripting context when using VB com object inside VC com object

10. Passing COM objects as parameters to other COM objects

11. Using COM objects within COM objects

12. Returns a ATL Simple object by a method of another ATLSimple object

 

 
Powered by phpBB® Forum Software