Question

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?

Was it helpful?

Solution

  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.

OTHER TIPS

Apparently I was searching for the wrong thing... I found the answer in a related question (from the sidebar) even though I was unable to find it while searching before asking the question...

The answer is two-fold:

  1. Add noncreatable to the coclass definition in the IDL
  2. Remove the OBJECT_ENTRY_AUTO entry from the ATL header file (at the bottom if generated by the wizard)

See this MSDN article: http://msdn.microsoft.com/en-us/library/4tc639ss.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top