COM+ 使用 C# 和 .NET 的事件

发布于 2024-07-23 22:40:17 字数 854 浏览 2 评论 0 原文

我试图通过 COM 访问应用程序的某些部分,主要是因为我需要 Delphi 应用程序能够调用 .NET 应用程序上的某些服务。

我已经设法通过继承自 ServicedComponent 类的 COM 公开一些方法,并且它工作正常。

现在我需要能够向客户提出事件,以便在发生某些事情时我可以提醒他。 我目前有这样的东西:

public interface IEvents
{
  void OnMessage();
}

[EventClass]    
[ComVisible(true)]
public class MyEventsClass : ServicedComponent, IEvents
{
  public void OnMessage()
  {
  }
}

但是当我从 Delphi 导入 TypeLibrary 时,我得到这个:

IEvents = interface(IDispatch)
    ['{E7605303-F968-3509-829B-BF70084053C4}']
    procedure OnMessage; safecall;
  end;

IEventsDisp = dispinterface
    ['{E7605303-F968-3509-829B-BF70084053C4}']
    procedure OnMessage; dispid 1610743808;
  end;

我真的不知道如何使用。 我期待一个 IUnknown 接口,我可以通过 MyService.Register(IEvents events) 实现并提供给服务...

我想我完全迷失在这里...有关如何正确实现 COM+ 事件的任何建议或参考。网?

I'm trying to get access to certain parts of my application through COM, mainly because I need a Delphi application to be able to invoke certain services on the .NET application.

I've managed to expose some methods via COM inheriting from ServicedComponent class and its working fine.

Now I need to be able to raise events to the client so I can alert him when certain things occur. I currently have something like this:

public interface IEvents
{
  void OnMessage();
}

[EventClass]    
[ComVisible(true)]
public class MyEventsClass : ServicedComponent, IEvents
{
  public void OnMessage()
  {
  }
}

but when I import the TypeLibrary from Delphi I get this:

IEvents = interface(IDispatch)
    ['{E7605303-F968-3509-829B-BF70084053C4}']
    procedure OnMessage; safecall;
  end;

IEventsDisp = dispinterface
    ['{E7605303-F968-3509-829B-BF70084053C4}']
    procedure OnMessage; dispid 1610743808;
  end;

which I don't really know how to use. I was expecting an IUnknown interface that I can implement and provide to the service through a MyService.Register(IEvents events)...

I think I'm completely lost here... Any advice or reference about how to properly implement COM+ events with .NET?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生共白头 2024-07-30 22:40:17

我仍然不确定 EventClass 到底有什么用途,因为我发现您不需要使用它。 我只是简单地声明了这一点:

[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IMyEventIface
{
}

并且该接口将作为 IUnknown 接口导出到 Delphi。

然后,您在 ServicedComponent 类上定义一个方法,该方法的连接签名如下:

public void Connect(IMyEventIface eventHandler)
{
  mEvents.Add(eventHandler);
}

这使您可以手动处理每个客户端事件处理程序的调用,但这是我找到的唯一方法。

I'm still unsure of what exactly is the use for EventClass by I've discovered you don't need to use it. I simply have declared this:

[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IMyEventIface
{
}

And with that the interface is exported as an IUnknown interface to Delphi.

Then you define a method on your ServicedComponent class which a connect signature like this:

public void Connect(IMyEventIface eventHandler)
{
  mEvents.Add(eventHandler);
}

which left you to manually handle the invoke of each client event handler but is the only way I've found.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文