Previous: , Up: Tutorial   [Contents][Index]


5.2 Using a COM Object

  1. Create the binding

    Run BindCOM gnatexample.tlb GNATClient to create a binding to the COM object in the previous tutorial.

  2. Create main procedure

    Set up the basic client main procedure and add error handling to catch COM errors as shown in bold:

    with Ada.Exceptions;
    with GNAT.IO; use GNAT.IO;
    
    procedure Client is
    begin
    
    exception
       when E : others =>
          Put_Line (Ada.Exceptions.Exception_Name (E));
          Put_Line (Ada.Exceptions.Exception_Message (E));
    
    end Client;
    
  3. Initialize COM

    Before using GNATCOM, the COM environment must be initialized. This is done by calling the procedure GNATCOM.Initialize.Initialize_COM at the start of the Client procedure.

    procedure Client is
    begin
    
       GNATCOM.Initialize.Initialize_COM;
    
    
  4. Declare an Interface Type

    Add code to with in the bindings for the interfaces that will be used and declare objects of those types.

    
    with GNATClient.IGNATMessage_Interface; use GNATClient.IGNATMessage_Interface;
    with GNATClient.IGNATStat_Interface; use GNATClient.IGNATStat_Interface;
    
    procedure Client is
    
       Messages : IGNATMessage_Type;
       Stats    : IGNATStat_Type;
    
    
  5. Create Object

    Create the COM object by calling Create on one of the interfaces supported by the object.

       Create (Messages, GNATClient.CLSID_GNATCOMClass);
    
  6. Use object

    Once the object has been created methods can be called on the interface used to create the object, or a Query can be performed to access other interfaces of the object.

       Beep (Messages);
    
       MessageBox (Messages, GNATCOM.BSTR.To_BSTR ("Hello World!"));
    
       Query (Stats, Messages);
    
       Put_Line ("IGNATMessage methods called" &
                 Integer (Calls (Stats))'Img & " times.");
    
  7. Compile and run

    Compile the client code using gnatmake client and then run it. To use the object on a remote machine, first run the beep-remote.exe on the remote machine with the objects host machine name and the run the client on that machine. You may have to configure permissions on the host machine using the utility dcomcnfg.exe.

    client.adb :

    with Ada.Exceptions;
    with GNAT.IO; use GNAT.IO;
    
    with GNATCOM.Initialize; use GNATCOM.Initialize;
    with GNATCOM.BSTR;
    
    with GNATClient.IGNATMessage_Interface; use GNATClient.IGNATMessage_Interface;
    with GNATClient.IGNATStat_Interface; use GNATClient.IGNATStat_Interface;
    
    procedure Client is
       Messages : IGNATMessage_Type;
       Stats    : IGNATStat_Type;
    begin
       GNATCOM.Initialize.Initialize_COM;
    
       Create (Messages, GNATClient.CLSID_GNATCOMClass);
    
       Beep (Messages);
    
       MessageBox (Messages, GNATCOM.BSTR.To_BSTR ("Hello World!"));
    
       Query (Stats, Messages);
    
       Put_Line ("IGNATMessage methods called" &
                 Integer (Calls (Stats))'Img & " times.");
    exception
       when E : others =>
          Put_Line (Ada.Exceptions.Exception_Name (E));
          Put_Line (Ada.Exceptions.Exception_Message (E));
    end Client;
    

Previous: , Up: Tutorial   [Contents][Index]