I have a .NET interface
<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
in an exported type library.
The VB6 code references this tlb and implements this interface.
The VB6 code creates an instance of it's implementation and passes it to .NET.
.NET calls OnEvent.
VB6 picks it up the event fine...but the sender and e argument values are strings, not objects when it gets there... The string values are the full names of the types...
The VB6 code:
Implements Interop.IEventHandler
Private Sub IEventHandler_OnEvent(ByRef sender As Variant, ByRef e As Variant)
Dim id
id = e.Person.Id
' The weird thing here:
' e = "XYZ.Tasks.PersonTaskEventArgs"
' sender = "XYZ.Tasks.PersonUIManager"
' The values of the arguments are the NAMEs of the actual object values' types...
End Sub
The code that fires the event is fairly trivial. I have a COM class with a dictionary that registers handlers and fires events.
<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
The .NET code looks like
Public Class PersonEventArgs
Inherits System.EventArgs
' Some properties
End Class
Public Class MyControl
Inherits UserControl
' Stuff
End Class
ComRegistrar.FireEvent("PersonSelected", Me, New PersonEventArgs With { Some stuff })
If I wire up the same code using a .NET class that implements IEventHandler, the arguments come through without a problem.
UPDATE: If I change my ByRef parameters for OnEvent to ByVal, it makes no difference. I'm sure the two types I'm trying to pass are from an assembly marked as ComVisible
.
What's going wrong here?