Top
NetCOBOL V11.0 Getting Started with COM Components
FUJITSU Software

3.4 Creating a COM Client Program

We will now modify the TESTFILEO5 program to be a COM client that will invoke the FILEIO5 COM Server program.

COBOL COM Client programs are not limited to invoking COBOL COM Server programs. Instead they can invoke COM Server programs written in a wide variety of languages such as Visual Basic and Visual C++. The technique shown below will work for COM Server modules written in other languages as well.

The TESTFILEIO5 program has been modified as shown in Figure 3.10 to turn it into a COM Client program.

 Identification Division.
 Program-ID. TESTFILEIO5.
 Environment Division.
 Configuration Section.
 Repository.
     Class FILEIO5 AS "*COM".
 Data Division.
 Working-Storage Section.
 01 FILEIO-PTR Object Reference FILEIO5.
 01 Return_Status Pic 99 Value 0.
 01 Return_Record Pic X(132).
 01 Prog-ID Pic X(15) Value "FILEIO5.FILEIO5".
 Procedure Division.
     Invoke FILEIO5 "CREATE-OBJECT" Using Prog-ID
                                    Returning FILEIO-PTR.
     Invoke FILEIO-PTR "OPEN_FILE" Using Return_Status.
     If Return_Status = 0
         Perform Until Return_Status Not = 0
             Invoke FILEIO-PTR "READ_FILE" Using Return_Status
                                                 Return_Record
             If Return_Status = 0
                 Display Return_Record
             End-If
         End-Perform
         Invoke FILEIO-PTR "CLOSE_FILE" Using Return_Status
     End-If.
     Set FILEIO-PTR To Null.
     Exit Program.

Figure 3.10 The TESTFILEIO5 program modified to be a COM Client program


If you compare the COM Client program TESTFILEIO5 shown in Figure 3.10 to its predecessor (TESTFILEIO4) shown in Figure 2.9, you will notice the following modifications were made:

  1. The Class reference statement in the Repository was changed to:

     Class FILEIO5 AS "*COM".

    The *COM class is a special class provided by NetCOBOL that allows COBOL programs to invoke COM classes. This statement identifies FILEIO5 as a COM class.

  2. The FILEIO-PTR data item’s definition has been changed from an Object Reference to:

     01 FILEIO-PTR Object Reference FILEIO5.

    This identifies this Object Reference as an object reference that will point at a FILEIO5 class instance at runtime.

  3. We’ve added a new data item to the WORKING-STORAGE SECTION:

     01 Prog-ID Pic X(15) Value "FILEIO5.FILEIO5".

    When Regsvr32.Exe registered the FILEIO5 COM Server module, it created an entry in the Windows registry for it entitled “FILEIO5.FILEIO5”. This name was derived from the actual .DLL name (FILEIO5) and the name of the Class we specified for the COM Server (FILEIO5). This name will be used at runtime to create an instance of it.

  4. The first Invoke statement has been changed to:

     Invoke FILEIO5 "CREATE-OBJECT" Using Prog-ID Returning FILEIO-PTR.

    The “CREATE-OBJECT” method was inherited from the FJBASE class. It creates an object at runtime using Late Binding. We will discuss Late Binding vs. Early Binding a bit later in this chapter. This call uses the Prog-ID data item and returns a pointer (object reference) to a FILEIO5 object.

You will notice that all of the remaining procedural code remains the same, and if you build and execute the TESTFILEIO5 program, you will receive the same results as in all of the previous projects we’ve used in this manual.