views:

271

answers:

3

Hi, we're considering exposing some C# types to C++ clients via COM. What problems can we expect to hit over the life of the project? E.g. how will versioning be managed?

On versioning, it would seem from reading this that we should decorate our types to be exposed with [ClassInterface(ClassInterfaceType.None)] and use an explicit interface. That way I assume we fully control the interface that will be exposed to COM clients.

Thanks in advance.

+1  A: 

Since you are using a C++ client you should definitely use explicit interfaces for early binding. Dispatch interfaces are useful when using scripting clients such as VBS but they are rarely useful for C++ clients.

The only way to version an interface is to create a new interface (possibly inheriting from the original interface). When using explicit interfaces you have full control over this process.

This means you should create an interface for every class that you intend to expose via COM. Don't forget to mark every interface and class with the ComVisible and Guid attributes. Also all your classes must have a default constructor.

Jakob Christensen
Only classes he wants to be instantiated with CoCreateInstance need to have default constructors and be exposed to COM. If he only wants a factory method somewhere the class implementing the interface doesn't need to be COM exposed and have a default constructor.
sharptooth
Where would the factory method live?
ng5000
I mean you could have several exposed classes that will have multiple factory or retrieveal methods returning some interfaces not related to these classes. These classes need to be exposed, but the classes that implement those returned interfaces need not.
sharptooth
@sharptooth: Good point.
Jakob Christensen
+1  A: 

You'll have to read about the GUID attribute (including this) to maintain binary compatibility and only rebuild the clients when necessary.

Also you might be interested in the ComVisible attribute that helps reduce registry pollution.

sharptooth
+1  A: 

To get full control over COM interfaces, define them in MIDL. Build a type library with those interfaces in a C++ project, then import type library to C# and implement interfaces.

This approach is useful with complex interfaces where marshaling is not trivial.

Versions should be done COM-style, changing GUIDs and adding new or inheriting interfaces.

ima