Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create A COM object inside another COM object

I have the following interface. The interface returns ISystemCmds interface as part of GetSystemCommandInterface method. We are using ATL for creating the COM.

interface IDevice : IDispatch{  
 [id(1), helpstring("method Connect")] HRESULT Connect([in] VARIANT varPortNo);    
 [id(2), helpstring("method GetSystemCommandInterface")] HRESULT GetSystemCommandInterface([out,retval] ISystemCmds** pISystemCmd);    
};

What code should add (and where) for creating the COM object for ISystemCmds if

a. I am creating the COM object for ISystemCmds as part of COM object creation of IDevice?
b. I am creating the COM object in GetSystemCommandInterface() method?

like image 718
Maanu Avatar asked Feb 03 '26 11:02

Maanu


2 Answers

Use the ATL wizard to create the implementation of ISystemCmds. Then create the object through normal CoCreateInstance, or use the CComObject<> template (see method CreateInstance) if you need to initialize the object in a way that ISystemCmds does not support. Be aware that CComObject<>::CreateInstance() does not AddRef() your object like QueryInterface() and CoCreateInstance() do. AddRef the object before passing it along!

like image 153
Jörgen Sigvardsson Avatar answered Feb 05 '26 03:02

Jörgen Sigvardsson


Getting new COM objects is rather a heavy operation so I think you should consider a variant where one class implements several interfaces as I think it is a perfectly valid assumption that ISystemCmds is not going to outlive IDevice. So think about implementing both interfaces using the same class and calling QueryInterface inside of GetSystemCommandInterface.

like image 37
jszpilewski Avatar answered Feb 05 '26 03:02

jszpilewski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!