VB.NET event handling in VB6 code

I have VB6 code that creates an instance of a class that handles events that arise from a VB.NET component. VB6 is pretty simple:

private m_eventHandler as new Collection

...

public sub InitSomething()
  dim handler as EventHandler

  set handler = new EventHandler
  m_eventHandler.Add handler
  ...
  m_engine.Start

end sub 

Note that the event handler object must go beyond the init method (therefore, it is stored in the collection). Also note that m_engine.Startindicates the point in the program where the VB.NET component will start raising events.

Actual event handler (on request):

Private WithEvents m_SomeClass As SomeClass
Private m_object as Object
...

Private Sub m_SomeClass_SomeEvent(obj As Variant)
    Set obj = m_object
End Sub

Note what is m_objectinitialized when the instance is created EventHandler.

The VB.NET code that raises the event is even simpler:

Public ReadOnly Property SomeProp() As Object
    Get
        Dim obj As Object
        obj = Nothing
        RaiseEvent SomeEvent(obj)
        SomeProp = obj
    End Get
End Property

, debug VB6, InitSomething ( VB6 ). InitSomething .

, , . , , .

, VB.NET VB6 Visual Studio ( ).

+3
2

, .Net Components for Consumption VB6 ( COM), .

COM, VStudio, , .

.

Imports System.Runtime.InteropServices
Imports System.ComponentModel

<InterfaceType(ComInterfaceType.InterfaceIsDual), Guid(ClientAction.InterfaceId)> Public Interface IClientAction
        <DispId(1), Description("Make the system raise the event")> sub SendMessage(ByVal theMessage As String)
    End Interface


    <InterfaceType(ComInterfaceType.InterfaceIsIDispatch), Guid(ClientAction.EventsId)> Public Interface IClientActionEvents
        <DispId(1)> Sub TestEvent(ByVal sender As Object, ByVal e As PacketArrivedEventArgs)
    End Interface



    <ComSourceInterfaces(GetType(IClientActionEvents)), Guid(ClientAction.ClassId), ClassInterface(ClassInterfaceType.None)> _
    Public Class ClientAction
        Implements IClientAction

        Public Delegate Sub TestEventDelegate(ByVal sender As Object, ByVal e As PacketArrivedEventArgs)

        Public Event TestEvent As TestEventDelegate

    public sub New()
        //Init etc
    end sub

    public sub SendMessage(theMessage as string) implements IClientAction.SendMessage
        onSendMessage(theMessage)
    end sub 

        Protected Sub onSendMessage(message as string)
            If mRaiseEvents Then
                RaiseEvent TestEvent(Me, New PacketArrivedEventArgs(theMessage))
            End If
        End Sub

    end Class

COM .Net / .

, .

+5

- - " .."

private m_eventHandler as Collection

public sub InitSomething()
  dim handler as EventHandler

  set handler = new EventHandler

  If m_eventHandler Is Nothing Then
    Set m_eventHandler = New Collection
  End if

  m_eventHandler.Add handler
 ...

  m_engine.Start

end sub

, , , , , , .NET VBA.Collection(MS VB6 , ), .NET-, .

0

Source: https://habr.com/ru/post/1708142/


All Articles