Creating COM objects from COM objects 
Author Message
 Creating COM objects from COM objects

What is the best way to have one ATL COM object create and return another
ATL COM object? I have a property on Object1 which needs to be able to
return a reference to a newly-created Object2. Right now I'm using this,
which seems to work, but I don't think it's right:

STDMETHODIMP CConnection::get_Layer(ILayer **ppLayer)
{
    CoCreateInstance(CLSID_Layer, NULL, CLSCTX_INPROC_SERVER, IID_ILayer,
(LPVOID *)ppLayer);
    CLayer *pLayer = (CLayer *)*ppLayer;
    pLayer->Attach(m_Connection);
    return S_OK;

Quote:
}

--

Software Engineer
Infinity Software Development, Inc.
Phone: (850) 383-1011 - Fax: (850) 383-1015



Mon, 21 Oct 2002 03:00:00 GMT  
 Creating COM objects from COM objects
You are correct in thinking it's not right :). Use CComObject and
create the object internally:

CComObject<CLayer> *pLayer = NULL;
hr = CComObject<CLayer>::CreateInstance(&pLayer);
hr = pLayer->QueryInterface(IID_ILayer, (void**)ppLayer);

Add the appropriate error checking of course.

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD

MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================



Quote:
> What is the best way to have one ATL COM object create and return another
> ATL COM object? I have a property on Object1 which needs to be able to
> return a reference to a newly-created Object2. Right now I'm using this,
> which seems to work, but I don't think it's right:

> STDMETHODIMP CConnection::get_Layer(ILayer **ppLayer)
> {
>     CoCreateInstance(CLSID_Layer, NULL, CLSCTX_INPROC_SERVER, IID_ILayer,
> (LPVOID *)ppLayer);
>     CLayer *pLayer = (CLayer *)*ppLayer;
>     pLayer->Attach(m_Connection);
>     return S_OK;
> }
> --

> Software Engineer
> Infinity Software Development, Inc.
> Phone: (850) 383-1011 - Fax: (850) 383-1015




Mon, 21 Oct 2002 03:00:00 GMT  
 Creating COM objects from COM objects
Awesome, thanks. Now, if my primary object (Connection) is also going to
hold a reference to the secondary object (Layer), do I also need to do an
extra AddRef in there? If so, should I just use CComObject::AddRef for that?



Quote:
> You are correct in thinking it's not right :). Use CComObject and
> create the object internally:

> CComObject<CLayer> *pLayer = NULL;
> hr = CComObject<CLayer>::CreateInstance(&pLayer);
> hr = pLayer->QueryInterface(IID_ILayer, (void**)ppLayer);

> Add the appropriate error checking of course.

> --
> =====================================
> Alexander Nickolov
> Microsoft MVP [VC], MCSD

> MVP VC FAQ: http://www.mvps.org/vcfaq
> =====================================



> > What is the best way to have one ATL COM object create and return
another
> > ATL COM object? I have a property on Object1 which needs to be able to
> > return a reference to a newly-created Object2. Right now I'm using this,
> > which seems to work, but I don't think it's right:

> > STDMETHODIMP CConnection::get_Layer(ILayer **ppLayer)
> > {
> >     CoCreateInstance(CLSID_Layer, NULL, CLSCTX_INPROC_SERVER,
IID_ILayer,
> > (LPVOID *)ppLayer);
> >     CLayer *pLayer = (CLayer *)*ppLayer;
> >     pLayer->Attach(m_Connection);
> >     return S_OK;
> > }
> > --

> > Software Engineer
> > Infinity Software Development, Inc.
> > Phone: (850) 383-1011 - Fax: (850) 383-1015




Mon, 21 Oct 2002 03:00:00 GMT  
 Creating COM objects from COM objects
You can store a C++ object pointer and then you use CComObject::
AddRef. Or you may wish to store a mere interface pointer - then
you have to call QueryInterface instead (or AddRef through that
interface pointer).

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD

MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================



Quote:
> Awesome, thanks. Now, if my primary object (Connection) is also going to
> hold a reference to the secondary object (Layer), do I also need to do an
> extra AddRef in there? If so, should I just use CComObject::AddRef for
that?

>



