English

How To: Communicate with ArcMap from standalone Visual Basic application

Summary

Memory address errors can occur when passing objects from a standalone application (vb.exe) to ArcMap.

This is because the standalone application is in a different address space than ArcMap. Passing objects across this boundary, can lead to unexpected behavior.

To pass an object from a vb.exe to ArcMap, use the IObjectFactory interface. This interface contains members that allow automation clients to create arbitrary objects within the application's process space.

Procedure

Below is a simple example that creates a polygon in a standalone Visual Basic application and uses IObjectFactory to pass the polygon object and the markersymbol object to ArcMap.

  1. Start ArcMap.
  2. Switch to page layout view.
  3. Create a new Visual Basic standard exe application.
  4. Click Project > References and select ESRI Object Library to add a reference to esriCore.olb.
  5. Add a button to the form.
  6. Paste the following code into the button's click event.

    Dim doc As IDocument
    Dim app As IApplication
    Dim pmxdoc As IMxDocument
    Dim pobjectFactory As IObjectFactory
    Dim rot As AppROT
    Dim strName As String

    Set rot = New AppROT
    Set app = rot.Item(0)
    Set pobjectFactory = app
    Set pmxdoc = app.Document
    Dim pmxapp As IMxApplication
    Set pmxapp = app

    Dim pav As IActiveView
    Set pav = pmxdoc.ActiveView

    ''''Here is the ObjectFactory assignment********************
    Dim ppointcoll As IPointCollection
    Dim ppoly As IPolygon
    Set ppoly = pobjectFactory.Create("esricore.polygon")
    Set ppointcoll = ppoly

    Dim pt1 As IPoint
    Set pt1 = New Point
    pt1.X = 1
    pt1.Y = 1

    Dim pt2 As IPoint
    Set pt2 = New Point
    pt2.X = 1
    pt2.Y = 4

    Dim pt3 As IPoint
    Set pt3 = New Point
    pt3.X = 4
    pt3.Y = 4

    Dim pt4 As IPoint
    Set pt4 = New Point
    pt4.X = 4
    pt4.Y = 1

    ppointcoll.AddPoint pt1
    ppointcoll.AddPoint pt2
    ppointcoll.AddPoint pt3
    ppointcoll.AddPoint pt4

    '''Here is the ObjectFactory assignment********************
    Dim psym As ISimpleFillSymbol
    Set psym = pobjectFactory.Create("esricore.SimplefillSymbol")

    Dim pcolor As IRgbColor
    Set pcolor = New RgbColor
    pcolor.Blue = 255
    pcolor.Red = 0
    pcolor.Green = 0

    psym.Color = pcolor

    pav.ScreenDisplay.StartDrawing 0, esriNoScreenCache
    pav.ScreenDisplay.SetSymbol psym
    pav.ScreenDisplay.DrawPolygon ppoly
    pav.ScreenDisplay.FinishDrawing


  7. Save and run the application.

    Note:
    This code sample adds a polygon to the page layout screen display. This polygon is temporary and any refresh will erase the polgyon. Therefore make sure the page layout view is visible before running the code.