|
IDL Connectivity Bridges: Using COM Objects in IDL |
|
This example uses a COM component included in the IDL distribution. The RSIDemoComponent is included purely for demonstration purposes, and does not perform any useful work beyond illustrating how IDLcomIDispatch objects are created and used.
The RSIDemoComponent is contained in a file named RSIDemoComponent.dll located in the examples\doc\bridges\COM subdirectory of the IDL distribution. Before attempting to execute this example, make sure the component is registered on your system as described in Registering COM Components on a Windows Machine.
There are three objects defined by the RSIDemoComponent. The example begins by using RSIDemoObj1, which has the program ID:
RSIDemoComponent.RSIDemoObj1
and the class ID:
{A77BC2B2-88EC-4D2A-B2B3-F556ACB52E52}
| Example Code The following section develops an IDL procedure called IDispatchDemo that illustrates use of the RSIDemoComponent. The complete .pro file is included in the examples\doc\bridges\COM subdirectory of the IDL distribution as IDispatchDemo.pro. |
obj1 = OBJ_NEW( $ 'IDLCOMIDispatch$PROGID$RSIDemoComponent_RSIDemoObj1')
or (with Class ID):
obj1 = OBJ_NEW( $ 'IDLCOMIDispatch$CLSID$A77BC2B2_88EC_4D2A_B2B3_F556ACB52E52')
GetCLSID method, which returns the class ID for the component. You can retrieve this value in and IDL variable and print it. The string should be '{A77BC2B2-88EC-4D2A-B2B3-F556ACB52E52}'.strCLSID = obj1->GetCLSID() PRINT, strCLSID
| Note The GetCLSID method returns the class identifier of the object using the standard COM separators ( - ). |
MessageStr. To retrieve the value of the MessageStr property, enter:obj1 -> GetProperty, MessageStr = outStr PRINT, outStr
IDL should print 'RSIDemoObj1'.
MessageStr property of the object and display it using the object's DisplayMessageStr method, which displays the value of the MessageStr property in a Windows dialog:obj1 -> SetProperty, MessageStr = 'Hello, world' obj1 -> DisplayMessageStr
Msg2InParams method takes two input parameters and concatenates them into a single string. Executing the following commands should cause IDL to print 'The value is: 25'.instr = 'The value is: ' val = 25L outStr = obj1->Msg2InParams(instr, val) PRINT, outStr
HELP, obj1, /OBJECTS
GetIndexObject() method returns an object reference to one of the following three possible objects: RSIDemoObj1 (index = 1)
RSIDemoObj2 (index = 2)
RSIDemoObj3 (index = 3)
| Note If the index is not 1, 2, or 3, the GetIndexObject method will return an error. |
To get a reference to RSIDemoObj3, use the following command:
obj3 = obj1->GetIndexObject(3)
GetCLSID method. You can use this method to verify that the desired object was returned. The output of the following commands should be '{13AB135D-A361-4A14-B165-785B03AB5023}'.obj3CLSID = obj3->GetCLSID() PRINT, obj3CLSID
OBJ_DESTROY, obj3
GetArrayOfObjects() method to return a vector of object references to RSIDemoObj1, RSIDemoObj2, and RSIDemoObj3, respectively. The number of elements in the vector is returned in the first parameter; the result should 3.objs = obj1->GetArrayOfObjects(cItems) PRINT, cItems
GetCLSID method, you could loop through all the object references and get its class ID:FOR i = 0, cItems-1 do begin objCLSID = objs[i] -> GetCLSID() PRINT, 'Object[',i,'] CLSID: ', objCLSID ENDFOR
OBJ_DESTROY, objs OBJ_DESTROY, obj1
IDL Online Help (March 06, 2007)