How to prevent coclass implementations from being exposed in an ATL type library

atlcomidl

I am building an ATL type library with a type of class factory. Something sorta like this:

[ object, uuid(...), ... ]
interface INumber : IDispatch {
    [propget, id(0)] HRESULT Value([out, retval] LONG* pVal);
}

[ object, uuid(...), ... ]
interface INumberFactory : IDispatch {
    [id(1)] HRESULT GetNumber([in] BSTR numberName, [out, retval] INumber* pVal);
}

[ uuid(...) ]
coclass NumberFactory {
    [default] interface INumberFactory;
}

The user can then get an instance of a class that implements the INumber interface through the NumberFactory.

This works well, but I cannot figure out how to define and instantiate the ATL objects that are returned through the NumberFactory.GetNumber() method. If I define the numbers in the IDL like this:

[ uuid(...) ]
coclass One {
    [default] interface INumber;
}

the One coclass can be instantiated by the user. But I would like to restrict it so the only way you can get an instance of the One coclass is by calling NumberFactory.GetNumber("One").

So my question is: How should the IDL be written so the user cannot instantiate One, but still be able to instantiate One from within then NumberFactory coclass and return the INumber interface of One to the user?

Additionally, is there anything special that must be done in the ATL side of things in order for this to work?

Best Answer

  1. Remove the CoClass from the IDL
  2. Remove the CoClass from the object table (remove its OBJECT_ENTRY_AUTO)
  3. Keep The CNumber class
  4. Your code for GetNumber(...) should look something like:
*pVal = new CComObject<CNumber>();  
(*pVal)->AddRef();

This way there is no CoClass to create by the user and your factory is the only one who can produce them.

Related Topic