简体   繁体   English

点网核心 dll 为 COM dll 与事件

[英]Dot Net Core dll as COM dll with events

Given it is possible to create a COM dll in Dot Net Core, in at least version 3.1 onwards...鉴于可以在 Dot Net Core 中创建 COM dll,至少在 3.1 版以上...

Is it possible to create a COM dll that is capable of raising COM EVENTS that the client application (NOT dot net of any description) can hook up to?是否可以创建一个 COM 事件,客户端应用程序(没有任何描述的点网)可以连接到 COM 事件的 COM dll?

I have succeeded in doing this with a dll built in Framework 3.5, but have had no success in the three times I've tried this in CORE.我已经使用框架 3.5 中内置的 dll 成功地做到了这一点,但在我在 CORE 中尝试过的三次都没有成功。

Has anyone succeeded in doing this and have any tips or core they'd be kind enough to share.有没有人成功地做到了这一点,并且有任何他们愿意分享的技巧或核心。

Yes, it works as it should.是的,它可以正常工作。

Here is a .NET Core 3.1 or .NET 5 COM class (follow more or less this tutorial and register: Exposing .NET Core components to COM ): Here is a .NET Core 3.1 or .NET 5 COM class (follow more or less this tutorial and register: Exposing .NET Core components to COM ):

namespace ClassLibrary1
{
    [ComVisible(true)]
    [Guid("f39feec6-dd52-4da3-967a-c1e67de9346d")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IServer
    {
        void ComputePi();
        double GetComputedPi();
    }

    [ComVisible(true)]
    [Guid("5ff4acc2-2faa-4ea3-bae9-8c90fa67d75b")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] // this is marked obsolete in .NET Core 3.1 but not any more in .NET 5
    public interface IServerEvents
    {
        [DispId(1234)]
        void OnPiComputed();
    }

    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    [ComSourceInterfaces(typeof(IServerEvents))] // this is marked obsolete in .NET Core 3.1 but not any more in .NET 5
    [Guid("32c58b14-b6fb-41f5-8368-52dc9289ae19")]
    public class Server : IServer
    {
        private event OnPiComputedEvent OnPiComputed;

        public delegate void OnPiComputedEvent();

        public void ComputePi()
        {
            Console.WriteLine("PI is being computed...");
            OnPiComputed?.Invoke();
        }

        public double GetComputedPi() => Math.PI;
    }
}

And for example, a .NET Framework COM client (interface are redefined exactly the same but you could create a common type library/.TLB or share a.cs):例如,.NET 框架 COM 客户端(接口重新定义完全相同,但您可以创建通用类型库/.TLB 或共享 a.cs):

class Program
{
    static void Main()
    {
        var type = Type.GetTypeFromCLSID(new Guid("32c58b14-b6fb-41f5-8368-52dc9289ae19"));
        var obj = (IServer)Activator.CreateInstance(type);

        var container = (IConnectionPointContainer)obj;
        var iid = typeof(IServerEvents).GUID;
        container.FindConnectionPoint(ref iid, out var point);
        
        var sink = new ServerEvents();
        point.Advise(sink, out var cookie);
        
        obj.ComputePi();
        Console.ReadKey(true);

        point.Unadvise(cookie);
    }
}

public class ServerEvents : IServerEvents
{
    public void OnPiComputed()
    {
        Console.WriteLine("PI was computed!");
    }
}

[ComVisible(true)]
[Guid("f39feec6-dd52-4da3-967a-c1e67de9346d")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IServer
{
    void ComputePi();
    double GetComputedPi();
}

[ComVisible(true)]
[Guid("5ff4acc2-2faa-4ea3-bae9-8c90fa67d75b")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IServerEvents
{
    [DispId(1234)]
    void OnPiComputed();
}

When ran, it should display:运行时,它应该显示:

PI is being computed...
PI was computed!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM