views:

422

answers:

2

What is the reason for declaring noncreatable coclasses like the following in IDL?

[
    uuid(uuidhere),
    noncreatable
]
coclass CoClass {
    [default] interface ICoClass;
};

I mean such class will not be registered to COM anyway. What's the reason to mention it in the IDL file and in the type library produced by compiling that IDL file?

A: 
Alex Martelli
A: 

The noncreatable attribute is just a hint to the consumer of the object -- .Net and VB6, for example, when seeing this attribute, will not allow the client to create the object "the normal way", e.g. by calling New CoClass() [VB6].

However, the COM server's class factory is the definite authority for deciding whether it allows objects of given class to be created or not -- so in fact, it is possible that a class is marked noncreatable and yet, the class factory allows objects to be created. To avoid such situations, make sure that you update your class factory accordingly.

Mentioning noncreatable classes in the IDL is in fact optional. Note, however, that you get at least one benefit from including them anyway: midl will create CLSID_CoClass constants etc.

Johannes Passing
Let's pretend I don't want the objects of this class to be ever externally created - only return their interfaces by some retrieval/creation methods. Is there any sence in declaring the class in the IDL and making it noncreatable? Why not just omit its declaration?
sharptooth
noncreatable does just what you asked there :). If you leave it in - you will have all the benefits (appearance in the Objects view of any MS-VBA compatible tool for instance) of the other classes but won't be able to create but via your custom creator.
da_m_n

related questions