Passing a COM object to a non-COM aware class
Author |
Message |
Mike Grig #1 / 6
|
 Passing a COM object to a non-COM aware class
Hi, I have a COM service that defines two COM objects. The first (ProductDataAccess) creates instances of the second (ProductDataObject) and then populates them from a database. To perform the data population the ProductDataObject needs to be passed into the constructor of another, non-COM class. Here is the function: STDMETHODIMP CCOMProductDataAccess::getData(/*[in]*/BSTR identifier, /*[out,retval]*/ ICOMProductDataObject** result) { // create the COM object that we want to populate CComPtr<ICOMProductDataObject> myCOMobj; HRESULT hr = myCOMobj.CreateInstance(CLSID_COMProductDataObject); if (FAILED(hr)) return hr; // now create the non-COM object that will perform the population GetStockInfo gsi(myCOMobj); // call function on GetStockInfo class that will populate the COM object gsi->getData(); // send the newly populated object back to the client (*result) = myCOMobj; return S_OK; Quote: }
The prototype of the GetStockInfo class's constructor is: class GetStockInfo { public: GetStockInfo(CCOMProductDataObject* obj) { // load stuff from database obj->populate(param1, param2, ..., paramN); } Quote: };
However, I don't seem to be able to get the GetStockInfo class to recognise what the CCOMProductDataObject is. Am I wrong in thinking that, in this case, I can just pass the CCOMProductDataObject to the function and that I don't need to worry about the interface pointer and stuff like that? This is all taking place within the same COM call, so I'm sure it should be possible. Any help greatly appreciated! If you could copy any responses to my e-mail address as well as to the newsgroup then that would be most helpful. thanks guys mike
|
Mon, 15 Aug 2005 21:13:02 GMT |
|
 |
Igor Tandetni #2 / 6
|
 Passing a COM object to a non-COM aware class
Make it CComObject<CCOMProductDataObject>* pObj; HRESULT hr = CComObject<CCOMProductDataObject>::CreateInstance(&pObj); // Attention: new object is created with refcount==0 GetStockInfo gsi(pObj); // ... return pObj->QueryInterface(result); -- With best wishes, Igor Tandetnik "For every complex problem, there is a solution that is simple, neat, and wrong." H.L. Mencken
Quote: > Hi, > I have a COM service that defines two COM objects. The first > (ProductDataAccess) creates instances of the second > (ProductDataObject) and then populates them from a database. To > perform the data population the ProductDataObject needs to be passed > into the constructor of another, non-COM class. Here is the function: > STDMETHODIMP CCOMProductDataAccess::getData(/*[in]*/BSTR identifier, > /*[out,retval]*/ ICOMProductDataObject** result) > { > // create the COM object that we want to populate > CComPtr<ICOMProductDataObject> myCOMobj; > HRESULT hr = myCOMobj.CreateInstance(CLSID_COMProductDataObject); > if (FAILED(hr)) return hr; > // now create the non-COM object that will perform the population > GetStockInfo gsi(myCOMobj); > // call function on GetStockInfo class that will populate the COM > object > gsi->getData(); > // send the newly populated object back to the client > (*result) = myCOMobj; > return S_OK; > } > The prototype of the GetStockInfo class's constructor is: > class GetStockInfo > { > public: > GetStockInfo(CCOMProductDataObject* obj) > { > // load stuff from database > obj->populate(param1, param2, ..., paramN); > } > }; > However, I don't seem to be able to get the GetStockInfo class to > recognise what the CCOMProductDataObject is. Am I wrong in thinking > that, in this case, I can just pass the CCOMProductDataObject to the > function and that I don't need to worry about the interface pointer > and stuff like that? This is all taking place within the same COM > call, so I'm sure it should be possible. > Any help greatly appreciated! If you could copy any responses to my > e-mail address as well as to the newsgroup then that would be most > helpful. > thanks guys > mike
|
Mon, 15 Aug 2005 23:23:40 GMT |
|
 |
Mike Grig #3 / 6
|
 Passing a COM object to a non-COM aware class
Quote:
> > Hi, > > I have a COM service that defines two COM objects. The first > > (ProductDataAccess) creates instances of the second > > (ProductDataObject) and then populates them from a database. To > > perform the data population the ProductDataObject needs to be passed > > into the constructor of another, non-COM class. Here is the function: > > STDMETHODIMP CCOMProductDataAccess::getData(/*[in]*/BSTR identifier, > > /*[out,retval]*/ ICOMProductDataObject** result) > > { > > // create the COM object that we want to populate > > CComPtr<ICOMProductDataObject> myCOMobj; > > HRESULT hr = myCOMobj.CreateInstance(CLSID_COMProductDataObject); > > if (FAILED(hr)) return hr; > > // now create the non-COM object that will perform the population > > GetStockInfo gsi(myCOMobj); > > // call function on GetStockInfo class that will populate the COM > > object > > gsi->getData(); > > // send the newly populated object back to the client > > (*result) = myCOMobj; > > return S_OK; > > } > > The prototype of the GetStockInfo class's constructor is: > > class GetStockInfo > > { > > public: > > GetStockInfo(CCOMProductDataObject* obj) > > { > > // load stuff from database > > obj->populate(param1, param2, ..., paramN); > > } > > }; > > However, I don't seem to be able to get the GetStockInfo class to > > recognise what the CCOMProductDataObject is. Am I wrong in thinking > > that, in this case, I can just pass the CCOMProductDataObject to the > > function and that I don't need to worry about the interface pointer > > and stuff like that? This is all taking place within the same COM > > call, so I'm sure it should be possible. > > Any help greatly appreciated! If you could copy any responses to my > > e-mail address as well as to the newsgroup then that would be most > > helpful. > > thanks guys > > mike
> Make it > CComObject<CCOMProductDataObject>* pObj; > HRESULT hr = CComObject<CCOMProductDataObject>::CreateInstance(&pObj); > // Attention: new object is created with refcount==0 > GetStockInfo gsi(pObj); > // ... > return pObj->QueryInterface(result); > -- > With best wishes, > Igor Tandetnik > "For every complex problem, there is a solution that is simple, neat, > and wrong." H.L. Mencken
Thanks Igor, that's great. Why don't the books tell me things like that?! I ended up just passing the interface pointer around and that's working fine. Just got to figure out why my object's crashing when it's destroyed now...no doubt I'll be posting here to try and figure it out :-) My copy of Win32 Multithreaded Programming arrived today. More books.... all the best mike
|
Tue, 16 Aug 2005 16:03:38 GMT |
|
 |
Kim Gr?sma #4 / 6
|
 Passing a COM object to a non-COM aware class
Hi Mike, Quote: > Thanks Igor, that's great. Why don't the books tell me things like > that?!
ATL Internals [1] does :) Quote: > Just got to figure out why my object's crashing when > it's destroyed now...
Wild guess: It's Release()'d after CoUninitialize(). [1] http://www.amazon.com/o/ASIN/0201695898/ -- Best regards, Kim
Quote:
> > > Hi, > > > I have a COM service that defines two COM objects. The first > > > (ProductDataAccess) creates instances of the second > > > (ProductDataObject) and then populates them from a database. To > > > perform the data population the ProductDataObject needs to be passed > > > into the constructor of another, non-COM class. Here is the function: > > > STDMETHODIMP CCOMProductDataAccess::getData(/*[in]*/BSTR identifier, > > > /*[out,retval]*/ ICOMProductDataObject** result) > > > { > > > // create the COM object that we want to populate > > > CComPtr<ICOMProductDataObject> myCOMobj; > > > HRESULT hr = myCOMobj.CreateInstance(CLSID_COMProductDataObject); > > > if (FAILED(hr)) return hr; > > > // now create the non-COM object that will perform the population > > > GetStockInfo gsi(myCOMobj); > > > // call function on GetStockInfo class that will populate the COM > > > object > > > gsi->getData(); > > > // send the newly populated object back to the client > > > (*result) = myCOMobj; > > > return S_OK; > > > } > > > The prototype of the GetStockInfo class's constructor is: > > > class GetStockInfo > > > { > > > public: > > > GetStockInfo(CCOMProductDataObject* obj) > > > { > > > // load stuff from database > > > obj->populate(param1, param2, ..., paramN); > > > } > > > }; > > > However, I don't seem to be able to get the GetStockInfo class to > > > recognise what the CCOMProductDataObject is. Am I wrong in thinking > > > that, in this case, I can just pass the CCOMProductDataObject to the > > > function and that I don't need to worry about the interface pointer > > > and stuff like that? This is all taking place within the same COM > > > call, so I'm sure it should be possible. > > > Any help greatly appreciated! If you could copy any responses to my > > > e-mail address as well as to the newsgroup then that would be most > > > helpful. > > > thanks guys > > > mike
Quote: > > Make it > > CComObject<CCOMProductDataObject>* pObj; > > HRESULT hr = CComObject<CCOMProductDataObject>::CreateInstance(&pObj); > > // Attention: new object is created with refcount==0 > > GetStockInfo gsi(pObj); > > // ... > > return pObj->QueryInterface(result); > > -- > > With best wishes, > > Igor Tandetnik > > "For every complex problem, there is a solution that is simple, neat, > > and wrong." H.L. Mencken > Thanks Igor, that's great. Why don't the books tell me things like > that?! > I ended up just passing the interface pointer around and that's > working fine. Just got to figure out why my object's crashing when > it's destroyed now...no doubt I'll be posting here to try and figure > it out :-) > My copy of Win32 Multithreaded Programming arrived today. More > books.... > all the best > mike
|
Tue, 16 Aug 2005 16:26:23 GMT |
|
 |
Mike Grig #5 / 6
|
 Passing a COM object to a non-COM aware class
Quote:
> Hi Mike, > > Thanks Igor, that's great. Why don't the books tell me things like > > that?! > ATL Internals [1] does :)
Does it? I couldn't find it in there, although admittedly I don't have an intimate knowledge of the book. If there were 50 hours in a day, then maybe... Quote: > > Just got to figure out why my object's crashing when > > it's destroyed now... > Wild guess: It's Release()'d after CoUninitialize().
No, turned out that it was in my client code and nothing to do with COM/ATL. However, see my new post for the latest problem.... Quote: > [1] http://www.amazon.com/o/ASIN/0201695898/ > -- > Best regards, > Kim
Thanks again Kim. mike Quote:
> > > > Hi, > > > > I have a COM service that defines two COM objects. The first > > > > (ProductDataAccess) creates instances of the second > > > > (ProductDataObject) and then populates them from a database. To > > > > perform the data population the ProductDataObject needs to be passed > > > > into the constructor of another, non-COM class. Here is the function: > > > > STDMETHODIMP CCOMProductDataAccess::getData(/*[in]*/BSTR identifier, > > > > /*[out,retval]*/ ICOMProductDataObject** result) > > > > { > > > > // create the COM object that we want to populate > > > > CComPtr<ICOMProductDataObject> myCOMobj; > > > > HRESULT hr = myCOMobj.CreateInstance(CLSID_COMProductDataObject); > > > > if (FAILED(hr)) return hr; > > > > // now create the non-COM object that will perform the population > > > > GetStockInfo gsi(myCOMobj); > > > > // call function on GetStockInfo class that will populate the COM > > > > object > > > > gsi->getData(); > > > > // send the newly populated object back to the client > > > > (*result) = myCOMobj; > > > > return S_OK; > > > > } > > > > The prototype of the GetStockInfo class's constructor is: > > > > class GetStockInfo > > > > { > > > > public: > > > > GetStockInfo(CCOMProductDataObject* obj) > > > > { > > > > // load stuff from database > > > > obj->populate(param1, param2, ..., paramN); > > > > } > > > > }; > > > > However, I don't seem to be able to get the GetStockInfo class to > > > > recognise what the CCOMProductDataObject is. Am I wrong in thinking > > > > that, in this case, I can just pass the CCOMProductDataObject to the > > > > function and that I don't need to worry about the interface pointer > > > > and stuff like that? This is all taking place within the same COM > > > > call, so I'm sure it should be possible. > > > > Any help greatly appreciated! If you could copy any responses to my > > > > e-mail address as well as to the newsgroup then that would be most > > > > helpful. > > > > thanks guys > > > > mike
> > > Make it > > > CComObject<CCOMProductDataObject>* pObj; > > > HRESULT hr = CComObject<CCOMProductDataObject>::CreateInstance(&pObj); > > > // Attention: new object is created with refcount==0 > > > GetStockInfo gsi(pObj); > > > // ... > > > return pObj->QueryInterface(result); > > > -- > > > With best wishes, > > > Igor Tandetnik > > > "For every complex problem, there is a solution that is simple, neat, > > > and wrong." H.L. Mencken > > Thanks Igor, that's great. Why don't the books tell me things like > > that?! > > I ended up just passing the interface pointer around and that's > > working fine. Just got to figure out why my object's crashing when > > it's destroyed now...no doubt I'll be posting here to try and figure > > it out :-) > > My copy of Win32 Multithreaded Programming arrived today. More > > books.... > > all the best > > mike
|
Tue, 16 Aug 2005 23:56:42 GMT |
|
 |
Kim Gr?sma #6 / 6
|
 Passing a COM object to a non-COM aware class
Mike, Quote: > > ATL Internals [1] does :) > Does it? I couldn't find it in there, although admittedly I don't > have an intimate knowledge of the book. If there were 50 hours in a > day, then maybe...
To be perfectly honest, I didn't understand it from ATL Internals, either, though I'm sure it's described there. I had to have someone explain it to me :) One step at a time. -- Best regards, Kim
|
Wed, 17 Aug 2005 00:31:03 GMT |
|
|
|