Select to view content in your preferred language

Hook runnig ArcMap application

3199
4
01-11-2011 09:41 PM
EugeneChuguy
Emerging Contributor
Goodnight.
I can use Marshal.GetActiveObject("Word.Application") to
obtain a reference to Microsoft.Office.Interop.Word.DocumentClass.
Does any ways to obtain running ArcMap application? I create DesktopWindowsApplication and have to manage running ArcMap application. How can i do it?

I created and registred extention "ArcMapClassLibrary6._TryExtension", run ArcMap and try to obtain it

             object o = Marshal.GetActiveObject("ArcMapClassLibrary6._TryExtension");

but have a COMException: Operation unavailable (0x800401E3 (MK_E_UNAVAILABLE))
0 Kudos
4 Replies
NeilClemmons
Honored Contributor
Use the IAppRot interface.  There is a developer sample that deals with automation that you might want to look at as well.
0 Kudos
EugeneChuguy
Emerging Contributor
Thanx a lot!
0 Kudos
RiverTaig1
Deactivated User
I'm having problems using AppRot and I can't find the developer sample mentioned in this thread.  In the code below, I get a fatal error in my stand-alone application which is trying to attach to ArcMap - something about needing to add a reference to Microsoft.CSharp.dll.  I found that .dll and added it, but to no avail. when I added it, it wanted me to upgrade the project to .NET framework 4.0 which wasn't really an option. Thoughts?

ESRI.ArcGIS.Framework.IAppROT appRot = new ESRI.ArcGIS.Framework.AppROTClass();
For (int i = 0; i < appRot.Count; i++)
{
    ESRI.ArcGIS.Framework.IApplication app = (ESRI.ArcGIS.Framework.IApplication)appRot.get_Item(i);
if (app.Document is ESRI.ArcGIS.ArcMapUI.IMxApplication2) //FATAL ERROR HERE
{
   IMxDocument mxDoc = app.Document as IMxDocument;
  ... more code
0 Kudos
MichaelRobb
Honored Contributor
I also use the IApprot interface.


Added showing the Map title being attached to
      
Dim m_appROT As ESRI.ArcGIS.Framework.IAppROT
        m_appROT = New ESRI.ArcGIS.Framework.AppROT

        Dim q As Object
        'Cycle through appROT to work on the latest ArcMap open
        If m_appROT.Count > 0 Then
            For q = 0 To m_appROT.Count - 1
                'If TypeOf m_appROT.Item(i) Is IGxApplication Then ' ArcCatalog running

                If TypeOf m_appROT.Item(q) Is IMxApplication Then ' ArcMap running
                    If m_appROT.Item(q).Visible = True Then
                        If m_appROT.Item(q).Document.Title = lblConnectedTo.Text Then
                            pApp = m_appROT.Item(q) ' The session originally used.
                        End If
                    End If

                End If
            Next q
        End If


Now you can go from there...
Also added a public for several forms
  pObjectFactory = CType(pApp, IObjectFactory)
        pApp = pObjectFactory

        pDoc = pApp.Document
        pApp = pDoc.Parent

        pMxDoc = CType(pDoc, IMxDocument)

        pMap = CType(pMxDoc.FocusMap, IMap) ' Focus on the map

        Me.ArcGISApplication = pApp ' pass this to the public 


A little extra, getting carried away, but now you can just use m_application in any SUB or Function
 
  Public WriteOnly Property ArcGISApplication() As ESRI.ArcGIS.Framework.IApplication

        Set(ByVal value As ESRI.ArcGIS.Framework.IApplication)
            m_application = value
        End Set
    End Property
0 Kudos