将C#COM服务器事件暴露给Delphi客户端应用程序 [英] Exposing C# COM server events to Delphi client applications

查看:143
本文介绍了将C#COM服务器事件暴露给Delphi客户端应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与这两个非常相似:

My question is very similar to these two:

C#组件事件?

C# - 编写COM服务器 - 事件不会在客户端上触发

然而,不为我工作。类型库文件,没有任何事件定义的提示,所以Delphi看不到它。

However, what worked for them is not working for me. The type library file, does not have any hints of events definitions, so Delphi doesn't see it. The class works fine for other C# applications, as you would expect.

COM服务器工具


  • Visual Studio 2010

  • .NET 4.0

Delphi应用程序


  • Delphi 2010


以下是代码的简化版本:

Here's a simplified version of the code:

 /// <summary>
/// Call has arrived delegate.
/// </summary>
[ComVisible(false)]
public delegate void CallArrived(object sender, string callData);

/// <summary>
/// Interface to expose SimpleAgent events to COM
/// </summary>
[ComVisible(true)]
[GuidAttribute("1FFBFF09-3AF0-4F06-998D-7F4B6CB978DD")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IAgentEvents
{
    ///<summary>
    /// Handles incoming calls from the predictive manager.
    ///</summary>
    ///<param name="sender">The class that initiated this event</param>
    ///<param name="callData">The data associated with the incoming call.</param>
    [DispId(1)]
    void OnCallArrived(object sender, string callData);
}

/// <summary>
/// Represents the agent side of the system. This is usually related to UI interactions.
/// </summary>
[ComVisible(true)]
[GuidAttribute("EF00685F-1C14-4D05-9EFA-538B3137D86C")]
[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(IAgentEvents))]
public class SimpleAgent
{
    /// <summary>
    /// Occurs when a call arrives.
    /// </summary>
    public event CallArrived OnCallArrived;

    public SimpleAgent() {}

    public string AgentName { get; set; }

    public string CurrentPhoneNumber { get; set; }

    public void FireOffCall()
    {
        if (OnCallArrived != null)
        {
            OnCallArrived(this, "555-123-4567");
        }
    }
}

类型库文件属性和方法的定义,但没有可见的事件。我甚至打开类型库在Delphi的查看器,以确保。 Delphi应用程序可以看到和使用任何属性,方法和函数很好。

The type library file has the definitions for the properties and methods, but no events are visible. I even opened the type library in Delphi's viewer to make sure. The Delphi app can see and use any property, methods, and functions just fine. It just doesn't see the events.

感谢任何指针或文章的阅读。

I would appreciate any pointers or articles to read.

推荐答案

我终于得到这个解决后多试验和错误。有两件事我需要改变C#代码。

I finally got this resolved after much trial and error. There were 2 things that I needed to change on the C# code.

1)[ClassInterface(ClassInterfaceType.None)]需要更改为[ClassInterface(ClassInterfaceType.AutoDual )]

1) [ClassInterface(ClassInterfaceType.None)] needed to be changed to [ClassInterface(ClassInterfaceType.AutoDual)]

2)作为事件源的类需要继承MarshalByRefObject。这有助于如果在源类中有任何线程。

2) The class that is the source of the events needs to inherit from MarshalByRefObject. This helps if there is any threading done in the source class.

我只需要一个东西在Delphi一侧。我需要确保选中Generate Component Wrapper复选框。

I only needed one thing on the Delphi side. I needed to make sure to have the "Generate Component Wrapper" checkbox checked. This is what will actually build the event scaffolding on Delphi's side.

这是你在Delphi 7中的做法:

This is how you do it in Delphi 7:


  1. 从项目 - >导入类型库中选择

  2. 确保选中生成组件包装器

  3. 从列表中选择COM类别

  4. 点击添加单位按钮。

  1. Select from the menu Project -> Import Type Library
  2. Make sure that "Generate Component Wrapper" is checked
  3. Select the COM class from the list
  4. Click on the "Add Unit" button

新单元将包含您的COM事件的定义。

The new unit will have the definitions of your COM events.

如何执行此操作的分步博客帖子

这篇关于将C#COM服务器事件暴露给Delphi客户端应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