C#到VB6 COM事件(“;对象或类不支持事件集”;),但不同
我知道一个10年前的问题与这个问题同名,但我已经仔细检查了一遍,我没有错误地使用代表姓名。这是另一个问题 在这里工作,我们有一个旧的VB6应用程序,我需要教新的(er)技巧。我要做的第一件事是让它从用C#编写的.Net COM可见DLL调用方法。我有工作。现在我需要让它处理来自同一DLL的传入进度通知事件。昨天我问了一个类似的问题,其中VB6IDE甚至没有看到DLL提供了事件。通过正确地装饰C#接口和类,这个问题得到了解决 首先,C#codez: 现在我尝试在VB6中使用它。请注意,“刁钻”NotificationEvent事件处理程序是由IDE通过从左侧下拉菜单中选择“刁钻”和从右侧下拉菜单中选择“NotificationEvent”生成的,因此我知道VB6 IDE可以看到此事件C#到VB6 COM事件(“;对象或类不支持事件集”;),但不同,com,vb6,com-interop,Com,Vb6,Com Interop,我知道一个10年前的问题与这个问题同名,但我已经仔细检查了一遍,我没有错误地使用代表姓名。这是另一个问题 在这里工作,我们有一个旧的VB6应用程序,我需要教新的(er)技巧。我要做的第一件事是让它从用C#编写的.Net COM可见DLL调用方法。我有工作。现在我需要让它处理来自同一DLL的传入进度通知事件。昨天我问了一个类似的问题,其中VB6IDE甚至没有看到DLL提供了事件。通过正确地装饰C#接口和类,这个问题得到了解决 首先,C#codez: 现在我尝试在VB6中使用它。请注意,“刁钻”No
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
如果使用
CreateObject
,它将创建一个类型为object的对象。只需使用New
,即可创建对象:
Set _tricky = New NewTricksDLL.NewTricks
由于您已将变量声明为WithEvents,因此不能仅使用
将其声明为New NewTricksDLL.NewTricks
,我想您可能已经尝试过了。希望这能有所帮助-下面是一个基本的VB.net实现,满足您的需求:COM接口可由VB6使用,一个事件,一个方法
Option Explicit On
Option Strict On
<ComClass(newTricks.ClassId, newTricks.InterfaceId, newTricks.EventsId)>
Public Class newTricks
#Region "COM GUIDs"
' These GUIDs provide the COM identity for this class
' and its COM interfaces. If you change them, existing
' clients will no longer be able to access the class.
Public Const ClassId As String = "386d540d-f8b8-46e1-939d-7b69dd5eff0a"
Public Const InterfaceId As String = "78b4036e-86a0-4671-997d-da5a33bf026f"
Public Const EventsId As String = "7b0fa5b5-b45e-4db2-9282-c06e09852161"
#End Region
' A creatable COM class must have a Public Sub New()
' with no parameters, otherwise, the class will not be
' registered in the COM registry and cannot be created
' via CreateObject.
Public Sub New()
MyBase.New()
End Sub
Public Event NotificationEvent()
Public Sub SendAnEvent()
RaiseEvent NotificationEvent()
End Sub
End Class
下面是VB.net代码对应的C#代码,由
我没有试过这个C代码。这能回答你的问题吗@没有,因为它看起来像我已经拥有的。它确实包括通过接口公开异步任务,这是我下一步需要做的事情,因此非常感谢。:)虽然您缺少了
[ClassInterface(ClassInterfaceType.None)]
,但我不确定这是否重要。它似乎不重要。我以前试过(现在又试了一次),但没有用。到底是哪一行给了你错误?这绝对值得一试,但我不确定这是问题所在。由于\u
是用正确的类型定义的,因此我认为应该正确使用CreateObject()
的结果。也就是说,除非它确实不返回预期类型的对象。New
和CreateObject
都会导致“对象或类不支持事件集”
。同样,ObjectBrowser会显示该程序集中的事件。
Option Explicit On
Option Strict On
<ComClass(newTricks.ClassId, newTricks.InterfaceId, newTricks.EventsId)>
Public Class newTricks
#Region "COM GUIDs"
' These GUIDs provide the COM identity for this class
' and its COM interfaces. If you change them, existing
' clients will no longer be able to access the class.
Public Const ClassId As String = "386d540d-f8b8-46e1-939d-7b69dd5eff0a"
Public Const InterfaceId As String = "78b4036e-86a0-4671-997d-da5a33bf026f"
Public Const EventsId As String = "7b0fa5b5-b45e-4db2-9282-c06e09852161"
#End Region
' A creatable COM class must have a Public Sub New()
' with no parameters, otherwise, the class will not be
' registered in the COM registry and cannot be created
' via CreateObject.
Public Sub New()
MyBase.New()
End Sub
Public Event NotificationEvent()
Public Sub SendAnEvent()
RaiseEvent NotificationEvent()
End Sub
End Class
Option Explicit
Private WithEvents oTricks As NewTricksDll.newTricks
Private Sub Command1_Click()
Set oTricks = New NewTricksDll.newTricks
'Set oTricks = CreateObject("NewTricksDll.newTricks")
oTricks.SendAnEvent
End Sub
Private Sub oTricks_NotificationEvent()
Debug.Print "Event fired"
End Sub
using Microsoft.VisualBasic;
[ComClass(newTricks.ClassId, newTricks.InterfaceId, newTricks.EventsId)]
public class newTricks
{
// These GUIDs provide the COM identity for this class
// and its COM interfaces. If you change them, existing
// clients will no longer be able to access the class.
public const string ClassId = "386d540d-f8b8-46e1-939d-7b69dd5eff0a";
public const string InterfaceId = "78b4036e-86a0-4671-997d-da5a33bf026f";
public const string EventsId = "7b0fa5b5-b45e-4db2-9282-c06e09852161";
// A creatable COM class must have a Public Sub New()
// with no parameters, otherwise, the class will not be
// registered in the COM registry and cannot be created
// via CreateObject.
public newTricks() : base()
{
}
public event NotificationEventEventHandler NotificationEvent;
public delegate void NotificationEventEventHandler();
public void SendAnEvent()
{
NotificationEvent?.Invoke();
}
}