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

别来无恙 提交于 2019-12-23 18:04:04

问题


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?


回答1:


  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.




回答2:


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



来源:https://stackoverflow.com/questions/1191012/how-to-prevent-coclass-implementations-from-being-exposed-in-an-atl-type-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!