> > You are correct in thinking it's not right :). Use CComObject and
> > create the object internally:

> > CComObject<CLayer> *pLayer = NULL;
> > hr = CComObject<CLayer>::CreateInstance(&pLayer);
> > hr = pLayer->QueryInterface(IID_ILayer, (void**)ppLayer);

> > Add the appropriate error checking of course.

> > --
> > =====================================
> > Alexander Nickolov
> > Microsoft MVP [VC], MCSD

> > MVP VC FAQ: http://www.mvps.org/vcfaq
> > =====================================

> > Michael Gunter


- Show quoted text -

Quote:

> > > What is the best way to have one ATL COM object create and return
> another
> > > ATL COM object? I have a property on Object1 which needs to be able to
> > > return a reference to a newly-created Object2. Right now I'm using
this,
> > > which seems to work, but I don't think it's right:

> > > STDMETHODIMP CConnection::get_Layer(ILayer **ppLayer)
> > > {
> > >     CoCreateInstance(CLSID_Layer, NULL, CLSCTX_INPROC_SERVER,
> IID_ILayer,
> > > (LPVOID *)ppLayer);
> > >     CLayer *pLayer = (CLayer *)*ppLayer;
> > >     pLayer->Attach(m_Connection);
> > >     return S_OK;
> > > }
> > > --

> > > Software Engineer
> > > Infinity Software Development, Inc.
> > > Phone: (850) 383-1011 - Fax: (850) 383-1015




Mon, 21 Oct 2002 03:00:00 GMT  
 Creating COM objects from COM objects
Nothing wrong with that, a bit slow maybe (due to registry lookups). If the
Layer is a class within the same module, you can try
CComObject::CreateInstance() + QueryInterface() to get the interface and
return it that way..



Quote:
> What is the best way to have one ATL COM object create and return another
> ATL COM object? I have a property on Object1 which needs to be able to
> return a reference to a newly-created Object2. Right now I'm using this,
> which seems to work, but I don't think it's right:

> STDMETHODIMP CConnection::get_Layer(ILayer **ppLayer)
> {
>     CoCreateInstance(CLSID_Layer, NULL, CLSCTX_INPROC_SERVER, IID_ILayer,
> (LPVOID *)ppLayer);
>     CLayer *pLayer = (CLayer *)*ppLayer;
>     pLayer->Attach(m_Connection);
>     return S_OK;
> }
> --

> Software Engineer
> Infinity Software Development, Inc.
> Phone: (850) 383-1011 - Fax: (850) 383-1015




Tue, 22 Oct 2002 03:00:00 GMT  
 Creating COM objects from COM objects
Why is it not right??


Quote:
> You are correct in thinking it's not right :). Use CComObject and
> create the object internally:

> CComObject<CLayer> *pLayer = NULL;
> hr = CComObject<CLayer>::CreateInstance(&pLayer);
> hr = pLayer->QueryInterface(IID_ILayer, (void**)ppLayer);

> Add the appropriate error checking of course.

> --
> =====================================
> Alexander Nickolov
> Microsoft MVP [VC], MCSD

> MVP VC FAQ: http://www.mvps.org/vcfaq
> =====================================



> > What is the best way to have one ATL COM object create and return
another
> > ATL COM object? I have a property on Object1 which needs to be able to
> > return a reference to a newly-created Object2. Right now I'm using this,
> > which seems to work, but I don't think it's right:

> > STDMETHODIMP CConnection::get_Layer(ILayer **ppLayer)
> > {
> >     CoCreateInstance(CLSID_Layer, NULL, CLSCTX_INPROC_SERVER,
IID_ILayer,
> > (LPVOID *)ppLayer);
> >     CLayer *pLayer = (CLayer *)*ppLayer;
> >     pLayer->Attach(m_Connection);
> >     return S_OK;
> > }
> > --

> > Software Engineer
> > Infinity Software Development, Inc.
> > Phone: (850) 383-1011 - Fax: (850) 383-1015




Tue, 22 Oct 2002 03:00:00 GMT  
 Creating COM objects from COM objects
The upcast from interface to its implementation class. What if this
is done in an incompatible apartment type? Not to mention the
implications on supporting this code (what if this code is moved
into a separate DLL?)...

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD

MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================


Quote:
> Why is it not right??



> > You are correct in thinking it's not right :). Use CComObject and
> > create the object internally:

> > CComObject<CLayer> *pLayer = NULL;
> > hr = CComObject<CLayer>::CreateInstance(&pLayer);
> > hr = pLayer->QueryInterface(IID_ILayer, (void**)ppLayer);

> > Add the appropriate error checking of course.

> > --
> > =====================================
> > Alexander Nickolov
> > Microsoft MVP [VC], MCSD

> > MVP VC FAQ: http://www.mvps.org/vcfaq
> > =====================================

> > Michael Gunter


- Show quoted text -

Quote:

> > > What is the best way to have one ATL COM object create and return
> another
> > > ATL COM object? I have a property on Object1 which needs to be able to
> > > return a reference to a newly-created Object2. Right now I'm using
this,
> > > which seems to work, but I don't think it's right:

> > > STDMETHODIMP CConnection::get_Layer(ILayer **ppLayer)
> > > {
> > >     CoCreateInstance(CLSID_Layer, NULL, CLSCTX_INPROC_SERVER,
> IID_ILayer,
> > > (LPVOID *)ppLayer);
> > >     CLayer *pLayer = (CLayer *)*ppLayer;
> > >     pLayer->Attach(m_Connection);
> > >     return S_OK;
> > > }
> > > --

> > > Software Engineer
> > > Infinity Software Development, Inc.
> > > Phone: (850) 383-1011 - Fax: (850) 383-1015




Tue, 22 Oct 2002 03:00:00 GMT  
 Creating COM objects from COM objects
Doh.. I did not see the upcast. I was just looking at the CCI and I thought
that was under question. Yes. You are right. Casting ILayer to CLayer is
*BAD*..


Quote:
> The upcast from interface to its implementation class. What if this
> is done in an incompatible apartment type? Not to mention the
> implications on supporting this code (what if this code is moved
> into a separate DLL?)...

> --
> =====================================
> Alexander Nickolov
> Microsoft MVP [VC], MCSD

> MVP VC FAQ: http://www.mvps.org/vcfaq
> =====================================



> > Why is it not right??



> > > You are correct in thinking it's not right :). Use CComObject and
> > > create the object internally:

> > > CComObject<CLayer> *pLayer = NULL;
> > > hr = CComObject<CLayer>::CreateInstance(&pLayer);
> > > hr = pLayer->QueryInterface(IID_ILayer, (void**)ppLayer);

> > > Add the appropriate error checking of course.

> > > --
> > > =====================================
> > > Alexander Nickolov
> > > Microsoft MVP [VC], MCSD

> > > MVP VC FAQ: http://www.mvps.org/vcfaq
> > > =====================================

> > > Michael Gunter


> > > > What is the best way to have one ATL COM object create and return
> > another
> > > > ATL COM object? I have a property on Object1 which needs to be able
to
> > > > return a reference to a newly-created Object2. Right now I'm using
> this,
> > > > which seems to work, but I don't think it's right:

> > > > STDMETHODIMP CConnection::get_Layer(ILayer **ppLayer)
> > > > {
> > > >     CoCreateInstance(CLSID_Layer, NULL, CLSCTX_INPROC_SERVER,
> > IID_ILayer,
> > > > (LPVOID *)ppLayer);
> > > >     CLayer *pLayer = (CLayer *)*ppLayer;
> > > >     pLayer->Attach(m_Connection);
> > > >     return S_OK;
> > > > }
> > > > --

> > > > Software Engineer
> > > > Infinity Software Development, Inc.
> > > > Phone: (850) 383-1011 - Fax: (850) 383-1015




Fri, 25 Oct 2002 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Return a pointer to a nested COM object from a COM object method

2. Invoking COM object from COM object

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

4. COM Object returning COM Object Reference

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. Problem with scripting context when using VB com object inside VC com object

9. Passing COM objects as parameters to other COM objects

10. Using COM objects within COM objects

11. Confused regarding apartments, when COM objects are forced by COM to be created and when shared

12. Create instance of an com object inside a the Com server

 

 
Powered by phpBB® Forum Software