Developing COM Applications with Oaa2JavaNet.dll

Overview
Registering for COM interoperability
Importing the COM library
Differences in the COM API
Examples


Overview

This document describes the proper way to develop COM applications using Oaa2JavaNet.dll and the dotnetproxy API, accessing .NET through a COM interoperability wrapper. Before reading this document, read Developing .NET Applications with Oaa2JavaNet.dll.

COM interoperability is provided so legacy COM code can leverage OAA code. However, it is recommended that the COM code be ported to .NET when possible. Note that in order to use Oaa2JavaNet.dll from a COM application, .NET must still be installed.

Back To Top

Registering for COM interoperability

In order to use Oaa2JavaNet.dll from COM applications, the dll must first be registered for COM interoperability. This is automatically done when the .NET library is built using ant, as described in building-dotnet.html. To manually register the dll, use Microsoft's regasm.exe and gacutil.exe utilities. See the Microsoft documentation for more details.

Back To Top

Importing the COM library

The COM library can be imported from the file "lib/win32/Oaa2JavaNet.tlb". For example, to import into visual basic 6.0, choose the menu item "Project->References". Then browse and open the file "Oaa2JavaNet.tlb". A reference will appear named ".NET bridge for OAA2". Add this reference to the project.

Back To Top

Differences in the COM API

The API is the same from COM with a few differences. The first difference is that events are exposed by the COM object "Oaa2JavaNet.LibOaaEvents". This object will expose the "DoOAAEvent" event. For example, in a vb application:

    Public WithEvents myOaaEvents As Oaa2JavaNet.LibOaaEvents

    Private Sub myOaaEvents_DoOAAEvent(ByVal goalTerm As Oaa2JavaNet.IclTerm, ByVal params As Oaa2JavaNet.IclList, ByVal solutions As Oaa2JavaNet.IclList)
        MsgBox ("Got Event: " & goalTerm.ToIdentifyingString() & ", " & params.ToIdentifyingString() & ", " & solutions.ToIdentifyingString())
    End Sub

There also exists a factory for creating objects. This factory exists in order to support object creation for objects that do not have default constructors. Here is an initOaa example, taken from the vb AddAgent example program:


Public WithEvents myOaaEvents As Oaa2JavaNet.LibOaaEvents
Dim comFactory As Oaa2COMFactory
Dim comFactoryImpl As Oaa2COMFactoryImpl
Dim oaaUtil As Oaa2JavaNet.ILibOaaUtil
Const AGENT_NAME = "vb_addAgent"
Const CONNECTION_ID = "parent"

Private Sub initOaa()
    Set oaaUtil = New LibOaaUtil

    ' To set the connect address and port of the dotnetproxyagent, uncomment these lines:
    ' These lines must be the first lines of the application, executed before any
    ' .NET objects are created.
    ' oaaUtil.setConnectAddress ("128.18.233.111")
    ' oaaUtil.setConnectPort (4080)

    Set comFactoryImpl = New Oaa2COMFactoryImpl
    Set comFactory = comFactoryImpl
    Set myOaaEvents = New Oaa2JavaNet.LibOaaEvents
    Dim ievents As Oaa2JavaNet.ILibOaaEvents
    Set ievents = myOaaEvents

    Dim protocol As Oaa2JavaNet.LibComTcpProtocol
    Set protocol = New Oaa2JavaNet.LibComTcpProtocol
    Dim libCom As Oaa2JavaNet.libCom
    Dim strArray() As String

    ReDim strArray(1)
    strArray(0) = "-oaa_listen"
    strArray(1) = "tcp('localhost',4020)"

    Set libCom = comFactoryImpl.newLibCom(protocol, strArray)

    Set myOaa = comFactory.newLibOaa(libCom)
    If Not myOaa.oaaSetupCommunication(AGENT_NAME) Then
        MsgBox ("Error: Failed to setup OAA")
    End If

    Dim solveables As Oaa2JavaNet.IclTerm
    Set solveables = oaaUtil.fromString("[add(Num1, Num2, Sum)]")

    myOaa.oaaRegister CONNECTION_ID, AGENT_NAME, solveables, New Oaa2JavaNet.IclList
    myOaa.oaaReady True

    ievents.setLibOaa myOaa
    ievents.oaaRegisterCallback "app_do_event"
End Sub
Notice that a util class also exists, named "LibOaaUtil". This utilitity class allows the location of the dotnetproxy to be changed. As with the .NET API, the location of the proxy must be changed before any objects from the library are created or accessed. LibOaaUtil also defines the "fromString" method for convenience, used above to convert a string to an IclTerm. Also note above that the variable "ievents" is set to the events object, casted as an interface. At the end of the sample code, the "setLibOaa" method is used to set the LibOaa instance the events object will attach itself to.

Back To Top

Examples

In the OAA distribution, "src/samples_vb" contains VB 6.0 examples that use the dotnetproxy through COM. The most simple of these is the AddAgent, which adds two integers together. Also note that the example program "TestAgent" is a port of the Java TestAgent to vb (using COM). This agent is used for unit testing and performance testing.

Back To Top