Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to create a com class template and use if for CoCreateInstance?

how to create a com class template and use if for CoCreateInstance?

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++comtutorialquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ehaerim
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups