Communicating with a COM host through a VstaIntegration library

When using a VstaIntegration library with some COM hosts, such as the MyAppVB6 sample, there are two ways to communicate: 
1)  Through public methods and properties. 
2)  Through public events. 

Some instances where raising a public event from the VstaIntegration library and catching it in the COM host is appropriate include the IExternalDebugHost methods in the VstaRuntimeIntegration class:  OnBeforeDebugStarting, OnDebugStart, and OnDebugStop methods.   

In the typical .Net integration it is easy to simply add code to these methods (which are called by the VSTA design time during debugging events) to allow the host to do any necessary actions.  However, for some integrations where the host application is unknown to the VstaIntegration library, raising an event to alert the host is the way to go.  

The code below demonstrates this with the OnDebugStop method. 
The basic steps to do this are:

1) In the VstaIntegration library the VstaDesignTimeIntegarion class exposes a public event OnDebugStopping.
2) The host application monitors this event. In this case the host is a VB6 host so it also declares the variable for this class as "with events" and hooks into the event by using the naming convention (variableName_eventName) for the method to react to the event.
3) The event is raised in the VstaIntegaration code.

 

'1)  expose a public event
Public Event OnDebugStoppingEvent()

''' <summary>
''' Called when debugging is stoped.
''' </summary>
Public Sub OnDebugStopping() Implements Microsoft.VisualStudio.Tools.Applications.DesignTime.IExternalDebugHost.OnDebugStopping

    If (Not Me.mIsDebugging Or Me.mIsShuttingDown) Then
        Return
    End If

    '3)  raise the OnDebugStoppingEvent
    RaiseEvent OnDebugStoppingEvent()

    Me.UnloadCurrentMacroAddin() 

    ' Do not reload the macro if a new macro is being recorded.
    If Me.mReloadInProc Then
        Me.LoadMacros(Nothing)
    End If

    Me.mReloadInProc = True
    Me.mIsDebugging = False

End Sub

 '2 for VB6) declare with events to catch debugger stopping events
Public WithEvents IdeIntegration As VSTA2_Integration.VSTADesignTimeIntegration 

'2) monitor and catch the event by using the naming convention variableName_EventName for the method name.
Sub IdeIntegration_OnDebugStoppingEvent ()

    MsgBox("do clean up here")

End Sub

 


Posted Apr 22 2010, 09:47 AM by Melody
Copyright Summit Software Company, 2008. All rights reserved.