C # - COM server record - events not triggered on the client

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

When my events are triggered, the handlers are always zero - it seems that the vb6 application never subscribes to my events.

The vb6 application is an existing third-party application and does not appear to report errors.

Normal methods work fine with COM client -> server.

Is there anything I can do to debug what is happening? Or why my events do not work?

Here is a quick code 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 an existing ocx file that I am trying to replace / implement using my C # com server, the events work as usual. It is also written in vb6, so something on my C # server should be wrong.

I also want to add that I tried all 3 parameters for ClassInterfaceType and got the same results - although I had to re-register my COM server for every attempt.

Looking at my generated IDL, it looks correct to me, and everything seems very similar to the original IDL, which I am trying to recreate, I can publish 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 the free vb6 decompiler, which could output the decompiled code to the vb6 project and run it on a third-party application that I want to download to my COM server, and saw that it was not working.

I noticed that their application uses my COM server as a control from the designer, and my test program simply declared a member variable as WithEvents. Is something going on behind the scenes with a designer who breaks this? How can I make my COM server compatible with ActiveX? I also notice that VB6 ide will not allow me to add my C # com server to the toolbar as a control.

0
source share
3 answers

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

Apparently UserControl implements some interface that does this job right ... who knows ...

+1
source

I vaguely remember that I had this error, because the runtime does not know how to match the COM registration with your event.

I came back to see what I did, the only difference I see is that the delegate definition is inside my COM class, which I also see in this example .

So, try moving MyEventHandler inside MyClass, as shown 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); } 
+1
source

You probably forgot to add [assembly: Guid ("...")] to your project, which means that the TypeLib identifier in COM changes every time you recompile a C # project.

Without an attribute, the compiler will create another type library, but some COM functions work without matching library identifiers.

0
source

Source: https://habr.com/ru/post/1310273/


All Articles