我知道有一个与这个问题标题相同的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
[ClassInterface(ClassInterfaceType.None)]
,但我不确定它是否重要。 - GSerg