【发布时间】:2020-04-30 10:00:35
【问题描述】:
我知道一个 10 年前的问题与这个问题的标题相同,但我已经仔细检查过,我没有错误地使用代表姓名。这是一个不同的问题。
在工作中,我们有一个旧的 VB6 应用程序,我需要教授新的(呃)技巧。我要做的第一件事是让它从一个用 C# 编写的 .Net COM 可见 DLL 调用方法。我有那个工作。现在我需要让它处理来自同一个 DLL 的传入进度通知事件。我昨天问了一个类似的问题,其中 VB6 IDE 甚至没有看到 DLL 有事件要提供。通过正确装饰 C# 接口和类解决了这个问题。
首先,C#代码:
namespace NewTricksDLL
{
[ComVisible(true)]
[Guid("16fb3de9-3ffd-4efa-ab9b-0f4117259c75")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ITransfer
{
[DispId(2)]
string SendAnEvent();
}
[ComVisible(true)]
[Guid("16fb3de9-3ffd-4efa-ab9b-0f4117259c74")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IManagedEventsToCOM
{
[DispId(2)]
void NotificationEvent();
}
[ComVisible(true)]
[Guid("dcf177ab-24a7-4145-b7cf-fa06e892ef21")]
[ComSourceInterfaces(typeof(IManagedEventsToCOM))]
[ProgId("ADUTransferCS.NewTricks")]
public class NewTricks : ITransfer
{
public delegate void NotificationEventHandler();
public event NotificationEventHandler NotifificationEvent;
public string SendAnEvent()
{
if (NotifificationEvent != null)
NotifificationEvent();
}
}
}
现在我尝试在 VB6 中使用它。请注意,_tricky_NotificationEvent 事件处理程序是由 IDE 通过从左侧下拉列表中选择 _tricky 和从右侧下拉列表中选择 NotificationEvent 生成的,因此我知道此事件对 VB6 IDE 可见。
Option Explicit
Public WithEvents _tricky As NewTricksDLL.NewTricks
Private Sub Command1_Click()
' The next line fails with 'Object or class does not support the set of events'
Set _tricky = CreateObject("NewTricksDLL.NewTricks")
' Execution never makes to the next line
_tricky.SendAnEvent()
End Sub
Private Sub _tricky_NotificationEvent()
' This handler was auto generated by the IDE
End Sub
【问题讨论】:
-
这能回答你的问题吗? Exposing .NET events to COM?
-
@GSerg 不,因为它看起来像我已经拥有的。它确实包括通过接口公开异步任务,这是我接下来需要做的,所以谢谢你。 :)
-
虽然你错过了
[ClassInterface(ClassInterfaceType.None)],但我不确定它是否重要。 -
似乎没有。我以前试过(然后又试了一次),但没有帮助。
-
给出错误的确切行是什么?
标签: com vb6 com-interop