English

How To: Pass an object from an ActiveX DLL to VBA

Summary

When creating an ActiveX DLL there may be a desire to pass an object from the DLL to ArcMap. Instructions provided demonstrate how to pass a Geometry object.

Procedure

The following steps outline how to pass an object, in this case a geometry object, from a DLL to ArcMap. This article assumes previous experience creating custom Tools and Commands.

  1. Create a Visual Basic ActiveX DLL project that implements ICommand. To do this, click on the article in Related Information below called "Implement ICommand in Visual Basic". When following the procedure, include the following:

    - Name the class 'clsGeom'.
    - Name the project 'AoDemo'.
    - In Project Properties, enter 'ArcObjects Geometry' as the Project Description.

    - Save the class as clsGeom.cls
    - Save the project as AoDemo_clsGeom.vbp.
    - Compile the DLL as AoDemo_clsGeom.dll
  2. In the Visual Basic ActiveX DLL project, declare a module-level object variable to hold a reference to IGeometry.

    Code:
    Private m_pGeom As IGeometry

  3. Create a geometry object in OnClick and assign that geometry object to m_pGeom.

    Code:
    Private Sub ICommand_OnClick()

    Dim pPt As IPoint
    Set pPt = New Point
    pPt.X = 15
    pPt.Y = 25
    Set m_pGeom = pPt

    End Sub

  4. Create a Public Property that returns a Geometry.

    Code:
    Public Property Get Geometry() As IGeometry
    Set Geometry = m_pGeom
    End Property

  5. Compile the DLL as AoDemo_clsGeom.dll.

    A. Click the File Menu and select Make DLL.
    B. From the Project Menu, select Properties.
    C. Select the Component Tab.
    D. Click Binary Compatibility.
    E. Click the File Menu and select Make DLL.

  6. Add the custom command to ArcMap.

    A. If it is not already running, start the ArcGIS application (ArcMap, ArcCatalog, or ArcScene) to which you want to add your custom command.
    B. Click the Tools menu and select Customize.
    C. Select the Commands Tab.
    D. In the lower left corner of the dialog, use the pulldown to select where to save your command.
    E. Click 'Add from file'.
    F. Navigate to the DLL that contains the custom command.
    G. Select the file and click Open.
    H. When the 'Added Objects' dialog box appears, it reports the new objects that have been registered with ArcMap, ArcCatalog, or ArcScene. Click OK.
    I. Click and drag the command from the Commands list and drop it onto a toolbar. Note that the Category you assigned appears in the left-hand panel.
    J. Click Close.


  7. Open the VB Editor.

    In ArcMap, select Tools > Macros > Visual Basic Editor.

  8. Copy the code below into the Project's ThisDocument code module.

    Code:
    Public Sub GetGeometry()
    Dim pApp As IApplication
    Dim pDoc As IDocument
    Dim pGeometry As IGeometry
    Dim pCmdItem As ICommandItem
    Dim pCmd As ICommand

    Dim pClsSelection As AoDemo.clsGeom
    Dim pID As New UID
    pID = "AoDemo.clsGeom"
    Set pApp = Application
    Set pDoc = pApp.Document
    Set pCmdItem = pDoc.CommandBars.Find(pID, True, False)
    Set pCmd = pCmdItem.Command
    Set pClsSelection = pCmd
    Set pGeometry = pClsSelection.Geometry

    If Not pGeometry Is Nothing Then
    If Not pGeometry.IsEmpty Then
    MsgBox "X: " & pGeometry.Envelope.LowerLeft.X
    MsgBox "Y: " & pGeometry.Envelope.LowerLeft.Y
    Else
    MsgBox "Geometry Is Empty"
    End If
    Else
    MsgBox "Geometry is Nothing"
    End If

    End Sub

  9. Click Tools and select References.
  10. Scroll to 'ArcObjects Geometry' that was assigned to the Project Description in Step 1 and enable it.
  11. In ArcMap, click the command to initialize the Geometry.
  12. In ArcMap, run the macro GetGeometry.

    In ArcMap, click Tools > Macros > Macros.


    To demonstrate that the geometry object has been passed from the DLL to ArcMap, MsgBoxes displays the X and Y coordinates of the point created in the DLL.

Related Information