Gentee Programming Language > Documentation > Libraries Download documentation

Working with COM Object

The COM library is applied for working with the COM/OLE objects, the IDispatch interface and maintains late binding operations. For using this library, it is required to specify the file olecom.ge (from Lib subfolder) with include command.

This library also contains the support of the VARIANT type, used for data transmitting from/to COM objects.

Variables of the oleobj type are used for working with the COM objects; furthermore, each variable of this type has one appropriate COM object. A COM objects method is called with the help of the ~ late binding operation. There are two ways of binding a COM object with a variable , as follows:
1. The createobj method is used for creating a new COM object:

oleobj excapp
excapp.createobj( "Excel.Application", "" )

2. Binding a variable with the existing COM object (child) is returned by another COM object method call:

oleobj workbooks
workbooks = excapp~WorkBooks

The oleobj object can maintain the following kinds of late binding:

The method call can return only the VARIANT type, and the appropriate assignment operators and type cast operators are used to convert data to basic Gentee types. Parameters of the COM objects methods call as well as the assigned values are automatically converted to the appropriate VARIANT types. The following Gentee types can be used - uint, int, ulong, long, float, double, str, VARIANT.

Use the release method in order to release the COM object; otherwise, the COM object is released when the variable is deleted; also the object is released when the variable is bound with another COM object.
Have a look at the example of using the COM object

include { "olecom.ge"}
func ole_example 
{ oleobj excapp excapp.createobj( "Excel.Application", "" ) excapp.flgs = $FOLEOBJ_INT excapp~Visible = 1 excapp~WorkBooks~Add excapp~Cells( 3, 2 ) = "Hello World!" }

The oleobj object has properties, as follows:

All child objects automatically inherit the flgs property as well as the errfunc property.

VARIANT is a universal type that is used for storing various data and it enables different programs to exchange data properly. This type represents a structure consisted of two main fields: the first field is a type of the stored value, the second field is the stored value or the pointer to a storage area. The VARIANT type is defined as follows:

type VARIANT {
   ushort vt          
   ushort wReserved1     
   ushort wReserved2     
   ushort wReserved3 
   ulong  val
}

vt is a type code of the contained value ( type constants VT_*: $VT_UI4, $VT_I4, $VT_BSTR ... );
val is a field used for storing values
The library provides only some of the operations of the VARIANT type, however, you can use the fields of the given structure
The example illustrates creation of the VARIANT( VT_BOOL ) variable:

VARIANT bool
....
bool.clear()
bool.vt = $VT_BOOL
(&bool.val)->uint = 0xffff// 0xffff - VARIANT_TRUE


There are all possible operations with the VARIANT variable below
 
Type of the left-hand operandOperatorType of the right-hand operandResult type
VARIANT=ulongVARIANT( VT_UI8 )
VARINAT=longVARIANT( VT_I8 )
VARIANT=uintVARIANT( VT_UI4 )
VARIANT=intVARIANT( VT_I4 )
VARIANT=floatVARIANT( VT_R4 )
VARIANT=doubleVARIANT( VT_R8 )
VARIANT=strVARIANT( VT_BSTR )
VARIANT=VARIANTis equal to the type of the right-hand operand
str=VARIANT( VT_BSTR )str
oleobj=VARIANT( VT_DISPATCH )oleobj

There are possible functions used for conversion:
 
Function namePossible converting types of VARIANT
ulongVT_UI8
longVT_I8
uintVT_UI4, VT_UI2, VT_UI1, VT_I4, VT_I2, VT_I1, VT_BOOL
intVT_UI4, VT_UI2, VT_UI1, VT_I4, VT_I2, VT_I1, VT_BOOL
floatVT_R4
doubleVT_R8
strVT_BSTR


This example shows VARIANT operations

uint val
str  res
oleobj ActWorkSheet
VARIANT vval

....
vval = int( 100 )        //VARIANT( VT_I4 ) is being created
excapp~Cells(1,1) = vval //equals excapp~Cells(1,1) = 100
                        
vval = "Test string"     //VARIANT( VT_BSTR ) is being created
excapp~Cells(2,1) = vval //equals excapp~Cells(1,1) = "Test string"

val = uint( excapp~Cells(1,1)~Value ) //VARIANT( VT_I4 ) is converted to uint 
res = excapp~Cells(2,1)~Value         //VARIANT( VT_BSTR ) is converted to str
ActWorkSheet = excapp~ActiveWorkSheet //VARIANT( VT_DISPATCH ) is converted to oleobj

  • Methods
  • VARIANT Methods

    Methods

    oleobj.createobjCreating COM object
    oleobj.getresResult of the last operation
    oleobj.iserrEnables to define whether or not an error occurs while working with a COM object
    oleobj.releaseReleasing the COM object

    VARIANT Methods

    variant.arrcreateCreating the SafeArray array
    variant.arrfromgAssigning a value to an element of the SafeArray array
    variant.arrgetptrObtaining a pointer to an element of the SafeArray array
    variant.clearClearing a variable
    variant.isnullEnables to define whether or not a variable is NULL


     Copyright © 2004-2006 Gentee Inc. All rights reserved.