Select to view content in your preferred language

Problem With Snippet: "Get MxApplication from ArcMap"

2263
3
03-12-2012 06:47 AM
AultonSmith
Deactivated User
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.
0 Kudos
3 Replies
DuncanHornby
MVP Notable Contributor
Aulton,

Firstly when you post code can you place it inside the
 tags, use the # button in the editor bar to do this. It makes your code easier to read.

I think your problem is this line:

PublicFunction GetMxApplicationFromArcMap(ByVal application As IApplication) As ESRI.ArcGIS.Carto.IMap


Look at what your function is returning, an IMap object you want to return an IMXApplication object.

Duncan
0 Kudos
AultonSmith
Deactivated User
Duncan,
You nailed it! I doubled checked the snippet, and that is an error supplied by esri, someone should notify them...
Thanks for the tip on how to post code, too. This is only my 2nd time, the 1st being 2 or 3 years ago, it being much simpler then :).
Thanks a bunch!
Aulton Smith
0 Kudos
EdgarBejarano
Deactivated User

... that is an error supplied by esri, someone should notify them...


Hello Aulton,

I have submitted the following bug report to address the incorrect code implementation in the "Get MxApplication from ArcMap" code snippet:

Bug Report ID:  NIM079042
Bug Report Synopsis:
The "Get MxApplication from ArcMap" snippet performs an invalid type cast to IMap and also incorrectly returns an IMap reference instead of IMxApplication, in both VB.NET and C#.

The issue has been reported for both the code snippet that adds with the ArcGIS Snippet Finder in Visual Studio as well as with the code snippet in the ArcGIS Resource Center (online):
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#//00490000002n000000
0 Kudos