3

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?

1
  • We need to go deeper! (sorry) Commented Jun 8, 2011 at 9:44

2 Answers 2

2

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!

Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

You cannot implement two IDispatch interfaces, and expect that it will work with late binding clients. You have to do it this way.
I think the OP should precise if he needs IDispatch and the late binding functionality, still he may use some workaround for ATL::IDispatchImpl like described at <codeproject.com/KB/atl/IDispatchImplEx.aspx>
Sweet template! I suppose it cannot resolve name clashes between interface members (IX::X vs IY::X), but that can be designed away on the interface level.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.