Problem With Snippet: "Get MxApplication from ArcMap"

Discussion created by aultonsmith on Mar 12, 2012
Latest reply on Mar 13, 2012 by EBejarano-esristaff
I am working on a custom tool for ArcMap version 9.3.3  in Visual Studio 2008. In order to get a reference to the mxApplication, I used this ArcGis snippet:

#Region "Get MxApplication from ArcMap"

    '''<summary>Get MxApplication from ArcMap</summary>
    '''<param name="application">An IApplication interface that is the ArcMap application.</param>
    '''<returns>An IMxApplication interface.</returns>
    '''<remarks>The IMxApplication interface allows access the AppDisplay object, the selection environment, and the default printer page settings.</remarks>
    Public Function GetMxApplicationFromArcMap(ByVal application As IApplication) As ESRI.ArcGIS.Carto.IMap

        If application Is Nothing Then
            Return Nothing
        End If

        If Not TypeOf application Is ESRI.ArcGIS.ArcMapUI.IMxApplication Then
            Return Nothing
        End If

        Dim mxApplication As ESRI.ArcGIS.ArcMapUI.IMxApplication = CType(application, ESRI.ArcGIS.ArcMapUI.IMxApplication) ' Explicit Cast

        Return mxApplication

    End Function
#End Region

I called the snippet this way:

Dim pApp As IMxApplication = GetMxApplicationFromArcMap(m_application)

(this same syntax worked fine with the ???Get MxDocument from ArcMap??? snippet).

When I run the code in Debug, though, at the end of the snippet code, I get the following error in the Immediate Window:

A first chance exception of type 'System.InvalidCastException' occurred in GoogleEarth_Link.dll

And the code resumes in the next procedure down from it left off (it skips the rest of the procedure the calling line was in). Any help would be appreciated. I have not been able to find a reference to anything else like this in the forum.