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

Chapter 4 Using COBOL COM Servers with Visual Basic

Now that we have the FILEIO5 COM Server module created and registered we would like to use it from Visual Basic.

Unfortunately, once we step outside the COBOL world, we will find that under Visual Basic, we will receive errors if we try to use the FILEIO5 COM Server Program. We need to make some modifications and these are shown in the updated FILEIO5 program in Figure 4.1.

 Identification Division.
 Class-ID. FILEIO5 Inherits FJBASE.
 Environment Division.
 Configuration Section.
 Repository.
     Class FJBASE.

 Object.
 Environment Division.
 Input-Output Section.
 File-Control.
     Select Infile Assign To "c:\comdoc\test.txt"
                   Organization is Line Sequential
                   File Status is Infile-Status.
 Data Division.
 File Section.
 FD Infile.
 01 Infile-Record Pic X(132).
 Working-Storage Section.
 01 Infile-Status Pic 99.
 01 File-Opened-Flag Pic 9 Value 0.
 Procedure Division.

 Method-ID. OPEN_FILE.
 Data Division.
 Linkage Section.
 01 Return_Status Pic S9(9) COMP-5.
 Procedure Division Returning Return_Status.
     If File-Opened-Flag Not = 0
         *> File is already open, so return error
         Move 99 To Return_Status
     Else
         Open Input Infile
         Move Infile-Status To Return_Status
         If Infile-Status = 0
             Move 1 To File-Opened-Flag
         End-If
     End-If.
 End Method OPEN_FILE.

 Method-ID. CLOSE_FILE.
 Data Division.
 Linkage Section.
 01 Return_Status Pic S9(9) COMP-5.
 Procedure Division Returning Return_Status.
     If File-Opened-Flag = 0
         *> File is not open, so return error
         Move 88 To Return_Status
     Else
         Close Infile
         Move Infile-Status To Return_Status
         Move 0 To File-Opened-Flag
     End-If.
 End Method CLOSE_FILE.

 Method-ID. READ_FILE.
 Data Division.
 Linkage Section.
 01 Return_Record Pic X(132).
 01 Return_Status Pic S9(9) COMP-5.
 Procedure Division Using Return_Record
                    Returning Return_Status.
     If File-Opened-Flag = 0
         *> File is not open, so return error
         Move 88 To Return_Status
     Else
         Read Infile
         Move Infile-Status to Return_Status
         Move Infile-Record to Return_Record
     End-If.
 End Method READ_FILE.

 End Object.
 End Class FILEIO5.

Figure 4.1 Updated FILEIO5 program for use with Visual Basic


If you compare the FILEIO5 program in Figure 4.1 with the previous version shown in Figure 3.8, you will note the following modifications:

  1. The name of the text file being accessed in the SELECT… ASSIGN to statement has had a path added to it as shown:

     Select Infile Assign To "c:\comdoc\test.txt"

    The reason for this is that Visual Basic changes directories at runtime time and you cannot be sure that you will remain positioned in the same directory as you started execution in. This causes the file note to be found when attempting to open it with a path name on it.

  2. The RETURN_STATUS data item has had its definition changed from Pic 99 to Pic S9(9) COMP-5. This is an important modification. While we were executing completely in COBOL, we had the luxury of using any COBOL data type we desired. COM, however, supports a fixed set of specialized data types and Visual Basic does not support all of the various COBOL data types as a result. This means we must choose data types as parameters in method calls that are supported by the language that the COM client is written in. In this case, the Pic S9(9) COMP-5 is a LONG in Visual Basic, so we’ll use this definition for the RETURN_STATUS data item in all three of our methods.

  3. PROCEDURE DIVISION USING … statements in the OPEN_FILE and CLOSE_FILE methods have been changed to PROCEDURE DIVISION Returning … This signals the COM interface that we expect to return the data item named RETURN_STATUS to the COM client. It also allows us to call these methods from Visual Basic in a more logical fashion as well see a bit later in this chapter.

  4. The LINKAGE SECTION parameters in the Read_File method have been reordered so that we can change the PROCEDURE DIVISION USING… statement to:

     Procedure Division Using Return_Record
                        Returning Return_Status.

    This allows us to code the call to this method from Visual Basic in a more logical fashion, as we’ll see a bit later in this chapter.

Once we’ve applied these previously noted changes to our FILEIO5 module, rebuilt it and re-registered it using Regsvr32.exe, we are ready to create a Visual Basic project to access it.

To illustrate this, we create a standard .EXE project in Visual Basic and create a simple form with a command button (push button) control and a list box control. We will then implement behavior such that when the user clicks on the push button, the Visual Basic program creates an instance of the FILEIO5 class and executes its OPEN_FILE, READ_FILE, and CLOSE-FILE methods. Each record returned from the READ_FILE method will be added to the list box control for display purposes.

The Visual Basic form in this application is shown in Figure 4.2.

Figure 4.2 The Visual Basic form


Before we write any code referencing the FILEIO5 COM Server module, we must first add a reference to it in our Visual Basic application. This is done by selecting the References option in the Project pulldown menu in Visual Basic as shown in Figure 4.3.

Figure 4.3 The References option in the Project pulldown menu


This brings up the References dialog box listing all of the references available on the current system. You must scroll down and find the FILEIO5.DLL reference and check it as shown in Figure 4.4.

Figure 4.4 The References dialog box


After checking the FILEIO5.DLL check box, click on the OK button to apply this and close the Reference dialog box. We are now ready to write the actual Visual Basic code to implement the previously described behavior.

The source code of our Visual Basic Project is shown in Figure 4.5. We need only implement code for the Click event for the command button control named Command1.

 Private Sub Command1_Click()
 Dim Return_Status As Long
 Dim Return_Record As String
 Dim Return_Code As Long
 Dim FILEIO_PTR As FILEIO5.FILEIO5
     Set FILEIO_PTR = New FILEIO5.FILEIO5
     Return_Status = FILEIO_PTR.OPEN_FILE()
     If Return_Status = 0 Then
         Do While Return_Status = 0
             Return_Status = FILEIO_PTR.READ_FILE(Return_Record)
             If Return_Status = 0 Then
                 List1.AddItem (Return_Record)
             End If
         Loop
         Return_Status = FILEIO_PTR.CLOSE_FILE()
     End If
     Set FILEIO_PTR = Nothing
 End Sub

Figure 4.5 Visual Basic project source code


If we execute the previously noted Visual Basic application, the results will be as shown in Figure 4.6.

Figure 4.6 The results of executing the Visual Basic application


You should now understand how to create a Visual Basic COM Client program that accesses a COBOL COM Server program. In the final chapter, we will create an Internet application using an ASP page that calls the FILEIO5 COM Server program.