What is the purpose of ComDefaultInterfaceAttribute attribute, if the managed object with ClassInterfaceType.None
is marshaled as either IUnknown
o
The ComDefaultInterface
attribute is only really useful if you have more than one interface implemented on a single object. The "first" interface exposed by an object can be important in certain cases, but the order is not actually specified by the language. The attribute forces the interface you specify to be emitted first, with any others coming in a non-specified order.
It is also meant for classes that you are exporting from managed code to COM, so that clients who get your class returned to them in was other than CoCreateObject
get the correct 'default' interface (e.g. if your class is marked as [ClassInterface(ClassInterfaceType.None)]
).
For imported classes that you work with via managed code, or classes that only implement a single interface, the attribute is harmless but essentially useless.
Also, as far as your last question, you rarely have to resort to low-level interface querying when using COM objects in fully managed code. The C# compiler will automatically handle the QueryInterface
calls if you use the normal as
and is
type coercion keywords. In your case, AuthenticationHelper
is being created as a managed AuthenticationHelper
class because that's what you asked for; if you know what interface you want and you know it's implemented, ask for that:
AuthenticateHelper ah = new AuthenticateHelper();
IAuthenticate ia = ah as IAuthenticate;