How To: Communicate with ArcMap from standalone Visual Basic application
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.
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.
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
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.