Top
NetCOBOL V11.0 Getting Started
FUJITSU Software

A.14 Sample 13: Calling COBOL from Visual Basic

Sample 13 illustrates a COBOL DLL created with NetCOBOL that is called from a Visual Basic® application.

This sample program requires Visual Basic 5.0 or later.

Overview

At initialization the Visual Basic application calls a subroutine, JMPCINT2 that initializes the COBOL run-time environment, ready for a call to a COBOL program.

The Visual Basic form shows a simple equation in which the user enters two numbers (either side of a multiply “*” sign) and presses the “=” sign, which is a button. The Visual Basic application passes the two values to the COBOL application which does the multiplication and returns the result for the Visual Basic code to display.

In the Visual Basic application’s termination code, it calls the JMPCINT3 subroutine to close the COBOL run-time environment.

Files Included in Sample 13

Subroutines used in Sample 13

These subroutines are used by in the Visual Basic part of SAMPLE 13 to initialize and terminate the COBOL run-time system.

Building/Rebuilding the Program

Project manager's build function is used to create the executable program.

In the following screen snapshots, the sample program was installed to C:\NetCOBOL. Your installation folder may be different.

  1. The project manager is started, and project file "SAMPLE13.PRJ" is opened.

  2. The project file is selected, and "Linker options" is selected from "Project"-"Options" menu.

    The "Linker options" dialog is displayed.

  3. "/EXPORT:SAMPLE13" is specified for the "Command line" editing box. After confirms it, the OK button is clicked.

    You are now returned to the Project Manager window.

  4. Select "Build" from Project Manager's "Project" menu or click on the Build button in the Project Manager toolbar.

    After the build terminates, check that SAMPLE13.dll is created.

  5. When you rebuild executable file of Visual Basic, SAMPLE13.VBP (Visual Basic project file) included in the "Other” folder of the project is selected and "Edit" is selected from the project menu.

    Visual Basic starts; "Make of SAMPLE13.EXE" is selected from the file menu.

Debugging the Program

Applications containing a mixture of COBOL and Visual Basic code can be debugged under the control of both the COBOL and Visual Basic debuggers. To accomplish this, follow these steps:

  1. Select "Run-time Environment Setup Tool" from the "Tools" menu of the Project Manager.

    The run-time environment setup tool is displayed.

  2. Select "Open" on the "File" menu and select COBOL85.CBR in the folder that contains the executable program (SAMPLE13.EXE).

  3. Select the Common tab and enter data as shown below:

    • For the environment variable @CBR_ATTACH_TOOL, specify TEST.

  4. Click the Apply button.

    The data is saved in the object initialization file.

  5. Select "Exit" on the "File" menu to terminate the run-time environment setup tool.

  6. Start Visual Basic and load the SAMPLE13.VBP project.

  7. Start debugging the Visual Basic code when you execute the call to SAMPLE13 (the COBOL program) the COBOL debugger is invoked so that you can continue debugging in the COBOL part of the application.

  8. When you exit from the COBOL application control returns to the Visual Basic code.

The section “H. Debugging with Visual Basic” in Chapter 1’s Quick Tour steps through this example in some detail.

For more information on this topic, see Chapter 3, “Working with Visual Basic and COBOL” and the “NetCOBOL Debugging Guide”.

Executing the Program

To execute the program:

  1. The simple calculator window used by this application is shown in the following figure.

  2. To use this form:

    Enter a number (up to 4 digits) in each text box to the left of the “=” button.

Press the “=” button.

Visual Basic calls COBOL to perform the calculation and format the answer. Visual Basic then displays the answer to the right of the “=” button.