How to restrict client application to create COM object
-
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,
-
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,
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; }
-
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,
- 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/[^])