C# - 写一个COM服务器 - 事件不触发客户端客户端、事件、服务器、COM

2023-09-04 06:41:28 作者:心如荒島囚我終老

我已经实现了在C#中的COM服务器,具有VB6的客户端。

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

当要解雇我的事件处理程序总是空 - 看来,VB6应用程序从未订阅我的事件

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

VB6的应用程序是一个现有的第三方应用程序,并显示给任何错误消息。

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

普通方式工作得很好,从COM客户端 - >服务器

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?

下面是我的code一个简单的例子片断:

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);

如果我尝试,我想,以取代现有的OCX文件/实现与我的C#COM服务器,事件照常上班。这是写在维生素B6一样,所以东西在我的C#服务器必须是错误的。

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.

我也想补充一点,我尝试了所有3设置ClassInterfaceType,并得到了相同的结果 - 虽然我不得不重新登记自己的COM服务器为每个尝试

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.

看着我产生IDL,它看起来正确的我,一切似乎是非常相似的原始IDL我试图重现,我可以张贴如果我需要。

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.

更新:嗯,我拿出旧的Visual Studio 6,并提出了VB6应用程序来测试我的C#COM服务器,它工作得很好

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.

于是我花了一个免费的VB6的反编译器,可以输出反编译code到VB6的项目,并运行它,我想我的加载COM服务器的第三方应用程序 - 看见没有奏效。

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.

我注意到,他们的应用程序使用我的COM服务器作为从设计师的控制,我的测试程序只是声明的形式为WithEvents就一个成员变量。是不是有什么事情在幕后与正在打破这一设计?我怎样才能让我的COM服务器的ActiveX兼容?我也注意到,VB6 IDE不会让我我的C#COM服务器添加到工具箱作为对照。

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.

推荐答案

我是从控制,而不是用户控件派生,我看到了实现一个ActiveX控件在把我带到这个C#的例子。

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...