CCW's and Duplicate Names

I had a short project during the Christmas/New Year's break.  I had to create some CCW's (COM Callable Wrappers) for a client that wanted to replace some existing COM objects with new .NET objects.  The requirement was that the existing COM clients (built with early-binding) would not need recompiling.  They have a product that allows their customers to write their own extensions.  So requiring hundreds of clients to recompile all of their extensions was something they wanted to avoid.

I started with the usual COM-interop procedures -- define an interface, use attributes for your GUIDs and DISPids, events, etc...  But then I ran into a problem.  One of their COM objects that I was replacing had both a method and an event with the same name.  Since they were a VB.NET shop I was doing all of this work in VB.NET.  Regardless of the language, .NET doesn't let you create a method and an event with the same name.

I had an idea.  While C# gives you two ways to implement an interface -- either explicit or implicit -- VB.NET only gives you one (which matches C#'s implicit implementation).  I wrote a quick test using explicit implementation of an interface method along with an event with the same name as the method.  C# was fine with that and as far as interop goes, the CCW didn't care.  It could find both the method and the event without any problems.

So I checked with the client and they were okay with using C# for the CCW's.  I moved my current code to C# and completed the wrappers.  So if you ever need to do some COM interop and run into an event and method with the same name, look into a C# wrapper with explicit interface implementation.

2 Comments

  • Perhaps you could post some example code here.

    I have just been trying to get events raised fom c# to com with a vb6 consumer and have not been very successful (even using separate event ids, disp ids, interfaces, sourceInterface attributes etc). The com object can be called by nearly all other com consumers except vb6. Vb6 recognises an event is there, but fails when trying to instantiate the object.

    The common perception seems to be that vb6 appears is unable to handle events from c# built ccw, but is fine with vb.net ccw. With this advice I have had to build vb.net wrappers around c# code so that vb6 can consume events. And this works, but it is a pain.

    If you posted some code, I could see if you have managed to get around the problem I am experiencing using c#.


  • See my second COM interop article at 15seconds.com:

    http://www.15seconds.com/issue/060309.htm

    I discuss how the events work in COM interop and include working examples in both C# and VB.NET.

Comments have been disabled for this content.