PreviousMicrosoft Windows NT Applications Panels Version 2Next"

Chapter 18: Using GUIs from Third Party Tools

This chapter shows how you can develop Microsoft Windows applications using 16-bit third party products that interface with this COBOL system.

The products covered are:

Both of these products can produce graphical user interfaces for the Windows environment.

18.1 Guidelines

Before trying to interface COBOL with another product, you must consider the following issues to ensure a stable interface:

18.1.1 Called Versus Calls

You need to determine if COBOL calls the other product, or the other product calls COBOL. This chapter only considers third party products that call COBOL.

Visual Basic and PowerBuilder, described below, are examples of other products calling COBOL. For this type of interface, you either statically link COBOL into the application or create a COBOL Dynamic Load Library (.dll) file that is called from the other product.

18.1.2 Calling Conventions

You also need to determine the type of calling convention that is compatible with both products. A calling convention defines:

Under Windows, for example, most applications use the Pascal calling convention (CALL-CONVENTION 3).

See the documentation provided with your third party product to determine which calling convention to use.

18.1.3 Data Compatibility

You need to determine which COBOL data types map onto the corresponding data-types in the other language.

Some mappings are well defined. For example, the H2cpy program translates C data-types into their COBOL-equivalent data-types. See the chapter Header-to-COPY Utility in your Programmer's Guide to Writing Programs.

18.2 Interfacing with Visual Basic V2.0 and V3.0

Microsoft Visual Basic for Windows enables you to create a Windows user interface with all the features associated with a Graphical User Interface (GUI). These features include, for example, buttons, dialog boxes and sizable windows.

Combining Visual Basic and COBOL, you can build graphical Windows-based applications that use the strengths of each product. Used in this way, Visual Basic creates a GUI for your application, and calls a COBOL .dll to do other processing.

18.2.1 Creating a COBOL .dll File for Use with Visual Basic

The chapter Interfacing and Mixed-language Programming describes in detail how you can interface COBOL to other languages. The chapter Microsoft Windows Applications describes how you can create applications that run under Microsoft Windows.

This section uses techniques found in both chapters. You might want to refer to those chapters while reading this section.

Assume you have created this COBOL program named cobstr.cbl. You want to call this program from Visual Basic.

Example

 1  special-names.
 2  call-convention 3 is winapi.
 3  data division.
 4  working-storage section.
 5  01 str            pic x(10).
 6  ...
 7  procedure division winapi
 8  entry 'cobstring' using str. 
 9  ... 
10  exit program.

In this program:

Lines 1 - 2:

special-names.
call-convention 3 is winapi.

Identifies the calling convention. You must use the Pascal calling convention (CALL-CONVENTION 3) for all entry points. The example assigns the name winapi to the call convention.

Line 5:

01 str            pic x(10).

Defines a parameter passed from Visual Basic. You also could define this parameter in the File Section or Linkage Section.

Line 7:

procedure division winapi

Defines the parameter passing convention to be used on entry to the program.

Line 8:

entry 'cobstring' using str.

Identifies the entry point called by the Visual Basic application. (An entry point identifies a particular place in the program where another program can begin execution.) str is a parameter passed between the two programs.

Your Programmer's Guide to Writing Programs provides a detailed description of how to create .dll format files for Windows-based applications.

For this example, compile the program:

cobol cobstr.cbl omf"obj" litlink deffile deffiletype"win";

The DEFFILE directive instructs the Compiler to produce a definition file. The DEFFILETYPE"WIN" directive specifies the type of definition file to be suitable for use with Windows. By default, the Compiler names the definition file cobstr.def.

The LITLINK directive makes the Compiler declare the literals in the CALL literal statements as public symbols, so they are resolved at link time rather than at run time.

This step creates cobstr.obj.

Now link the application with the Windows libraries:

link cobstr.obj+libinit+cblwinl,vbstr.dll,,
    lcobolw+lcobol+cobw,cobstr.def /NOE/NOD;

where the definition file, cobstr.def, is generated by the Compiler. This link step creates the file vbstr.dll.


Note: You need to identify explicitly .dll as the extension for the output file (in the example vbstr.dll). Otherwise the Linker generates the file with the default extension .exe.


If your program uses floating point numbers, replace cblwinl with cblwinlf and place cobfp87w at the beginning of the link libraries.

18.2.2 Declaring the Visual Basic-to-COBOL Interface

Your Visual Basic program can now directly call the COBOL .dll file. In Visual Basic, you have to declare the function before you can use it. For example, declare the function using a statement like:

