Одним из способов будет определение интерфейса COM в .NET:
<System.Runtime.InteropServices.GuidAttribute("0896D946-8A8B-4E7D-9D0D-BB29A52B5D08"), _
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _
Public Interface IEventHandler
Sub OnEvent(ByRef sender As Object, ByRef e As Object)
End Interface
Реализация этого интерфейса в VB6
Implements MyInterop.IEventHandler
Private Sub IEventHandler_OnEvent(ByRef sender As Variant, ByRef e As Variant)
Dim id
id = e.Entity.Id
' As long as e is COM Visible (not necessarily COM registered, this will work)
End Sub
, а затем наличие регистратора в .NET со статическимколлекция IEventHandlers:
<ComClass(ComRegistrar.ClassId, ComRegistrar.InterfaceId, ComRegistrar.EventsId>
Public Class ComRegistrar
Private Shared ReadOnly _eventHandlers As New Dictionary(Of String, List(Of IEventHandler))
' This is called by .NET code to fire events to VB6
Public Shared Sub FireEvent(ByVal eventName As String, ByVal sender As Object, ByVal e As Object)
For Each eventHandler In _eventHandlers(eventName)
eventHandler.OnEvent(sender, e)
Next
End Sub
Public Sub RegisterHandler(ByVal eventName As String, ByVal handler As IEventHandler)
Dim handlers as List(Of IEventHandler)
If Not _eventHandlers.TryGetValue(eventName, handlers)
handlers = New List(Of IEventHandler)
_eventHandlers(eventName) = handlers
End If
handlers.Add(handler)
End Sub
End Class
Ваш код .NET будет вызывать FireEvent, и если VB6 ранее вызывал RegisterHandler, ваш VB6 IEventHandler будет вызываться.