Previous IDL Connectivity Bridges: Using COM Objects in IDL Next

Example: RSIDemoComponent

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.

  1. Begin by creating an IDLcomIDispatch object from the COM object. You can use either the class ID or the program ID. Remember that if you use the class ID, you must remove the braces ( { } ) and replace the hyphens with underscores.
  2. obj1 = OBJ_NEW( $  
       'IDLCOMIDispatch$PROGID$RSIDemoComponent_RSIDemoObj1')  
    

     

    or (with Class ID):

     

    obj1 = OBJ_NEW( $   
       'IDLCOMIDispatch$CLSID$A77BC2B2_88EC_4D2A_B2B3_F556ACB52E52')  
    

     

  3. The COM object implements the 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}'.
  4. strCLSID = obj1->GetCLSID()  
    PRINT, strCLSID  
    

     


    Note
    The GetCLSID method returns the class identifier of the object using the standard COM separators ( - ).

     

  5. The COM object has a property named MessageStr. To retrieve the value of the MessageStr property, enter:
  6. obj1 -> GetProperty, MessageStr = outStr  
    PRINT, outStr  
    

     

    IDL should print 'RSIDemoObj1'.

     

  7. You can also set the 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:
  8. obj1 -> SetProperty, MessageStr = 'Hello, world'  
    obj1 -> DisplayMessageStr  
    

     

  9. The 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'.
  10. instr = 'The value is: '  
    val = 25L  
    outStr = obj1->Msg2InParams(instr, val)  
    PRINT, outStr  
    

     

  11. To view all known information about the IDLcomIDispatch object, including its dynamic subclass name and the names of its methods, use the IDL HELP command with the OBJECTS keyword:
  12. HELP, obj1, /OBJECTS  
    

     

  13. The 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)  
      

     

  14. All three objects have the 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}'.
  15. obj3CLSID = obj3->GetCLSID()  
    PRINT, obj3CLSID  
    

     

  16. Remember to destroy a retrieved object when you are finished with it:
  17. OBJ_DESTROY, obj3  
    

     

  18. Next, use the COM object's 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.
  19. objs = obj1->GetArrayOfObjects(cItems)  
    PRINT, cItems  
    

     

  20. Since each object implements the GetCLSID method, you could loop through all the object references and get its class ID:
  21. FOR i = 0, cItems-1 do begin  
       objCLSID = objs[i] -> GetCLSID()  
       PRINT, 'Object[',i,'] CLSID: ', objCLSID  
    ENDFOR  
    

     

  22. Remember to destroy object references when you are finished with them:
  23. OBJ_DESTROY, objs  
    OBJ_DESTROY, obj1  
    

  IDL Online Help (March 06, 2007)