Creating COM objects from COM objects
Author |
Message |
Michael Gunte #1 / 8
|
 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 |
|
 |
Alexander Nickolo #2 / 8
|
 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 |
|
 |
Michael Gunte #3 / 8
|
 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 |
|
 |
Alexander Nickolo #4 / 8
|
 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
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 |
|
 |
Girish Bharadwa #5 / 8
|
 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 |
|
 |
Girish Bharadwa #6 / 8
|
 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 |
|
 |
Alexander Nickolo #7 / 8
|
 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
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 |
|
 |
Girish Bharadwa #8 / 8
|
 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 |
|
|
|