Declare Sub cobstring Lib "vbstr.dll"(PassString as String)

where the parameters are:

cobstring Name of exported entry in "vbstr.dll"
vbstr.dll Name of COBOL .dll file.
PassString Name of the passed parameter. This parameter corresponds to the str parameter in the COBOL .dll.
String Parameter type

Now you can call the COBOL routine using the Visual Basic CALL statement:

CALL cobstring(string_in)

18.2.3 Visual Basic Data-types and COBOL Data-types

When you pass a parameter between Visual Basic and COBOL, you must make equal declarations in both programs. This table lists equivalent data-types:

Visual Basic V2.0/V3.0
Micro Focus COBOL
Integer pic s9(4) comp-5
Long pic s9(9) comp-5
Single comp-1
Double comp-2

To pass a string from Visual Basic to COBOL, you must declare it in Visual Basic as a fixed-size string.

For example:

TypeString10                pic x(10)
    element as string * 10
End type

You also can pass user-defined types from Visual Basic to COBOL. For example, the following structure can be passed.

The Visual Basic declaration is:

Type MemInfo
    name as string * 20
    amount as integer
End type
Dim Mem as MemInfo

The equivalent COBOL Working-Storage Section declaration is:

 01 Mem.
    03 name             pic x(20).
    03 amount           pic s9(4) comp-5.

18.2.4 Restrictions

Some restrictions affect the interface between Visual Basic and COBOL:

  1. On termination, Visual Basic removes the main COBOL .dll, but leaves all other subprograms in memory. As a result, your COBOL application should cancel all COBOL subprograms before returning to Visual Basic.

  2. You should close all open files before returning to Visual Basic. Exclusive locks left on the files prevent you from reopening the files on subsequent calls to the COBOL subprogram.

18.3 Interfacing with PowerBuilder V3.0

PowerBuilder from Powersoft Corporation is a graphic PC-based client/server application development environment.

You can combine PowerBuilder and COBOL to build a Windows-based application; use PowerBuilder to create a GUI for your application, and COBOL to do other processing.

18.3.1 Creating a COBOL .dll File for Use by PowerBuilder

Say for example, you want PowerBuilder to interface with the same COBOL .dll that was described in the last section. Only now you want to name it pbstr.dll.

For this example, compile the program:

cobol cobstr.cbl omf"obj" litlink deffile deffiletype"win";

The directive DEFFILE and DEFFILETYPE"WIN" tell the Compiler to generate a definition file that is suitable for use with Windows. By default, the Compiler names the definition file cobstr.def.

Now link the .obj file with the Windows libraries:

link cobstr.obj+libinit+cblwinl,pbstr.dll,,
    lcobolw+lcobol+cobw,cobstr.def /NOE/NOD;

18.3.2 Declaring the PowerBuilder Function

Before using the .dll file in your PowerBuilder application, you first have to declare the function. For example:

FUNCTION int COBSTRING (REF string PassString) LIBRARY
     "pbstr.dll"

where the parameters are:

int Type of the returned function.
COBSTRING Name of the exported entry in pbstr.dll.
(REF string PassString) Parameter passed between the two programs. This parameter corresponds to the str parameter in the COBOL .dll.
pbstr.dll Name of the COBOL .dll.

18.3.3 Calling the COBOL .dll from PowerBuilder

In your PowerBuilder application, you can use the declaration with statements like:

String Pstring;
...
Pstring = SPACE(length);
...
value = COBSTRING (Pstring);

18.3.4 PowerBuilder Data-types and COBOL Data-types

When you pass a parameter between PowerBuilder and COBOL, you must make equal declarations in both programs. This table lists equivalent data-types:

PowerBuilder V3.0
Micro Focus COBOL
UINT pic 9(4) comp-5
ULONG pic 9(9) comp-5
INT pic s9(4) comp-5
LONG pic s9(9) comp-5
STRING pic x(...)
BYTE pic x
REAL comp-1
DOUBLE comp-2

18.3.5 Restrictions

Some restrictions you need to be aware of when you interface PowerBuilder and COBOL:

  1. PowerBuilder is not aware of any loaded COBOL .dlls. As a result, your COBOL application should cancel all COBOL subprograms before returning to PowerBuilder.

  2. You should close all open files before returning to PowerBuilder. Exclusive locks left on the files prevent you from reopening the files on subsequent calls to the COBOL subprogram.


Copyright © 1999 MERANT International Limited. All rights reserved.
This document and the proprietary marks and names used herein are protected by international law.

PreviousMicrosoft Windows NT Applications Panels Version 2Next"