how to create a com class template and use if for CoCreateInstance?
-
I have a need for creating a class template and you may help me. [1] Multiple coclasses will be implemented in a single dll. [2] Each coclass will implement the same interfaces but will have different class/clsid/resid. [3] It is necessary to create a class template taking clsid/resid as its template paramemters. [4] Finally, I need to use template classes for CoCreateInstance. Let me elaborate more in detail. Here is a simple interface IA defined. interface IA : IDispatch { [id(1), helpstring("method M1")] HRESULT M1(); }; Below ia a coclass defined with Class(CComX), Clsid(CLSID_ComX), Resid(IDR_COMX). It implements Intid(IA). class ATL_NO_VTABLE CComX : public CComObjectRootEx, , public CComCoClass , public IDispatchImpl { CComX(); ~CComX(); BEGIN_COM_MAP(CComX) COM_INTERFACE_ENTRY(IA) COM_INTERFACE_ENTRY2(IDispatch, IA) END_COM_MAP() DECLARE_REGISTRY_RESOURCEID(IDR_COMX) ... } OBJECT_ENTRY_AUTO(CLSID_ComX, CComX) Next, I need to create second coclass implementing the same interface IA but having different Class(CComY), Clsid(CLSID_ComY), and Resid(IDR_COMY). So, I simply copied the above coclass and replaced 'X' with 'Y'. class ATL_NO_VTABLE CComY : public CComObjectRootEx, , public CComCoClass , public IDispatchImpl { CComY(); ~CComY(); BEGIN_COM_MAP(CComY) COM_INTERFACE_ENTRY(IA) COM_INTERFACE_ENTRY2(IDispatch, IA) END_COM_MAP() DECLARE_REGISTRY_RESOURCEID(IDR_COMY) ... } OBJECT_ENTRY_AUTO(CLSID_ComY, CComY) And I successfully CoCreateInstance'd these two coclasses: HRESULT hr = S_OK; ULONG rc = 0; IA *pXA = NULL; hr = CoCreateInstance(CLSID_ComX, NULL, CLSCTX_ALL, IID_IA, (void **)&pXA); rc = pXA->Release(); IA *pYA = NULL; hr = CoCreateInstance(CLSID_ComY, NULL, CLSCTX_ALL, IID_IA, (void **)&pYA); rc = pYA->Release(); So far so good! The problem is that I need to create many coclasses now and in the future. I don't want to copy/paste for all of them because it is quite hard to maintain the source code for all the copy/paste'd coclasses. It is quite error-prone. So, I definitely would like to create a class template. Below, I created a class template and it compiled ok. template class ATL_NO_VTABLE CCom