Microsoft Windows NT Applications | Panels Version 2 |
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.
Before trying to interface COBOL with another product, you must consider the following issues to ensure a stable interface:
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.
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.
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.
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.
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.
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)
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.
Some restrictions affect the interface between Visual Basic and COBOL:
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.
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;
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. |
In your PowerBuilder application, you can use the declaration with statements like:
String Pstring; ... Pstring = SPACE(length); ... value = COBSTRING (Pstring);
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 |
Some restrictions you need to be aware of when you interface PowerBuilder and COBOL:
Copyright © 1999 MERANT International Limited. All rights reserved.
This document and the proprietary marks and names
used herein are protected by international law.
Microsoft Windows NT Applications | Panels Version 2 |