OLE Automation does not support structures. The Active Expert converts structures defined in the Contract Repository to objects known as pseudo-objects, making them available to OLE Automation controllers. Structures in the Contract Repository are defined globally, independent of modules. A structure can be used as the type of a parameter for an operation by first defining an element, which has the structure as its base type. The element in turn becomes the type of the parameter. The Active Expert does not expose the element level of the Contract Repository; instead, a pseudo-object representing a structure has the same name as the original structure in the Contract Repository.
To use a structure from OLE Automation, you can access each member of the structure as an OLE Automation property.
Listing 4-15 Example: Definition for the Customer Structure and Interface
struct Customer
{
string name;
string billingAddress;
double credit;
};
interface Customers
{
Customer FirstCustomer();
void AddCustomer(in Customer newOne);
};
Visual Basic uses the Customer interface as follows.
Listing 4-16 Example: Customer Interface in Visual Basic Syntax
Dim cust as _Customer
set cust = clients.FirstCustomer()
print "Name: " & cust.name
print "Address: " & cust.billingAddress
print "Balance: " & str$(cust.credit)
After accessing an instance of a structure pseudo-object, the structure behaves in the same manner as any other object. Assigning another variable to the structure means both variables refer to the same structure. To make a copy of the structure, you can use the INSTANCE_Clone()
method:
Listing 4-17 Using INSTANCE_Clone() to Copy a Structure
dim custRef As _Customer
dim custCopy As _Customer
set custRef = cust
'cust and custRef now refer to the same
'structure
set custCopy = cust.INSTANCE_Clone
'copy the structure so that custCopy and
'cust are different objects.
You can create an empty structure to pass to a method by using the CORBA.Factory
which is a built-in object of the Active Expert. The CreateType
method on the CORBA.Factory
is used to create an empty instance of the structure.
Listing 4-18 Using CreateType() to Create an Empty Instance of a Structure
Sub AddCustomer(clients as Customers, name as string, addr as string)
dim dir as object
dim newOne as _Customer
set dir = CreateObject("CORBA.Factory ")
'Create empty customer structure
set newOne = dir.CreateType
(clients, "Customer")
'Fill in customer structure
newOne.name = name
newOne.billingAddress = addr
newOne.credit = OpeningCharge
'Pass it to AddCustomer
clients.AddCustomer newOne
End Sub
The first parameter of CreateType
is the OLE Automation View of the interface where the structure is used. If the Automation View is an OCX, the Object property of the OCX should be passed in instead of the OCX itself. For example, if the OCX is called MyOCX
, CreateType
should be passed MyOCX.Object
as the first parameter. The second parameter of CreateType
is the name of the structure as defined in the Contract Repository.