tags:

views:

201

answers:

2

I have implemented a COM server in C#, that has a vb6 client.

When going to fire my events, the handlers are always null--it appears that the vb6 app never subscribes to my events.

The vb6 application is an existing 3rd party app, and appears to give no error messages.

Normal methods work just fine from COM client -> server.

Is there anything I can do to debug what is going on? Or why my events are not working?

Here is a quick example snippet of my code:

    [ComVisible(true),
        Guid(Constants.CLASS_IID),
        ProgId(Constants.PROG_ID),
        ClassInterface(ClassInterfaceType.None),
        ComSourceInterfaces(typeof(IMyServiceEvents))]
    public class MyClass : Control, IMyService, IMyServiceEvents
    {
       [DispId(1)]
       public event MyEventHandler MyEvent;

       //... also implements IMyService, which is working
    }

    [ComVisible(true),
        InterfaceType(ComInterfaceType.InterfaceIsIDispatch),
        Guid(Constants.EVENTS_IID)]
    public interface IMyServiceEvents
    {
        [PreserveSig, DispId(1)]
        void MyEvent([In]int Status);
    }

    [ComVisible(false)]
    public delegate void MyEventHandler(int Status);

If I try the existing ocx file that I'm trying to replace/implement with my C# com server, the events work as usual. It is written in vb6 as well, so something in my C# server must be wrong.

I also want to add that I tried all 3 settings for ClassInterfaceType, and got the same results--although I had to re-register my COM server for each try.

Looking at my generated IDL, it looks correct to me, and everything seems to be very similar to the original IDL I'm trying to recreate, I can post if I need to.

UPDATE: Well I pulled out the old Visual Studio 6, and made a VB6 application to test my C# COM server, and it worked fine.

So I took a free vb6 decompiler that could output the decompiled code to a vb6 project, and ran it on the 3rd party app that I want to load my COM server--and saw it didn't work.

I noticed that their application is using my COM server as a control from the designer, and my test program merely declared a member variable in the form as WithEvents. Is there something going on behind the scenes with the designer that is breaking this? How can I make my COM server ActiveX compatible? I also notice that the VB6 ide won't let me add my C# com server to the toolbox as a control.

+1  A: 

I vaguely remember having this error myself, it's like the run time doesn't know how to map the COM registration to your event.

I went back to look at what I did an the only difference I can see is that the delegate definition is inside my COM class which I also see occurring in this example.

So try moving the MyEventHandler inside of MyClass as below:

   [ComVisible(true),
        Guid(Constants.CLASS_IID),
        ProgId(Constants.PROG_ID),
        ClassInterface(ClassInterfaceType.None),
        ComSourceInterfaces(typeof(IMyServiceEvents))]
    public class MyClass : Control, IMyService, IMyServiceEvents
    {
       [ComVisible(false)]
       public delegate void MyEventHandler(int Status);

       [DispId(1)]
       public event MyEventHandler MyEvent;



       //... also implements IMyService, which is working
    }

    [ComVisible(true),
        InterfaceType(ComInterfaceType.InterfaceIsIDispatch),
        Guid(Constants.EVENTS_IID)]
    public interface IMyServiceEvents
    {
        [PreserveSig, DispId(1)]
        void MyEvent([In]int Status);
    }
shf301
Good try, but it did not work. I'm wondering if their vb6 code is doing something incorrectly that does not allow the .Net CCW to handle the event. I have also tried several things, adding a snk and putting the assembly in the GAC vs. using regasm /codebase, but nothing seems to work. However, standard methods work just fine.
Jonathan.Peppers
+1  A: 

I was deriving from Control, not UserControl, I saw an example of implementing an ActiveX control in C# that brought me to this.

Apparently UserControl implements some interface that makes this work correctly... who knows...

Jonathan.Peppers