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. COM
  4. How to restrict client application to create COM object

How to restrict client application to create COM object

Scheduled Pinned Locked Moved COM
tutorialc++comsysadminquestion
3 Posts 3 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.
  • L Offline
    L Offline
    lvantin
    wrote on last edited by
    #1

    Hi all, I wrote a server(VC++) having 2 COM object(A,B) & I want client application only can create COM object A and can not create COM object B. COM object B will be return by calling a function exported in an interface of COM object A. For example, client application written by VB Dim a = new A Dim b as B call a.getInstanceB(b) call b.doSomething Any body know what to do to make COM object B can not be created by client application? Thanks a lot, Tin Le,

    P L 2 Replies Last reply
    0
    • L lvantin

      Hi all, I wrote a server(VC++) having 2 COM object(A,B) & I want client application only can create COM object A and can not create COM object B. COM object B will be return by calling a function exported in an interface of COM object A. For example, client application written by VB Dim a = new A Dim b as B call a.getInstanceB(b) call b.doSomething Any body know what to do to make COM object B can not be created by client application? Thanks a lot, Tin Le,

      P Offline
      P Offline
      Per Nilsson
      wrote on last edited by
      #2

      As I've been curious about the same thing, I took this as an exercise, and if you use ATL for your COM server, I have the solution for you. ATL includes an object map in its implementation file that includes entries for each class the server implements. The map is located in the COM servers implementation file (named .cpp) and looks like this

      BEGIN_OBJECT_MAP(ObjectMap)
        OBJECT_ENTRY(CLSID_Creatable, CCreatable)
        OBJECT_ENTRY(CLSID_IsNotCreatable, CIsNotCreatable)
      END_OBJECT_MAP()
      

      Change the object entry to OBJECT_ENTRY_NON_CREATEABLE of your non, creatable class.

      BEGIN_OBJECT_MAP(ObjectMap)
        OBJECT_ENTRY(CLSID_Creatable, CCreatable)
        OBJECT_ENTRY_NON_CREATEABLE(CIsNotCreatable)
      END_OBJECT_MAP()
      

      After this, the CIsNotCreatable coclass cannot be created by CoCreateInstance. (Take a look at the documentation of OBJECT_ENTRY_NON_CREATEABLE in MSDN). To let the creatable class create an instance of the non creatable and return a COM pointer ot it, you can do like this

      STDMETHODIMP CCreatable::CreateNotCreatableObject(IIsNotCreatable **obj)
      {
        AFX_MANAGE_STATE(AfxGetStaticModuleState())
      
        HRESULT hr;
      
        //
        // CComObject template class is the root of all ATL COM objects
        // The CComObject implements the IUnknown defined methods.
        //
        // We set up a pointer to a COM object of class CIsNotCreatable
        //(that inherits from IIsNotCreatable)
        //
        CComObject* instanceOfNotCreatableClass;
      
        //
        // Then we can use the CreateInstance static method olf CComObject to
        // create an instance of the object. (Otherwise we could have used new)
        //
        hr = CComObject::CreateInstance(&instanceOfNotCreatableClass);
      
      
        if (SUCCEEDED (hr)) {
          // 
          // As we will return an COM pointer from our method, we must remember
          // to call AddRef on it.
          //
          instanceOfNotCreatableClass->AddRef ();
      
          //
          // Assign the return value
          //
          *obj = instanceOfNotCreatableClass;
        }
      
        return hr;
      }
      
      1 Reply Last reply
      0
      • L lvantin

        Hi all, I wrote a server(VC++) having 2 COM object(A,B) & I want client application only can create COM object A and can not create COM object B. COM object B will be return by calling a function exported in an interface of COM object A. For example, client application written by VB Dim a = new A Dim b as B call a.getInstanceB(b) call b.doSomething Any body know what to do to make COM object B can not be created by client application? Thanks a lot, Tin Le,

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        - Check for noncreatable[^] MIDL attribute - Making an ATL Object Noncreatable[^]* * *

        Have a great day ahead! Regards, Sohail Kadiwala (My Blog - http://blogs.wdevs.com/sohail/[^])

        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