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.
- Start ArcMap.
- Switch to page layout view.
- Create a new Visual Basic standard exe application.
- Click Project > References and select ESRI Object Library to add a reference to esriCore.olb.
- Add a button to the form.
- 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
- 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.