Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Net Cobol

Download as pdf or txt
Download as pdf or txt
You are on page 1of 322

Getting Started

Sixth Edition: August 2005

The contents of this manual may be revised without prior notice. No part of this document may
be reproduced or transmitted in any form or by any means, electronic or mechanical, for any
purpose, without the express written permission of Fujitsu Limited.

© 1992-2005 Fujitsu Limited. All Rights Reserved.


Preface
This manual provides an introduction to NetCOBOL for Windows. NetCOBOL for
Windows comes in three tiers, described below. All versions of the product provide a
full-featured development environment for COBOL programs. It allows you to
develop COBOL programs that easily integrate with other languages, such as
Microsoft®’s Visual Basic® development environment.
The first tier product is NetCOBOL Standard Edition. This provides a fully functional
COBOL development environment (COBOL Project Manager) designed to allow
COBOL programmers to develop powerful COBOL applications for the Microsoft
Windows family of platforms. It includes support that allows users to develop COBOL
applications that interact with Microsoft’s Visual Basic GUI development environment.
The second tier product is NetCOBOL Professional Edition. It builds on the Standard
Edition and adds a GUI development environment (PowerCOBOL). PowerCOBOL
allows COBOL programmers to develop sophisticated GUI applications, using native
COBOL code extensions. It provides an integrated GUI painter to aid this
development, and client/server development technology.
The third tier product is NetCOBOL Enterprise Edition. This builds upon the first two
tiers adding PowerFORM, PowerGEM Plus, PowerBSORT, the Foundation Class
Libraries, Data Tools, and the MultiThreaded Run-time system. PowerGEM Plus
provides sophisticated source code management capabilities for standalone and
collaborative development teams.
PowerCOBOL represents a modern COBOL development environment. It provides a
rich array of tools to allow Graphical User Interface (GUI) development in the COBOL
language.

Audience
Prior to using NetCOBOL, it is assumed that you have the following knowledge:
• You have some basic understanding as to how to navigate through and use the
Microsoft Windows product on your machine.
• You understand the COBOL language from a development perspective.
• If you plan on using Microsoft’s Visual Basic development environment, you
have spent some time using Visual Basic to get a feel for its interface and
capabilities.

Getting Started with Fujitsu COBOL 3


How This Manual is Organized
This manual contains the following information:
Chapter 1 Introduction
Chapter 2 A Quick Tour
Chapter 3 Developing GUI Applications
Chapter 4 Working with Visual Basic and COBOL
Chapter 5 Using SQL with COBOL
Appendix A Sample Programs
Appendix B Tips
Appendix C Support

Conventions Used in This Manual


Example of convention Description
setup, setup Characters you enter appear in bold.
Program-name Underlined text indicates a placeholder for
information you supply.
ENTER Small capital letters are used for the name
of keys and key sequences such as ENTER
and CTRL+R. A plus sign (+) indicates a
combination of keys.
… Ellipses indicate the item immediately
preceding can be specified repeatedly.
Edit, Literal Names of menus and options appear with
the initial letter capitalized.
[def] Indicates that the enclosed item may be
omitted.
{ABC|DEF} Indicates that one of the enclosed items
delimited by | is to be selected.
CHECK Commands, statements, clauses, and
WITH PASCAL LINKAGE options you enter or select appear in
uppercase. Program section names, and
PARAGRAPH-ID some proper names also appear in
COBOL uppercase. Underlined text indicates the
default.
ALL
DATA DIVISION.
This font is used for examples of program
WORKING-STORAGE SECTION. code.
* Get the #INCLUDE file to the data
control
#INCLUDE "NUMDATA.COB".

The form acts as an application creation Italics are occasionally used for emphasis.
window.
“PowerCOBOL User’s Guide” References to other publications or chapters
See Chapter 6, “Creating an Executable within publications are in quotation marks.
Program.”

4 Getting Started with Fujitsu COBOL


Related Manuals
NetCOBOL User’s Guide
NetCOBOL Language Reference
NetCOBOL Syntax Samples
NetCOBOL Debugging Guide
NetCOBOL CBL Subroutines User’s Guide
NetCOBOL CGI Subroutines User’s Guide
NetCOBOL Collection Class Library User’s Guide
NetCOBOL File Access Subroutines User's Guide
NetCOBOL Getting Started with COM Components
NetCOBOL ISAPI Subroutines User’s Guide
NetCOBOL SAF Subroutines User’s Guide
NetCOBOL Web Development Tools
NetCOBOL Web Guide
NetCOBOL Run-time Installation Guide
NetCOBOL DB Access Class Library API Reference
NetCOBOL J Adapter Class Generator User's Guide
PowerBSORT Getting Started
PowerBSORT Reference
PowerCOBOL Getting Started
PowerCOBOL Reference
PowerCOBOL User’s Guide
PowerFORM Getting Started
PowerFORM Run-time Reference
PowerGEM Plus Getting Started
PowerGEM Plus User’s Guide

Trademarks
COBOL/2 is a registered trademark of Micro Focus International Ltd.
Microsoft, MS-DOS, Windows, Windows NT, Windows Server and Visual Basic are
trademarks or registered trademarks of Microsoft Corporation.
Oracle is a registered trademark of Oracle Corporation.
Other product names are trademarks or registered trademarks of each company.
Trademark indications are omitted for some system and product names described in
this manual.

Getting Started with Fujitsu COBOL 5


Contents

Chapter 1. Introduction ...............................................................................9


Overview of the Getting Started Manual ............................................................... 9
Purpose of the NetCOBOL Product ......................................................................10
Core Features of NetCOBOL ...............................................................................11
Key Features of the V8 Release ..........................................................................12
Installing NetCOBOL ..........................................................................................13
Features of the Different NetCOBOL Editions .......................................................14

Chapter 2. A Quick Tour .............................................................................17


Overview of the Guided Tour..............................................................................17
A Guided Tour through NetCOBOL ......................................................................19

Chapter 3. Developing GUI Applications.....................................................63


A Brief History...................................................................................................63
NetCOBOL ........................................................................................................65
Event-driven Programming .................................................................................65
Using NetCOBOL ...............................................................................................68

Chapter 4. Working with Visual Basic and COBOL......................................69


Creating Your First Visual Basic COBOL Application...............................................69
Integrating COBOL Programs with Visual Basic.....................................................89

Chapter 5. Using SQL with COBOL .............................................................95


Introduction......................................................................................................95
Setting up the SQL Execution Environment ..........................................................96
Setting up SQL Databases and Tables .................................................................97
Defining an ODBC Data Source ...........................................................................97
Setting Up the COBOL Runtime Information.......................................................109
Potential Execution Errors Caused by Incorrect Setups .......................................122

Appendix A. Sample Programs .................................................................125


Using the Sample Program Projects ..................................................................127
Sample 1: Data Processing Using Standard Input-Output...................................128
Sample 2: Using Line Sequential and Indexed Files ...........................................135

6 Getting Started with Fujitsu COBOL


Sample 3: Screen Input-Output Using the Presentation File Function ..................138
Sample 4: Screen Input-Output Using the Screen Section ..................................138
Sample 5: Calling COBOL Subprograms ............................................................143
Sample 6: Receiving a Command Line Argument...............................................150
Sample 7: Environment Variable Handling ........................................................154
Sample 8: Using a Print File.............................................................................158
Sample 9: Using a Print File (Advanced usage) .................................................161
Sample 10: Using a Print File with FORMAT clause .............................................169
Sample 11: Remote Database Access...............................................................170
Sample 12: Remote Database Access (Multiple row processing) .........................180
Sample 13: Calling COBOL from Visual Basic.....................................................185
Sample 14: Visual Basic calling COBOL –Simple ATM Example ............................190
Sample 15: Basic Object-Oriented Programming ...............................................197
Sample 16: Collection Class (Class Library) .......................................................200
Sample 17: Object-Oriented Cobol (Aggregation, Singleton, and Iteration) ..........211
Sample 18: Advanced Object-Oriented Programming.........................................218
Sample 19: Object Persistence (Indexed File) ...................................................223
Sample 20: Object Persistence (Database) .......................................................226
Sample 21: Multi-Thread Programming ............................................................234
Sample 22: Multi-Thread Programming (Advanced usage) .................................245
Sample 23: COM Program to Control Excel (Late Binding) ..................................260
Sample 24: COM Program to Control Excel (Early Binding) .................................264
Sample 25: Creating a COBOL COM Server Program..........................................269
Sample 26: Using the COM Linkage-COBOL Server Program (COBOL Client) ........277
Sample 27: Using the COM Linkage-COBOL Server Program (ASP Client) ............284
Sample 28: Transaction Management with COM Linkage-MTS ............................291
Sample 29: Inter-application Communication Function.......................................299
Sample 31: Windows System Function Call .......................................................304
Sample 32: Starting Another Program ..............................................................308

Appendix B. Tips........................................................................................313

Appendix C. Support .................................................................................315

Index .........................................................................................................317

Getting Started with Fujitsu COBOL 7


8 Getting Started with Fujitsu COBOL
Chapter 1. Introduction
This chapter introduces NetCOBOL and outlines its major functions. It covers the
following topics:
• Overview of the Getting Started Manual
• Purpose of the NetCOBOL Product
• Core Features of NetCOBOL
• Key Features of the V8 Release
• Installing NetCOBOL
• Features of the different NetCOBOL editions

Overview of the Getting Started Manual


Getting Started with NetCOBOL aims to take you through all the key areas you might
wish to cover when starting with the NetCOBOL product.
Chapters 1 and 2 give you a quick introduction to the product.
Chapters 3 to 5 and Appendix A cover GUI’s, working with Visual Basic, using
SQL, and the sample programs in more depth.
Appendix B provides tips for first-time users.
When you have covered the topics described in this manual you will find the
“NetCOBOL User’s Guide” to be your best source for detailed information about using
the product.

Product Reviewers
You may be reviewing NetCOBOL for the purpose of assessing its suitability for an
organization or writing an article. Chapters 1 and 2 aim to take you to the most
significant parts of the product's functionality as quickly as possible, so that your
time is used most effectively.
Because there is a wealth of functionality in the product, and your interest may be in
any part of the product, the guide may appear to be quite long. However, you will
find that you can work through the scripts quickly and can focus on the areas of
greatest interest by using the table of contents or the “Overview of the Guide
Through the Product” at the start of Chapter 2.
10 Chapter 1. Introduction

Purpose of the NetCOBOL Product


The NetCOBOL compiler and COBOL Project Manager are the core components of all
editions of NetCOBOL (Standard, Professional, and Enterprise).
The purposes of these two components are to provide:
• Highly reliable, highly optimized execution of COBOL applications.
• Object code that integrates well with code created using other languages or
development environments.
• Support for standard COBOL and COBOL dialects.
• Integration of tools that cover the complete code development cycle:
editing, compiling, debugging, testing, maintaining, and managing.
• Simplified application building.
• Support for object oriented program design and maintenance.
Other components, such as PowerCOBOL, depend on which edition you purchase,
and are accompanied by their own documentation. They provide a complete and rich
environment for developing COBOL applications, covering such needs as:
• GUI application development.
• Source control and configuration management.
• Graphical report form design and execution.
• Data reporting and data file editing.
• Stand-alone data sorting.
Chapter 1. Introduction 11

Core Features of NetCOBOL


The core features of the NetCOBOL compiler are:
• It produces highly reliable code.
• Using industry standard benchmarks its code is the fastest available in the PC
environment.
• It produces standard objects that are easily integrated with code created
using other languages.
• It supports a wide variety of COBOL dialects including:
CBL_ Routines Micro Focus COBOL1
COBOL 68, 74, 85, 97 Object-Oriented COBOL
1
IBM OS/VS COBOL X/OPEN COBOL
IBM VS COBOL II1
• It supports embedded SQL statements ("EXEC SQL") to provide access to
many relational databases including:
Oracle Any ODBC-compliant
database
SQL Server
• Provides the ability to create multi-threaded applications.
The key features of the COBOL Project Manager development environment are:
• Centered on a project tree view.
• Automates the application build steps.
• Provides access to all the essential tools required to support COBOL program
development.
• Supports the integration of other tools.
• Provides a language sensitive editor.
• Contains a full function debugger.
• Debugging with other languages, such as Visual Basic, is straightforward. For
example you can run the NetCOBOL and Visual Basic debuggers
concurrently - not something you can do with many COBOL compilers.
• Debugger has the unique ability to store, edit and play back sequences of
debugging commands or complete debugging sessions. This is particularly
useful when debugging code at the core of extremely large, nested,
applications.
• Provides basic and advanced tools for creating, editing and maintaining
data files.
• Links to the Fujitsu PowerGEM configuration management system.

1
Partial dialect support. For full support see Fujitsu’s Dialect Converter and Processor
products.
12 Chapter 1. Introduction

A number of additional features are provided with the product and these are
described at the end of this chapter in the topic "Features of the Different NetCOBOL
Editions".

Key Features of the V8 Release


The following are the main enhancements in NetCOBOL Version 8:
• New integer numeric data types - BINARY-CHAR, BINARY-SHORT, BINARY-
LONG, and BINARY-DOUBLE – from the International Standard COBOL2002
USAGE clause. These types are designed to facilitate sharing integer numeric
data with other languages.
• Items specified as USAGE BINARY, USAGE COMP, or USAGE COMPUTATIONAL
can be treated as if they were specified as USAGE COMP-5 by specifying the
ASCOMP5 compiler option.
• The following Micro Focus Native functions are supported. These make it easier
to migrate from Micro Focus COBOL.
- Specifying the default initial value for data items that do not have a VALUE
phrase, using the INITVALUE compiler option.
- Hexadecimal numeric literal (H”xx”)
- Specifying the ADVANCING phrase in WRITE statements for line sequential
files.
• Enhancement of the CHECK function:
- By specifying the CHECK (PRM) option, the system checks parameter
conflicts at execution time for CALL statements that call external programs.
- The data exception check performed by the CHECK(NUMERIC) option is now
also run when alphanumeric data items or group items are moved to display
decimal items or packed decimal items.
- You can specify multiple sub-operands of the CHECK option at one time.
• The compiler provides listings containing information on data areas by specifying
the MAP option.
• You can write simple COBOL programs to process deadlocks that occur with
linked databases by using the USE FOR DEADLOCK procedures and calling the
Deadlock Exit subroutine
• You can configure the date retrieved by COBOL applications in ACCEPT … FROM
DATE statements and the CURRENT-DATE function, by using the
@CBR_JOBDATE environment variable.
• Automated functions are provided for concatenating files and appending to
existing files, by using the MOD and CONCAT options in the file-identifier
environment variable.
• COBOL programs can share external data items with C program external
variables of the same name.
• Improved runtime messages
- COBOL program line numbers are included in runtime messages, making it
easier to locate errors.
Chapter 1. Introduction 13

- You can specify the severity code at which runtime messages are output.
This makes it possible to output the level of messages appropriate to your
operating environment.
• Sanitizing is now supported in the Web programming subroutines, making it
possible to counter cross-site scripting vulnerabilities of Web applications.

Installing NetCOBOL
To install NetCOBOL insert the CD in your CD drive. The NetCOBOL Master Setup
should start. If it does not start, run “autorun.exe” from the root folder of the CD.
(For example, from the Start menu select Run, and enter “M:autorun.exe”, where
“M” is the drive letter of your CD-ROM drive.)
From the Master Setup screen select NetCOBOL Suite and follow the Setup
instructions in the dialog.
MAKE SURE YOU HAVE YOUR SERIAL NUMBER at hand when you run the setup as
you will be prompted to enter it.
The Master Setup screen gives you the option of installing other items such as online
documentation and add-on products that are provided with NetCOBOL. To install any
of these items simply select them and follow the installation instructions that are
displayed.
14 Chapter 1. Introduction

Features of the Different NetCOBOL Editions


The section "Core Features of NetCOBOL" described the fundamental features of the
NetCOBOL product. However, NetCOBOL provides many other features, some of
which will depend on the product edition you purchase. In this final section of
Chapter 1 we give brief descriptions of these other features to give you a feel for the
depth of functionality provided in the product.

NetCOBOL Editions
NetCOBOL can be purchased in one of three editions: Standard, Professional and
Enterprise. The table below illustrates the contents of each of these editions.

Table 1.1. Make-up of the NetCOBOL Editions

Product name Component Outline


NetCOBOL Enterprise Edition
NetCOBOL Professional Edition
NetCOBOL Standard Edition
NetCOBOL Core NetCOBOL compiler and
development environment plus
other features to aid application
development.
PowerCOBOL Development environment for
developing and executing GUI
applications.
Foundation Class Set of OO COBOL libraries
Library offering extended functionality to
your applications.
PowerFORM Design tool for print forms and
reports with associated run-time
support.
PowerGEM Plus Resource management and
platform for remote application
development for UNIX
applications.
DataTools Tools for creating, converting
and maintaining COBOL data
files.
PowerBSORT High performance sort/merge
program with command line and
call interfaces.
Chapter 1. Introduction 15

Additional Features
The following list gives you brief descriptions of the more significant additional
features that make up your NetCOBOL product. The edition in which the feature
becomes available is shown in parentheses. Each of these features are described in
greater detail in feature-specific manuals or in the "NetCOBOL User's Guide", all of
which can be accessed from the Documentation Viewer, invoked from the Start,
Programs, Fujitsu NetCOBOL for Windows V8.0 menu.
• Precompiler Integration (Standard)
If your programs use features that need to be precompiled before compilation by
the NetCOBOL compiler, you can have the COBOL Project Manager invoke the
precompiler automatically whenever you build your application or compile your
program.
• Web Application Wizard (Standard)
Using HTML as input the Web Application Wizard can generate COBOL programs
that can support the data input and output using one of the following protocols:
CGI, ISAPI, and SAF
• CORBA Development Environment (Standard)
Tools and interfaces that support CORBA application development for both
traditional and OO COBOL programs.
• Project Browser and Class Browser (Standard)
Provide the ability to view relationships between classes and programs.
• COM/DCOM Integration (Standard)
Integration with products such as IIS2 and MTS3 is straightforward because of
the COM/DCOM support built into the product.
• COBOL Web Subroutines (Standard)
Subroutines are provided to simplify the process of writing Web-supporting
applications for CGI, ISAPI and SAF. Where appropriate the subroutines are
provided for other platforms, such as the CGI subroutines being available with
the Linux version of NetCOBOL, aiding application portability.
• GUI Application Development (Professional)
The PowerCOBOL component is a full wysiwyg development environment for GUI
applications. It uses COBOL to support the events and methods, enabling COBOL
programmers to quickly adapt to creating sophisticated GUI applications.
• GUI Report Design (Enterprise)
PowerFORM enables the creation of print forms and reports that can include
graphical elements such as lines, boxes, shading, pictures and bar codes. Its
programming interface is closely integrated with NetCOBOL.
• DB Access Class Library (Enterprise)
One of the foundation class libraries, provides the ability to access several
different DBMS's from OO COBOL applications.

2
Internet Information Server
3
Microsoft Transaction Server
16 Chapter 1. Introduction

• Collection Class Library (Enterprise)


Provides classes to support collections of objects. Part of the foundation class
library.
• Java Class Wrapper Library (Enterprise)
Provides the ability to wrap Java classes so that they can be used from OO
COBOL programs. Part of the foundation class library.
• Resource Management (Enterprise)
The PowerGEM component is a sophisticated resource management tool
providing centralized management of resources (such as COBOL sources, object
files etc.) for projects involving many developers or development teams.
• Remote Application Development (Enterprise)
PowerGEM also provides the underlying support that enables NetCOBOL for
Windows to be used as the development environment for applications targeted
at UNIX platforms such as Sun Solaris.
• Data File Editing (Enterprise)
The NetCOBOL Data Editor uses COBOL record descriptions to provide field-by-
field display and editing of COBOL data files. Functions that support the
automatic generation of data are important aids in creating test data.
• Data File Conversion (Enterprise)
The NetCOBOL Data Converter provides field-sensitive conversion of data files
between COBOL file types and between ASCII and EBCDIC character sets.
• Sort Utility (Enterprise)
The PowerBSORT component provides fast sorting routines that can be used to
replace the standard NetCOBOL sort support, be used as a batch sort utility or
incorporated into programs written in other languages using the PowerBSORT
OCX.
Chapter 2. A Quick Tour
This chapter takes you on a quick tour of the NetCOBOL development environment.
It provides you with:
• An “Overview of the Guided Tour” to help you determine which parts of the tour
are most relevant to you.
• “A Guided Tour through NetCOBOL” which takes you through:
− Using COBOL Project Manager
− Debugging
− Using Configuration Management
− Working with OO COBOL
The chapter focuses on creating COBOL applications. See “Chapter 4. Working with
Visual Basic” for details on developing Visual Basic COBOL applications.
NetCOBOL ships with sample COBOL applications that cover a wide array of
NetCOBOL functions. Appendix A describes these sample applications in detail.

Overview of the Guided Tour


With so many features it is not practical to give you a detailed guide through all of
NetCOBOL's features and qualities. For example, you need to experience the
reliability of the code for yourself, and the only valid test of a compiler's performance
is how fast it executes your code. The guided tours in the following sections
therefore aim to give you a quick, get-started overview of COBOL Project Manager,
then go into depth in just three of the feature areas, namely debugging,
configuration management and OO COBOL.
The guided tours will show you the following functions:
Using COBOL Project Manager
Quick tour, showing how to access and use most of the functions in COBOL Project
Manager.
• Using a project - opening projects, understanding the project tree, and
building trees.
• Editing code - invoking the editor, editing free-format COBOL source, and aids
to code readability.
• Building - building, executing, and setting compile and link options.
• Maintaining data files - pointers to, and descriptions of, the data
maintenance functions.
18 Chapter 2. A Quick Tour

Debugging
• Preparing for debugging - setting compiler options and building.
• Invoking the debugger - options available in setting the debugging
environment.
• Controlling execution - description of execution control functions and how to
access them.
• Breaking execution - setting simple breakpoints, breaking on change of data,
and breaking on a condition.
• Monitoring and changing data values - datatips, watching data, and the
detailed data dialog.
• Tracing execution path - overview of switching on recording and exploring
the execution path.
• Using the recording and playback functions - step-by-step instructions for
recording and playing back debugging commands or sessions.
• Debugging with Visual Basic - walk through a dual language debugging
session.
Using the configuration management functions
• Overview of PowerGEM structures - context setting for the configuration
management environment.
• How to set up a PowerGEM library - step-by-step instructions for setting up
a PowerGEM library.
• Using the configuration management functions from Project Manager -
how to check-out and check-in files, the effects of check-out, and overview of
other CM functions.
Working with OO COBOL
• Brief introduction to OO COBOL - concepts supported and a description of
the major constituents of OO COBOL.
• Guided tour of an OO COBOL application - an OO COBOL application
viewed from the Project Manager, COBOL editor, Project Browser and Class
Browser.
Chapter 2. A Quick Tour 19

A Guided Tour through NetCOBOL


Follow the steps outlined in the sections below to acquaint yourself with some of the
key features of the NetCOBOL product.
Note: Many of the windows have been resized before taking snapshots so that they
do not take up too much space in this document. The windows you see are likely to
be different sizes to the windows shown in this tour.

Using COBOL Project Manager


The COBOL Project Manager is an integrated development environment that provides
project level management for developing COBOL applications. The Project Manager
brings together:
• A sophisticated, yet easy to use source code editor
• A full ANSI 85 and ANSI 74 compliant 32 bit COBOL compiler
• A 32 bit linker
• A sophisticated COBOL source debugger
• Execution facilities
• Project management and Make facilities
• Fujitsu’s File Management Utility
• OO COBOL Class and Project browsers
We will use one of the COBOL samples to illustrate how to access the various
functions of COBOL Project Manager.
20 Chapter 2. A Quick Tour

A. Invoking COBOL Project Manager and Opening a Project


1. From the Windows Start menu select Programs, Fujitsu NetCOBOL for Windows
V8.0, COBOL Project Manager.
The COBOL Project Manager window is displayed.

Figure 2.1. COBOL Project Manager

2. Click on the Project Open button, , on the toolbar and navigate to the folder:
C:\Program Files\Fujitsu NetCOBOL for Windows\Samples\cobol\Sample05
- assuming that you used the default folder names when you installed the
products. Substitute your folder names if you overrode the defaults.
Chapter 2. A Quick Tour 21

3. Select the file sample05.prj and click Open.


Project Manager opens the project and displays a contracted project tree:

Figure 2.2. Sample05 in COBOL Project Manager

Notice that you can immediately see that there are two parts to this project - an
EXE file and a DLL file. You would be correct in deducing that the program
within the EXE calls a program within the DLL!
22 Chapter 2. A Quick Tour

4. Fully expand the project tree by selecting View, Expand All from the menu bar.
(Or you can repeatedly click on the plus symbols, , in the project tree but that
takes longer!). The expanded tree looks like this:

Figure 2.3. Sample05 fully expanded in COBOL Project Manager

Notice:
- You quickly have an overview of how the application sources are structured: a main
program, SAMPLE5.COB, calls a subprogram, PRINTPRC.COB, and they both share a
common copy library, S_REC.CBL.
- The chosen program structure uses a library file PRINTPRC.LIB.
- The different types of file are clearly distinguished by different icons.
- The icon for the main program, SAMPLE5.COB, stands out as it is colored red.
- The project tree is made up of folders and items. Folders contain collections of
associated items, and items are assembled from several collections. For example the
project folder contains two items, SAMPLE5.EXE and PRINTPRC.DLL. The
SAMPLE5.EXE item is made up of two collections: a collection of COBOL source files;
and a collection of library files (because the application is a simple one, each
collection only contains one item).
5. Right-click on various folders and items in the project tree.
Project Manager displays a pop-up menu with appropriate functions for the
selected folder or item.
You build project trees by using the New Folder and New/Add File functions.
Chapter 2. A Quick Tour 23

B. Editing Source Code


1. For example, one of the things you can do using the pop-up menu is edit the
source code. Right-click on SAMPLE5.COB and select Edit from the pop-up
menu.
SAMPLE5.COB demonstrates the use of free format coding, and doesn't have
line numbers in columns 1-6. The editor therefore displays the dialog below:

Figure 2.4. Unrecognized/incorrect line numbers dialog.

2. Make sure you select "Read as text without line numbers" and click OK.
The Editor displays the source code for SAMPLE5.COB.
Notice that readability of the code is aided by using different colors for
comments, reserved words and user-defined words.

Figure 2.5. Sample5 in the Editor.


24 Chapter 2. A Quick Tour

We will make a trivial change to the program so that you can see Project
Manager's build function in action.
3. Make sure the status bar is visible by selecting View from the Editor menu bar.
If there is no check mark by "Status Bar" click on "Status Bar" and the status
bar will be displayed at the bottom of the window.
4. Scroll down until you get to line 61 (the line number is displayed in the status
bar). Line 61 contains the text:

DISPLAY "GENERATE WORK-FILE =" WORK-FILE-NAME

5. Insert a space after the "=" sign.


6. Close and save the file by selecting File, Exit Editor from the menu bar and
responding "Yes" to the dialog asking if you want to save.

Figure 2.6. “Do you want to save?” dialog.

C. Building and Executing


Because the project structure is already in place building is straightforward.
1. Right-click on SAMPLE5.EXE in the project tree, and select Build from the pop-
up menu.
Project Manager compiles the changed source program and builds the EXE. The
build is actually guided by a make file that Project Manager maintains based on
the project tree.
2. Any errors in the build are highlighted in red. The Builder tool has functions for
navigating from one error to the next. If you have any errors, double click on
the error line and the builder puts you into the Editor at the appropriate line. Fix
each error and build the program again.
A successful build has no red error messages – as in Figure 2.7:
Chapter 2. A Quick Tour 25

Figure 2.7. Builder window showing a successful build.

3. Close the Builder window.

Setting compiler and linker options: Had you wanted to change the
compiler or linker options before building, you would have selected
SAMPLE05.PRJ in the project tree view and, from the menu bar, selected
Project, Option, Compiler (or Linker) Options. These functions display dialogs in
which you can set the options. You are offered lists of all the compiler options
and dialogs explaining the supported options, so you do not have to remember
all those details.

Note: SAMPLE5 prints a single sheet of data to your default printer. If this is
likely to cause a problem do not execute the steps 4, 5 and 6.

4. SAMPLE05 relies on a file that is created by SAMPLE02. With our apologies,


changes to this guided tour mean the file is not necessarily available at this
point. To be sure it is available, close the SAMPLE05 project, open
SAMPLE02.PRJ (in the ..\Sample02 folder), Build SAMPLE02 and execute
SAMPLE02 (select SAMPLE02.EXE in the project tree and select Project, Execute
from the menu bar). Then close SAMPLE02.PRJ and reopen SAMPLE05.PRJ.
5. Now execute the program by selecting SAMPLE5.EXE in the project tree and
selecting Project, Execute from the menu bar.
Project Manager starts the program executing. SAMPLE5 executes displaying the
message you edited - check that there is a space after the "=" sign.
6. Click OK in the message box. SAMPLE5 continues to run, printing a single sheet
of data to your default printer.
We will have a closer look at the program in the debugging section.
26 Chapter 2. A Quick Tour

D. Maintaining Data Files


NetCOBOL provides the COBOL File Utility for maintaining COBOL data files.
1. From the Tools menu, select File Utility.
Project Manager invokes the COBOL File Utility.
2. Click on the Commands menu of the COBOL File Utility to see the range of
functions provided. You can select each of these functions in turn to see exactly
what they provide. The following list summarizes the functions ("COBOL files"
means sequential, relative or indexed files in fixed or variable length format):
Convert: Converts text files to variable length sequential files or variable length
sequential files to text files.
Load: Loads COBOL files from variable length sequential files.
Unload: Creates variable length sequential files from COBOL files.
Browse: Provides hex and character display of data in COBOL and text files.
Print: Prints data from COBOL and text files.
Edit: Provides hex and character editing of data in COBOL and text files.
Extend: Provides hex and character input of data to extend (add to) COBOL
and text files.
Sort: Sorts data in COBOL and text files.
Attribute: Displays statistics on indexed files.
Recovery: Recovers data from corrupted indexed files.
Reorganize: Reorganizes indexed files by deleting blocks of unused data.
Copy: Copies files.
Delete: Deletes files.
Move: Moves files.
Although some of the functionality of the COBOL File Utility is provided in a basic
fashion you can see that most file maintenance needs are covered by the above
functions.
Purchasers of the Enterprise Edition of NetCOBOL also have the Data Converter and
Data Editor tools. These provide more sophisticated, COBOL-record-description-
aware data conversion and editing functions. These tools are provided on the Start
Programs menu (NetCOBOL for Windows group) or can be added to Project Manager
using the Tools, Customize Menu function. Dataedit.exe and Dataconv.exe are stored
in the "C:\Program Files\Fujitsu NetCOBOL for Windows\DataTool" folder.
Chapter 2. A Quick Tour 27

Debugging
This section guides you through a number of the key debugging functions.

A. Preparing for Debugging


Programs are prepared for debugging by compiling with the TEST compiler option.
When you set this compiler option within Project Manager the appropriate linker
options are set automatically. The Project Manager has a “Build for Debugging”
option on the Project, Option sub-menu that sets all the appropriate compiler and
linker options for you.
In Project Manager compiler and linker options are set at the project level - so all
files within a project are compiled with the same options.
Check that Sample05 is being built for debugging:
1. From the Project menu select, Option and check that “Build for Debugging” is
checked.
2. If it is not checked, click on the menu item.
If it is checked click on a blank area of the Project Manager window to close the
menu.
3. From the Project menu select Build.
Project Manager builds the project using the newly set compiler and linker
options.
Note that if you didn’t have to check the “Build for Debugging” option, so that
nothing has changed since the previous build, the builder simply displays the
messages “Build Start” and “Build End” indicating that there was nothing to do.

The project is now ready for debugging.

B. Invoking the Debugger


To invoke the debugger:
1. Select SAMPLE05.PRJ in the project tree.
2. From the Project menu select Debug.
The COBOL Debugger starts and displays the Start Debugging dialog.
28 Chapter 2. A Quick Tour

Figure 2.8. Start Debugging dialog.

This dialog gives you the opportunity to specify information that may affect your
debugging session, such as: the locations of various source and system files, the
option to have the debugging session driven by a command file, and even which
program you first want to see in the debugger. Although the defaults will work
for you most of the time, the ability to configure this information at start-up is
invaluable in certain situations, such as debugging with other languages. (See
"Debugging with Visual Basic" below).
3. For this debugging session you do not need to specify any extra information so
click OK.
4. The COBOL Debugger displays Sample5.cob. It highlights the first executable
line in the procedure division.
Chapter 2. A Quick Tour 29

Figure 2.9. Sample5 in the COBOL Debugger.

Notice that code readability is aided by coloring the source code.


5. You may want to open a number of windows in the debugger so maximize the
debugger window by clicking on the maximize button, , at the top right of the
window.
6. Widen the source display window by dragging the border.

You are now ready to explore the program using the debugging features.

Sections C to F give general descriptions of the functions available with pointers to


where these functions can be found. You can experiment with the functions that are
of most interest to you.
Some powerful features you may want to check are:
• The "Specific Execution" execution control function. (In section C)
• Break on change of data. (In section D)
• Datatips. (In section E)
• Tracing the execution path. (In section F)
30 Chapter 2. A Quick Tour

C. Controlling Execution
Location
The execution control functions are located on the Continue menu, and a number of
them are available on the toolbar:

Hover the mouse pointer over a button to display a tooltip confirming the function of
the button.
Description

Re-debug Restarts the program debugging session with re-initialized data.


Gives the option of clearing or retaining breakpoints, watch data, and
passage counts.

Go: Runs the program to the next breakpoint, break condition, or


the end whichever comes first.

Animate: Executes the program line by line at a configurable speed.

Break: Interrupts execution when the program is running or being


animated.
Change Start Point to Cursor: (not on toolbar)
Makes the statement containing the cursor the next statement to be
executed. (If disabled try stepping one statement.)

Run to Cursor:
Runs the program up to the statement containing the cursor.

Step Into: Executes one statement. If the statement transfers control to


another debuggable program the debugger takes you into that
program.

Step Over: Executes one statement and stops on the next statement in the
current program, regardless of whether the current statement
transfers control to another debuggable program.

Step Out: Runs the current program, interrupting execution when control
passes to the calling (debuggable) program.

Run to Next Program:


Runs the current program, interrupting execution when control
passes to another debuggable program.
Specific Execution: (not on toolbar)
Runs the program up to the next point specified in the Specific
Execution Condition.
Chapter 2. A Quick Tour 31

Specific Execution Condition: (not on toolbar)


Displays a dialog from which you can select a condition for
interrupting program execution. Options include stopping at any of 52
verbs, stopping at a specified line number, stopping at the entry or
exit of a named program, and stopping where a named file is
accessed.

D. Breaking Execution
In addition to specifying points to which execution should run in the execution
control functions, there are three other ways of breaking execution:
• Setting breakpoints
• Breaking on change of data
• Breaking on satisfaction of a specified condition
Breakpoints
1. To set a simple breakpoint - right click on the line to contain the breakpoint
and select "Set Breakpoint" from the pop-up menu.
2. To set a more complex breakpoint, select Breakpoint from the Debug menu.
You can specify that execution should only be interrupted at the line if a
condition is also satisfied, or if the line has been executed a particular number
of times. You can also maintain a list of breakpoints enabling and disabling them
depending on your debugging situation.
Break on Change of Data
To have the debugger interrupt execution when the value of a data item changes:
1. Right-click on the data item name, anywhere that it occurs in the text, and
select Add Watch Data from the pop-up menu.
The debugger displays the Add Watch Data dialog with the data and program
name fields already filled in:

Figure 2.10. “Add Watch Data” dialog.

2. Select any options you require, such as “Break at change of value” and click OK.
The item is added to the Watch window - shown below with two items:
MASTER-RECORD and PARAMETER-LEN.
32 Chapter 2. A Quick Tour

Figure 2.11. The Watch Data window.

The check box indicates that any change in PARAMETER-LEN causes a break in
execution – the red colored text indicates a break on change of value has just
occurred.
The queries in the MASTER-RECORD entry indicate non-character values (in this
case LOW-VALUES as the record has not yet been read).
The “…” in the MASTER-RECORD entry indicates there is more data than
displayed in the window.
3. You can check and uncheck the checkboxes in the Watch window to monitor
different items for changes.
Break on Condition
To have the debugger interrupt execution when a particular condition is satisfied:

1. From the Debug menu, select Add Watch Conditional Expression.


The debugger displays the Add Watch Conditional Expression dialog with any
word that was pointed at entered in the “Conditional expression” field.

Figure 2.12. The “Add Watch Conditional Expression” dialog.

2. Enter a conditional expression in the entry field. (e.g. PARAMETER-LEN>9).


Note: The Browse function lets you enter the name of a record or group item,
then select data items from within that record.
Chapter 2. A Quick Tour 33

3. Click the OK button.


The debugger adds the condition to the Watch window:

Figure 2.13. The Watch Data window with a condition added.

It then monitors the condition and interrupts execution when the condition is
satisfied.

E. Monitoring and Changing Data Values


You can monitor data values by using Datatips or the Watch Data window.
You can change data using the Debug - Data dialog, or the Watch Data window.
Datatips
You can check the value of a data item very quickly by using the datatip feature. Just
hover the mouse pointer over the data item name anywhere in the text, and the
debugger displays the value.

Figure 2.14. Hover the mouse pointer I-bar over "PARAMETER-LEN" and the
debugger displays the value of that item.
34 Chapter 2. A Quick Tour

Watch Data Window


The Watch Data window displays a list of items with their values.

Figure 2.15. The Watch Data window.

To add an item to the Watch Data window, right click on the item name, and select
"Add Watch Data" from the pop-up menu.
Alternatively display the Data dialog, enter the data name and click on the Watch
button.
To edit an item in the Watch Data window, just click on the value and enter the new
value.
Debug, Data Dialog
The Debug, Data dialog lets you:
• Enter a data name, including names of items in any loaded program.
• For recursive programs you can specify the call level at which to view the data
item.
• View the value of the item in character or hexadecimal formats.
• Edit the value.
• Add the item to the Watch Data window.
To display the Debug, Data dialog select Data from the Debug menu.

F. Tracing the Execution Path


The debugger can record the execution path so that you can check how execution
reached a particular point.
To start the debugger recording the execution path select Trace from the Debug
menu.
You can then run the code to a breakpoint or to the point at which a condition is
satisfied.

The Backward Trace functions (Previous Statement , Next Statement ,


Previous Procedure , Next Procedure ) help you explore the route that
execution took to reach that point.
Use View, Toolbars, to display the Backward Trace toolbar.

G. Using the Recording and Playback Functions.


The debugger lets you record complete debugging sessions or parts of debugging
sections. This can let you automate repetitive tasks or testing procedures.
Chapter 2. A Quick Tour 35

The following instructions record and playback a very simple sequence but they
demonstrate the principles required to set up much more complex debug recordings.
We will record some steps, and play the recorded file while recording a new file.
1. If you have been debugging using Sample5, select Continue, Redebug to
initialize the debugging session. If you have not been debugging Sample5 start
the debugger with Sample5.
2. From the Option menu select Output Log.
The debugger displays the Output Log dialog:

Figure 2.16. The Output Log dialog.

The History file is the file to which the debugger writes a record of the
operations performed with the results of those operations.
3. Enter "Recording1" for the name of the history file.
4. Click on the Start button to start recording.
5. Click on the Close button to close the Output Log dialog window.

6. Click on the Step Into button, , two times to execute two statements.
7. Right click on the data name "WORK-FILE-NAME" and select Data from the pop-
up menu.
The debugger displays the Data dialog.
There is nothing in WORK-FILE-NAME - it is all spaces (which you can check by
switching to Hexadecimal - an ASCII space is "20" in hex).
8. Click on the Output Log button in the Data dialog.
The debugger writes information about WORK-FILE-NAME to the log file.
9. Click on the Close button to close the Data dialog.
10. From the Option menu, select Output Log, and click on the End button.
This stops the debugger logging each operation.
11. Click on the Close button to close the Output Log dialog.
So, to recap, you have recorded stepping two statements, and checking the value of
WORK-FILE-NAME.

12. Now open the Recording1.log file by starting Windows Explorer (right click on
the Start button in the Windows task bar, and select Explore from the pop-up
menu) and navigating to the folder:
C:\Program Files\Fujitsu NetCOBOL for Windows\Samples\cobol\Sample05
36 Chapter 2. A Quick Tour

Then double click on the file "Recording1.log" to bring up the file in the COBOL
Editor.
Your recording should look something like this:

2.17. A Debugger log file.

Notice that the log file consists essentially of two types of lines - debug commands or
operations (ENV, RUNTO, LIST), and comment lines starting with ";". The comments
describe the commands and display results of the commands. Because the results
are output as comments you can use the log file as input to the debugger - it ignores
the comments and executes the commands.

So now we will execute the log file.


13. Close the Editor and return to the debugger by selecting "sample5 - COBOL
Debugger" from the Windows task bar.
14. Start another output log by selecting Option, Output Log from the menu bar,
and entering "Recording2" as the History file name. Click on the Start button,
then Close the Output Log window.
By writing an output log when playing back debugger commands you can later
inspect the results of those commands.
15. From the Option menu select Automatic Debugging.
The debugger displays the Automatic Debugging dialog.
Chapter 2. A Quick Tour 37

Figure 2.18. The “Automatic Debugging” dialog.

The command file is any file containing debug commands. We will use the first
log file we created.
16. Click on the Browse button to display a list of available command files.
The debugger displays all files with a .log extension.
If necessary navigate to the folder containing your log file.
17. Select Recording1.log and click on Open.
You return to the Automatic Debugging dialog with the file name in the
Command file entry field.
18. Click on OK to run the command file.
The debugger displays an information message that it is ignoring the DBTR
option in the command file. (The ENV, DBTR option starts history files, and is
not supported when running command files - we'd normally edit the log file to
remove this option.)
19. Respond OK to the message.
The debugger executes the commands in the Recording1.log file (two steps and
one check of data item contents).
20. Stop writing to the Recording2 output log by selecting Option, Output Log from
the menu bar, and clicking on End.
21. Now open the Recording2.log file by returning to Windows Explorer (select
Exploring from the Windows task bar) and double clicking on the Recording2.log
file to bring it up in the COBOL Editor.
38 Chapter 2. A Quick Tour

Your recording should look like this:

Figure 2.19. Log file resulting from playing back some recorded operations.

This has a couple of differences from Recording1.log:


- First it shows the Recording1.log file being run (the AUTORUN command).
- Second, the results of each of the commands (RUNTO, and LIST) are different
because the commands were executed at a different point in the program.
You can see how, in a few steps, long or highly repetitive debug operations can be
automated using the logging (recording) and automatic debugging (playback)
features.
The full set of debug commands is documented in the debugger help system in the
Reference, Line Commands section. This provides you with the option of coding
sequences of debug commands using a text editor, or of generating command
sequences automatically.

H. Debugging with Visual Basic


NetCOBOL is designed to work well with other languages. A good demonstration of
this ability is to see the COBOL and Visual Basic debuggers working side by side. The
following steps take you through a simple program made up of Visual Basic code
calling a COBOL DLL. They give you all the information necessary to set up a dual
language debugging session.
The example assumes that you have Microsoft Visual Basic 5.0 or later installed.
Chapter 2. A Quick Tour 39

Setup the Environment Variable to Invoke the Debugger


Because, in this example, Visual Basic is going to be calling COBOL, you need to
ensure that an environment variable (@CBR_ATTACH_TOOL) is set so that the
COBOL Debugger is started when the COBOL code is invoked.
1. From COBOL Project Manager’s Tools menu select “Run-time Environment Setup
Tool”.
2. Open the file COBOL85.CBR in the Sample13 folder. If it doesn’t exist, create it
by navigating to the folder and entering “COBOL85.CBR” as the file to open.
3. Select the Section tab.
4. Select SAMPLE13 from the Section drop-down list.
If there is nothing in the list enter “SAMPLE13”.
5. If @CBR_ATTACH_TOOL=TEST is not displayed in the Section list box:
Select @CBR_ATTACH_TOOL from the Variable Name drop-down list.
Enter “TEST” into the Variable Value field.
Click on Set to add the variable to the section list.

Figure 2.20. The Run-time Environment Setup Tool.

6. Click on Apply to write this information to the COBOL85.CBR file.


Exit from the tool.
40 Chapter 2. A Quick Tour

Start Debugging from Visual Basic


7. Start Visual Basic. (The instructions are based on Visual Basic 6.0, if you are
using a different version, you may need to adapt them to your version.)
8. Open the Visual Basic project:
C:\Program Files\Fujitsu NetCOBOL for Windows\Samples\cobol\Sample13\
sample13.vbp
9. Look at the form and code of this sample to get a feel for what it does.

Figure 2.21. The Sample 13 form in Visual Basic.

The form accepts two numbers and displays the result of multiplying those numbers
when the "=" button (Command1) is pressed.
The Command1 code invokes the Sample13 DLL created using NetCOBOL:
Chapter 2. A Quick Tour 41

Figure 2.22. Visual Basic code for Sample 13.

There are two other pieces of code you should note, although we will not discuss
them further in this guide. They are the calls to JMPCINT2 and JMPCINT3. These
calls initialize and close the COBOL run-time system.
10. Execute the Visual Basic code by repeating the Debug, Step Into function
(available on the Visual Basic Debug menu, and the Debug toolbar).
11. When the Sample13 dialog displays, enter two numbers and click on the "="
button.
12. Continue stepping up to, and into, the statement:
Call SAMPLE13 (ar%, br%, C)
Debug the COBOL Code
13. Control passes to the COBOL code. The runtime system recognizes that the
COBOL debugger should be invoked (because the @CBR_ATTACH_TOOL
environment variable is set) so starts the debugger.
The debugger displays the Start Debugging window.
42 Chapter 2. A Quick Tour

Figure 2.23. The Start Debugging dialog when invoked from Visual Basic.

14. The debugger displays "VB6.EXE" as the application being executed. This is the
application to which the debugger attaches. You need to be aware of this
because you cannot close the debugger until you close Visual Basic.
You need to confirm two locations to the debugger: on the Source and
Debugging Information tabs.
15. Select the Source tab and click on the Browse button by the "Source file storage
folders" entry field.
The debugger displays the "Browse for Folder" dialog.
16. From the folder list select
C:\Program Files\Fujitsu NetCOBOL for Windows\Samples\cobol\sample13
17. Select the Debugging Information tab and click on the Browse button.
The debugger displays the "Browse for Folder" dialog.
18. Again select:
C:\Program Files\Fujitsu NetCOBOL for Windows\Samples\cobol\Sample13
19. You have now ensured that the debugger can find the COBOL source files and
debugging information.
20. Click on OK to display the source in the COBOL debugger.
Chapter 2. A Quick Tour 43

Figure 2.24. Sample 13 in the Debugger

You can use all the COBOL debugging functions to debug this program. We will
simply step through its three statements.

21. On the COBOL debugger toolbar, click the Step Into button, , three times.
The debugger steps through the code. When the EXIT PROGRAM statement is
executed, control returns to the Visual Basic debugger (the Visual Basic button
on the task bar may flash to indicate you need to switch control to that
window).
22. Continue stepping through the Visual Basic code, and select the Sample 13
window to see the result displayed.
Through this simple example you have seen how complex mixed Visual Basic and
COBOL applications can be debugged effectively.

Using Configuration Management


The Enterprise edition of NetCOBOL contains the PowerGEM configuration
management utility. COBOL Project Manager provides access to the configuration
management functions from the CM menu. If you want to check out these functions
you first need to set up a PowerGEM library. The instructions below guide you
through that process.
44 Chapter 2. A Quick Tour

A. Overview of PowerGEM Structures


Before setting things up so that you can check out the configuration management
functions, it will help you to have a brief overview of how PowerGEM works.
PowerGEM stores the files it is controlling in PowerGEM libraries. You specify that
certain folders are PowerGEM "workspaces" by mapping a PowerGEM library to the
folders (a single library can be mapped to multiple folders). You can specify that only
certain members of a library are mapped to a given workspace. Once a folder is
specified as a workspace, PowerGEM controls files within that workspace that match
(mapped) members of the PowerGEM library. Note that PowerGEM only controls the
files that you have added to the library; it does not control files that are not in the
PowerGEM library, so you can select which files in your workspace are controlled.
The process of setting up configuration management for an application is:
1. Create a PowerGEM library (or determine which existing library you are going to
use).
2. Map a PowerGEM library to a folder, making the folder a PowerGEM workspace.
3. Add files from the workspace to the PowerGEM library.
These steps are detailed in the following paragraphs.

B. Setting Up a PowerGEM Library


Create a PowerGEM Library
1. From the Windows task bar, select Start, Programs, NetCOBOL for Windows
V8.0, PowerGEM Plus.
This brings up the PowerGEM Development Manager utility. This utility provides
the functions for maintaining your configuration management system.
The window looks similar to the Windows Explorer window, with the folder
structure displayed in the left-hand pane, and the files displayed in the right-
hand pane. The differences are in the functions provided and in the icons
displayed to represent files that are being controlled by the configuration
management system.
Chapter 2. A Quick Tour 45

We'll work with Sample05 so click on the plus icons, , in the left-hand pane to
navigate to:
C:\Program Files\Fujitsu NetCOBOL for Windows\Samples\cobol\Sample05

Figure 2.25. The PowerGEM Development Manager.

Note: The controlling part of PowerGEM runs as a background task so you will see a
second task, PowerGEM Plus, on the Windows task bar. This continues to run while
tools that use it are running. However, it does not have an interface so selecting the
button on the task bar has no effect.
2. From the CM (Configuration Management) menu select Utility.
Development Manager invokes the CM Utility dialog.
46 Chapter 2. A Quick Tour

Figure 2.26. The Configuration Management Utility dialog

3. Click on the "Create GEM Library" button.


CM displays the Create GEM Library dialog.

Figure 2.27. The “Create Gem Library” dialog.

The Folder name indicates where the GEM library will be created.
The Browse button actually sets the Folder Name, not the Library Name as its
position might suggest.
4. We will create our GEM library in the C:\Program Files\Fujitsu NetCOBOL for
Windows\PGEM" folder, so click on Browse, navigate to the folder:
C:\Program Files\Fujitsu NetCOBOL for Windows\PGEM ,
and click on Open to select the folder.
Chapter 2. A Quick Tour 47

5. Enter "Samples" in the Library Name field.


The Create GEM Library dialog should look like this:

Figure 2.28. The Create GEM Library dialog after selecting the folder name and
entering the library name.

6. Click on OK.
PowerGEM creates a library called Samples.
Note: PowerGEM uses hidden folders for storing the library data. So you will not
see the Samples library in the PGEM folder unless you have configured your
listing program (e.g. Windows Explorer or Development Manager) to display
hidden files.
7. Close the CM Utility dialog by clicking on the Cancel button.
48 Chapter 2. A Quick Tour

Map the PowerGEM Library to the Sample05 Folder


8. Now map the Samples PowerGEM library to the sample05 folder by selecting the
sample05 folder in the left-hand pane of the Development Manager window and
selecting Map from the CM menu.
CM displays the Map (Local) dialog.

Figure 2.29. The Configuration Management “Map (Local)” dialog

Workspace Name is the name of the folder selected when the Map function is
invoked. The folder becomes a workspace at the completion of this function.
Folder Name and Library Name specify the PowerGEM library to be mapped.
These can be set by the Browse button.
The Member Name field can be used when existing libraries are mapped to new
folders. You can specify which members to map to the folder. As we do not yet
have any members in the Samples library we won't use this field.
Chapter 2. A Quick Tour 49

9. Click on Browse and select the Samples library from the PGEM folder. (The
"Select Library or Member" dialog uses the icon to indicate PowerGEM
libraries).

Figure 2.30. The “Select Library or Member” dialog.

10. Click Open to return to the Map (Local) dialog.


11. Click OK to map the Sample library to the sample05 folder, making sample05 a
PowerGEM workspace.

Add Files from the Workspace to the PowerGEM Library


Because sample05 contains free format COBOL source code we need to change the
default CM setup that expects files with .COB and .CBL extensions to have sequence
numbers in columns 1-6.
12. From the CM menu, select Setup Environment.
CM displays the Setup CM Environment dialog.
13. Click on Setup Environment.
CM displays the Setup Environment dialog:
50 Chapter 2. A Quick Tour

Figure 2.31. Configuration Management “Set Environment” dialog.

14. From the Extension drop-down list select CBL.


15. In the Sequence Number group box, select the None selection button.
16. Click Set in the top right of the dialog.
*** Do not omit this step otherwise your change will not ***
*** take effect! ***
17. From the Extension drop-down list select COB, select the None selection button,
and click on Set again.
18. Click OK, then Cancel the Setup CM Environment dialog.

You are now ready to add files to the PowerGEM library. We will just add the
COB and CBL files. For a real project you would add all the files you wanted to
control including object files as well as source files.
19. In the right-hand pane of the Development Manager window select the following
files by keeping the Ctrl key depressed and clicking on each one in turn:
printprc.cob
s_rec.cbl
sample5.cob
20. From the CM menu select Add.
The selected files are added to the Samples library.
The file icons change from white, , to blue, .
You now have a PowerGEM library and workspace that you can use to review
the CM functions in Project Manager.
If you want to delve into Development Manager more deeply, see the topic
"Understanding PowerGEM Development Manager" in “Appendix B. Tips”.
Chapter 2. A Quick Tour 51

C. Using the Project Manager Configuration Management Functions


The following steps give you an overview of how configuration management works
within Project Manager.
1. If it is not already running, start COBOL Project Manager (from the Windows
task bar select Start, Programs, NetCOBOL for Windows V8.0, COBOL Project
Manager).
2. Open Sample05.prj and expand all the nodes so the window looks like this:

Figure 2.32. Sample05.prj fully expanded in COBOL Project Manager.

3. From the CM menu select CM Status.


Project Manager displays a submenu:

The options on this submenu determine whether and how configuration


management information is displayed in the project tree.
None - means no CM information is displayed
Add - means that the project tree icons overlay the normal project tree
icons with miniature CM status icons
Status - means that the normal project tree icons for controlled files are
replaced with CM status icons.
4. First select Status from the submenu.
The .COB and .CBL file icons change to plain blue sheets, .
52 Chapter 2. A Quick Tour

We will now see how a checked-out file is displayed.


5. Select SAMPLE5.COB in the project tree.
6. From the CM menu select, Check Out.
The icon by SAMPLE5.COB changes to a pencil (indicating that the file can be
edited).

Figure 2.33. COBOL Project Manager displaying library control and check-out icons.

7. From the CM menu select CM Status, Add.


Project Manager changes the icons for controlled files to be combination images
of the normal icon and the CM status icon: , .

Now check the difference between a checked-out file and one that is not
checked-out.
8. Double-click on SAMPLE5.COB to invoke the editor.
SAMPLE5.COB is displayed in the editor.
9. Press a few character keys.
Text is entered in the file.
10. From the File menu select Exit Editor and respond No to the "Do you want to
save?" dialog, so that your arbitrary changes are not saved.
11. Double-click on S_REC.CBL to invoke the editor.
S_REC.CBL is displayed in the editor.
12. Press a few character keys.
Nothing happens - because the file is not checked out you are not allowed to
make changes to it.
Chapter 2. A Quick Tour 53

13. From the File menu select Exit Editor.


14. Select SAMPLE5.COB in the project tree.
15. From the CM menu, select Cancel Check Out.
SAMPLE5.COB returns to the not-checked-out status.
The other functions on the CM menu are:
Add: Adds a file to the PowerGEM library.
Check In: Checks a file into the configuration management system after
changes have been made.
Retrieve: Fetches a copy of the file from the PowerGEM library without
checking it out.
Specify Execution Options:
When this item is checked CM displays a dialog box whenever the
Add, Check In, Check Out, Retrieve and Cancel Check Out functions
are used. The dialog allows for the entry of details such as
passwords.
Path String: Determines whether full path names or just file names are displayed
in the project tree.

Working with OO COBOL

A. Brief Introduction to OO COBOL


The "NetCOBOL User's Guide" contains a full introduction to the Fujitsu OO COBOL
implementation. The following comments are intended for those who only want the
minimum of information before looking at an actual OO COBOL application. The
comments assume a familiarity with OO concepts and terminology.
Fujitsu OO COBOL provides: class/object models, information hiding (data
encapsulation, object properties), modularization (methods, including prototype
methods), single and multiple inheritance, context-dependent functions
(polymorphism), conformance checking, static and dynamic binding, garbage
collection, and exception handling.
All these features are provided by very natural extensions to the COBOL language.
In OO COBOL:
• Classes are made up of a Factory object and Class objects.
• The Factory object contains data and methods required either for creating and
maintaining objects, or for objects that only require one instance per class.
• The Class objects contain the data and methods that define the entity that the
class models.
• Methods can have their own local data as well as procedure code.
• Methods are invoked using the INVOKE statement.
• Each of the main constructs "CLASS-ID", "FACTORY", "OBJECT" and "METHOD-
ID" are defined using appropriate combinations of the four standard COBOL
divisions (IDENTIFICATION, ENVIRONMENT, DATA and PROCEDURE).
54 Chapter 2. A Quick Tour

• Class definitions are stored in REPOSITORIES.


• Non-OO COBOL applications can invoke OO programs and OO code can call non-
OO programs.

B. Guided Tour of an OO COBOL Application


This tour uses one of the OO COBOL samples, Sample18, to give you quick views of
the support for OO COBOL within NetCOBOL. You will look at Sample18 in the
Project Manager, in the source editor, in the Project Browser and in the Class
Browser.
First we'll look at Sample18 within the COBOL Project Manager. Remember that
Project Manager presents an application build structure along with access to most of
the NetCOBOL development tools.
1. Start COBOL Project Manager (from the Windows task bar select Start,
Programs, Net COBOL for Windows, COBOL Project Manager).
2. From the Project Manager menu bar select File, Open and navigate to the:
C:\Program Files\Fujitsu NetCOBOL for Windows\Samples\cobol\Sample18
folder.
3. Select and Open sample18.prj.
4. Expand the MAIN.EXE, Cobol Source File, ALLMEM.COB, BONU_MAN.COB,
ADDRESS.COB, and MAIN.COB nodes by clicking on the plus, , icons.
The sample18 project tree looks like this:
Chapter 2. A Quick Tour 55

Figure 2.34. Sample 18 expanded in COBOL Project Manager.

The tree is similar to those for standard COBOL applications. It shows that
MAIN.EXE is built from several COBOL source files (ALLMEM.COB,
BONU_MEM.COB, etc.) and uses .DLLs defined in COLLECT.LIB.
The tree is different from those for standard COBOL applications in that
repositories have been added to the tree. These nodes show the repositories
that are either created by a particular source file (target repositories) or that are
referenced by the source file (dependent repositories).
Once you specify an application's source structure you can have Project
Manager figure out the repository information by using the "Repository File
Search" function on Project Manager's Edit menu.
The repository information helps identify the function of the different source
files. For example:
MAIN.COB is a standard COBOL program that uses an OO class system for its
functions. It therefore references several different classes.
ADDRESS.COB is a class definition, hence it has a target repository. It only
inherits from the FJBASE root class so it does not have any dependent
repositories.
ALLMEM.COB has a target repository and is therefore a class definition. It
references an item in the ADDRESS class so has a dependent repository.

Now we'll take a quick look at some OO COBOL source.


56 Chapter 2. A Quick Tour

5. Double-click on ALLMEM.COB.
6. Scroll past the initial (green) comments to the (blue and black) COBOL source.

Figure 2.35. Sample 18 source code.

Notice:
- The CLASS-ID paragraph. This is actually part of the IDENTIFICATION DIVISION
but the IDENTIFICATION DIVISION header is optional so you will often see a new
code structure starting with its identifying paragraph (CLASS-ID, FACTORY, OBJECT,
or METHOD-ID).
- The INHERITS clause in the CLASS-ID paragraph indicating the class this class
inherits from - in this case the system base class FJBASE.
- The methods starting with METHOD-ID, ending with END METHOD, containing data
and procedure code.
- At the end of the source file, the END OBJECT and END CLASS, delimiters.
Although some new elements have been introduced, the general structure and layout
Chapter 2. A Quick Tour 57

should be familiar to COBOL programmers - particularly those who have experience


with nested programs.
7. Close the editor and return to COBOL Project Manager.
When developing an OO COBOL application, people within a project group will
likely want to review relationships between source and class elements, without
having to release the classes to the whole development organization. The
Project Browser tool has been provided for this purpose.
8. First indicate that a project information database should be created, by selecting
Properties from the menu bar and confirming that "Create Project Database" is
checked on the Properties dialog, as shown in Figure 2.35.

Figure 2.36. “Create Project Database” checked on the Properties dialog.

If it is not checked, click on the "Create Project Database" check box.


9. Build the project information database by right-clicking on SAMPLE18.PRJ in the
project tree, and selecting Build from the pop-up menu.
Assuming that no other updates have been made to the project, Project
Manager does no compiles or links, but just displays a "Terminating database
file creation" message.
58 Chapter 2. A Quick Tour

10. You can invoke the Project Browser by selecting, Tools, Project Browser from
the Project Manager menu bar.
Project Manager invokes the Project Browser tool with the SAMPLE18 project
already loaded:

Figure 2.37. The Project Browser.

The left pane provides a tree view of the classes being used by the project.
The right pane provides details about the item selected in the left pane.
11. Expand one of the nodes in the left-hand pane, for example the ALLMEMBER-
CLASS.
The Project Browser expands the tree to show classes that inherit from this
class, referenced classes, methods, properties and inherited methods. See the
Project Browser Help for a description of the different icons. Sharp icons denote
items defined within the selected class, faded icons denote items defined
outside the class (generally these are inherited items).
12. Select different items in the left-hand pane and note the information that is
presented in the right-hand pane, such as arguments required for methods and
data descriptions of properties.

Notice that the Project Browser includes non-OO elements, like the MAIN
program - an essential part of the project, but not part of the OO system.

Now we'll switch to the Class Browser for a slightly different view on this
application.
13. Close the Project Browser and return to the COBOL Project Manager, where we
will create a class information database.
Chapter 2. A Quick Tour 59

A class information database combines information from several class


repositories for display by the Class Browser.
14. From the Project Manager menu bar, select Tools, Class Database, Options.
Project Manager displays the dialog titled "Set the Class Database Generation
Options". Its purpose is for you to list all the folders containing repositories that
you wish to include in your class information database, and for you to specify
the name and location for the class information database.
We will only specify the Sample18 folder for source repositories.
15. Click on the Add button in the "Repository folder" group box.
Project Manager displays the "Browse for Folder" dialog.
(Note – this dialog starts browsing at the desktop as it assumes that your
repositories will often be located on a central server.)
16. Navigate to the:
C:\Program Files\Fujitsu NetCOBOL for Windows\Samples\cobol\Sample18
folder, select it, and click OK.
The sample18 folder is added to the list.
17. Click on the Browse button in the "Class database file" group box and navigate
to the:
C:\Program Files\Fujitsu NetCOBOL for Windows\Samples\cobol\Sample18
folder. Then enter "samples" in the "File name" entry field of the Open window,
and click on the Open button.
A class information database called "samples.cid" will be created in the
sample18 folder - normally you would place this file in a central location
accessible to all developers.
18. Click OK to close the "Set the Class Database Generation Options" dialog.
19. Create the class information database by selecting Tools, Class Database, Create
from the Project Manager menu bar.
Project Manager creates the class information database and displays a message
saying "Class database created successfully".
20. You can now invoke the Class Browser by selecting Tools, Class Browser from
the Project Manager menu bar.
Project Manager invokes the Class Browser, displaying information from the
Samples class information database.
60 Chapter 2. A Quick Tour

Figure 2.38. The Class Browser.

The Class Browser has a similar appearance to the Project Browser with the
following differences:
- Class Browser only displays OO class information.
- Classes within class browser can come from many different projects and
application areas.
- Class Browser does not display source file name information.
- You can select whether Class Browser should expand classes up or down the
class hierarchy.
21. Experiment with browsing the Sample18 classes. Note how the Class Browser
helps you understand the relationships between the classes and the interfaces
available in each class.
For more information on OO COBOL consult chapters 14 - 17 in the "NetCOBOL
User's Guide".

Invoking COBOL Project Manager from Windows Explorer


COBOL Project Manager can be invoked not only from the Start, Programs menu (as
discussed previously) but also from Windows Explorer.
You do this by double clicking the mouse on a project file and the COBOL Project
Manager starts, displaying the project you selected.
For example, double click on SAMPLE4.PRJ in the Windows Explorer window.
Chapter 2. A Quick Tour 61

Figure 2.39. Selecting a project from Windows Explorer.

This starts COBOL Project Manager with SAMPLE4.PRJ loaded.

Converting Version 3 Projects


If you open a P-STAFF Version 3 project with NetCOBOL or Fujitsu COBOL (Version 4
or later), COBOL Project Manager will convert the project into the current project
format.
This is true whether you open the project from COBOL Project Manager or from
Windows Explorer.

Other Options
COBOL Project Manager provides many other functions such as:
• Invoking various tools individually from the Tools menu including the compiler,
linker, debugger and editor.
• Integrating pre-compilers into your system.
• Working with CORBA objects (Object Director).
• Configuring certain parts of your environment.
For details of these functions see the on-line help for COBOL Project Manager and
the “NetCOBOL User’s Guide”.
62 Chapter 2. A Quick Tour
Chapter 3. Developing GUI Applications
This chapter sets the context for developing GUI applications in COBOL. It is
descriptive in nature, providing a background for those who like to know more
details.

A Brief History
It is generally agreed that COBOL programs make up anywhere from 50-75% of the
world’s current business applications. Today COBOL programmers need a modern,
powerful, and easy to use development environment that fully exploits the Microsoft
Windows family of operating systems for both development and production.
The Microsoft Windows family of operating systems has become the preferred end
user platform for the vast majority of PC users. A number of pitfalls arose, however,
as traditional COBOL programmers from the mainframe world attempted to embrace
this exciting new platform.
Primarily, the intense time and complexity of low level Windows API (application
programming interface) programming prevents most developers from creating
sophisticated graphical user interfaces using traditional 3GL (3rd generation
language) compilers and tools.
Two approaches have been developed to deal with this problem. The first is to
extend the COBOL language itself to encompass GUI capability directly in the
language. This approach offers the advantage of using a single COBOL development
environment, and keeping the entire application in native COBOL. Fujitsu’s flagship
PowerCOBOL product is the best example of such an approach.
The second approach is to separate the user interface from the actual COBOL
application and have it managed by a wholly separate facility. Some COBOL vendors
have attempted to deliver such a combination, but these have fallen short for a
variety of reasons. Primarily, these environments have proven to be highly unstable,
lacking in many features, and very proprietary in nature. Additionally, these
environments have been in a constant state of flux as vendors attempted to deliver
better stability and a wider array of functionality. The results have typically been less
than successful for developers.
In recognizing these challenges, Fujitsu has developed the world’s premier COBOL
GUI and client/server application development environment -- Fujitsu’s flagship
PowerCOBOL product line.
Fujitsu has also recognized the need for a world class COBOL development product
that will integrate, coexist and exploit Microsoft’s Visual Basic development
environment.
If you want to develop full-fledged GUI applications, you should explore Fujitsu’s
PowerCOBOL product. If you have the Professional or Enterprise editions of
NetCOBOL, you have probably already installed the product. If you have NetCOBOL
Standard edition, you can check the NetCOBOL web site (www.netcobol.com) for
more details. You may additionally use Microsoft’s Visual Basic (version 4.0 or later)
with this product to develop GUI applications.
Visual Basic was an attempt to bring out a 4GL-like development environment that
would allow programmers and even non-programmers to develop GUI applications in
64 Chapter 1. Developing GUI Applications

the Windows environment. Users could visually ‘paint’ the user interface, and then
create the rest of the application by writing small snippets of code using a
proprietary scripting language that was loosely based upon the BASIC programming
language.
Initially, Visual Basic was lacking for serious business application development.
Microsoft has continued to evolve Visual Basic over the years into a serious
development environment. Today it is a strong development tool for a wide number
of professional developers who want to rapidly create GUI applications for the
Windows family of platforms.
For the users of COBOL applications, however, Visual Basic presents a number of
substantial challenges:
• Having no relationship to COBOL, Visual Basic presents an alien development
environment to COBOL programmers. They must learn an entirely new
development methodology and language.
• Visual Basic does not provide all of the facilities found in the COBOL language.
• Prior to NetCOBOL, Visual Basic could not easily be inter-mixed with COBOL
programs to combine the best of both worlds, and to allow the re-use of existing
COBOL programs.
Many COBOL programmers have requirements to re-host applications (move COBOL
applications from another platform such as the mainframe) to the PC environment.
At the same time they want to re-design traditional text-based interfaces to take
advantage of GUI.
While some COBOL vendors attempted to provide ‘bridges’ to allow Visual Basic to
intermingle with COBOL, these approaches typically fell short.
One of the major inhibiting factors in inter-mixing other vendor’s COBOL with Visual
Basic related to the fact that each had it own separate and complex run-time system.
Mixing two very proprietary run-time systems is a substantial undertaking. Most
legacy run-time systems were architected and developed by their respective vendors
long before PC’s, Windows and GUI’s were even invented.

Fujitsu Addresses the COBOL Need


Over the years Microsoft has recognized the need to support COBOL applications and
has made multiple attempts to provide products to the COBOL community. Their
original COBOL compiler was replaced by Micro Focus’ COBOL/2 product in 1988. The
COBOL/2 product suffered from many of the aforementioned pitfalls, and Microsoft
withdrew it from the market in June of 1993.
Fujitsu recognized this dilemma and set out to build a next generation COBOL
development environment from the ground up. A special emphasis was placed upon
the design of the run- time system to alleviate the above noted problems.
Additionally, Fujitsu developed a tool to aid in the conversion of COBOL/2
applications to NetCOBOL (while COBOL is a standard language, COBOL/2 contained
a significant number of proprietary extensions).
Chapter 1. Developing GUI Applications 65

NetCOBOL
NetCOBOL is a complete COBOL development environment that allows you to create
standalone COBOL applications and/or COBOL components for use with Microsoft
Visual Basic and Visual C++ applications.
The COBOL Project Manager is a 32-bit project-oriented development environment
that provides integrated access to an editor, compiler, interactive debugger,
execution environment, and other supporting tools. A sophisticated data file editor is
also included. It supports all COBOL data file types, and provides editing, conversion,
printing, sorting, reorganizing and recovery capabilities.
For previous users of Micro Focus’ or Microsoft’s COBOL/2, dialect conversion
products are available to aid in converting COBOL programs using some of the Micro
Focus proprietary extensions to run under NetCOBOL. See the www.adtools.com
Web site for more details.
Fujitsu PowerBSORT OCX is also included. PowerBSORT OCX is callable from any
Windows application that supports OCX’s (for example, Microsoft Visual Basic).
PowerBSORT OCX online help is provided along with sample applications for Visual
Basic 4.0 and 5.0. The online help and sample files are automatically installed along
with PowerBSORT OCX.

Event-driven Programming
In order to begin developing GUI COBOL applications in the Windows environments,
it is important to understand the concept of event -driven programming.
While the term ‘event driven’ may be somewhat new in the computer industry, the
concept has been around for a long time, and chances are that if you are a COBOL
programmer, you already understand it.
When COBOL programmers first began developing mainframe-based applications
that interacted with users (e.g. non-batch style applications), they were constrained
to simple line-oriented ACCEPT/DISPLAY syntax.
Later, on-line transaction monitoring systems such as IBM’s CICS and IMS/DC
enhanced this capability to handle full screen-oriented interaction (as opposed to line
oriented). Some PC COBOL vendors later enhanced the COBOL ACCEPT/DISPLAY
verbs in non-standard ways to provide full screen interaction.
While one could argue that none of these techniques qualified as event-driven
programming, they were in fact, a primitive form of this approach.
Event-driven programming breaks the user interface portion of a GUI application
(screen I/O, keyboard I/O, mouse I/O, and a few other possibilities), into a series of
possible events.
These events include user actions such as pressing a key on the keyboard, moving
the mouse, clicking the mouse on a screen object such as a button, holding the
mouse button down and dragging the mouse to move a window, and so forth.
Writing a proper Windows GUI application entails creating all of the potential
windows and objects that the user may interact with (including output objects as
66 Chapter 1. Developing GUI Applications

well), determining all of the possible events that may occur, and then creating a link
between each potential event and your application code to handle the event.
What you aim to end up with is an application that registers its user interface with
Windows (in much the same approach as CICS and IMS/DC applications register
themselves in the mainframe world). Once the application is registered, Windows
recognizes events and sends event driven messages to the application for resolution.
The registration and event handling processes are based upon the Windows message
queue paradigm.

The Windows Message Queue


The Windows message queue is an exceptionally complex topic, and will be
discussed only briefly to give you a basic understanding.
Much like CICS or IMS/DC (via VTAM) on a mainframe, Windows controls all input
from the screen, mouse and keyboard. It must then implement a technique to
manage all of this and ensure that any event that takes place is routed to the proper
application. For applications that wish to update system I/O resources such as the
screen, Windows also needs a mechanism to enable applications to alert it
(Windows) to do so.
Windows provides these services by implementing a message queue. Every Windows
application must register itself with Windows when it activates. As the user interacts
with the system, messages are routed into each application as appropriate. Because
Windows supports multiple applications running concurrently, this proves to be a
very busy part of the operating system.
This means that any Windows application registers itself with Windows, and then
goes into a recurring ‘message loop’ - simply reading in messages sent to it by the
operating system and reacting as appropriate. This continues until the application
receives whatever it has defined to be a ‘terminate application’ type of message and
then it terminates itself.
It’s important to understand that an application can easily receive thousands of
Windows event messages in a short period of time. For example, when a user moves
the mouse around the screen, multiple messages are generated to alert the
application whose window(s) the mouse passes over. These messages not only alert
the application that the mouse has moved over the application’s window, but
additionally provide information such as the exact location (x, y coordinates) where
the mouse moved - even though the user did not press any of the mouse buttons!
If your application does not contain code to handle a certain message then it may
lock up or be abended. Fortunately, a common programming technique is to provide
a catchall routine that simply ignores all messages other than the ones you’ve
defined.
Hopefully, you can begin to envision the Windows application environment where
messages are being sent to applications as the user interacts with the system. The
applications perform actions based upon the messages received and dispatch
messages back to Windows to perform user interface operations (for example, close
a window, create a new window, update the screen with the new data being passed
along, etc.).
In a low level Windows application, developers are forced to write extensive amounts
of code to deal with all of the possible events that may occur, manage the internal
message loop, and take care of a great deal of tedious work. This work may even
Chapter 1. Developing GUI Applications 67

include being forced to re-paint the screen because another application’s window
was placed on top of your application’s window. (Windows does not automatically
keep track of the layering of application windows and will not automatically restore
the prior contents of a window that was briefly overlaid.)

How PowerCOBOL and Visual Basic Simplify Application


Development
One of the goals of Fujitsu’s PowerCOBOL and Microsoft’s Visual Basic is to
effectively hide from the developer the tedious task of managing the Windows
message queue and an application’s message loop.
Using PowerCOBOL or Visual Basic, a developer need only paint the application’s
windows and contained objects (for example, buttons, list boxes, text fields, etc.).
PowerCOBOL users may program these windows in intuitive COBOL code, while
Visual Basic developers define (in a high level scripting language) how they are to
interact with the user and the application. The developer then decides which
potential events he or she cares about (for example, a button being pushed by a
mouse click), and writes the code to handle each such event.
PowerCOBOL actually generates a great deal of the COBOL source code for GUI
events and places it automatically into the COBOL program.
This approach frees the programmer from the tedious nature of low level Windows
API message queue programming in order to concentrate on higher level aspects of
the application.
Both PowerCOBOL and Visual Basic generate code for the application to handle any
potential events that the developer forgets or simply does not care to define event
code for.
Because some of this code can be quite tedious and complex (for example, re-sizing
a window and re-positioning all of its objects), both PowerCOBOL and Visual Basic
become great allies to the application developer who needs to field solid, fully
functional GUI applications.
When developing these types of applications using Visual Basic, users must be very
familiar with Visual Basic’s scripting language, which is very extensive and can be
quite complex at times.
Additionally, developers sometimes run into development scenarios where Visual
Basic may not provide a certain capability or provides it in a somewhat tedious
manner versus another programming language such as ‘C’ or COBOL.
Lastly, developers often have legacy applications available that they would like to
encompass in a GUI application. These legacy applications are often written in
something other than Visual Basic script. This means that Visual Basic needs to be
able to call programs written in languages other than its own scripting language. This
is exactly where NetCOBOL comes into play.
By providing the capability to associate potential events in a Visual Basic controlled
application to COBOL programs, the development environment is greatly extended.
COBOL programmers can write sophisticated GUI applications with a minimal amount
of knowledge of Visual Basic’s scripting language. They may additionally re-use
existing COBOL code with little or no change within a Visual Basic application as well.
68 Chapter 1. Developing GUI Applications

Using NetCOBOL
Creating COBOL applications to run standalone or under Visual Basic is a flexible and
straightforward process. Developers typically follow the steps below:
• Application analysis and design.
• For GUI applications, creation and testing of the user interface using Visual
Basic’s development environment.
• Creation and testing of the COBOL component(s) of the application using the
COBOL Project Manager development environment.
• Testing and continued development of the overall application that is run under
the initial control of Visual Basic when it is a GUI application.
It is worth noting that both Visual Basic and NetCOBOL offer extensive dynamic
development environments, making it easy to move between the two when creating
and testing applications.
Additionally, both environments provide extensive development support tools to aid
the process.
NetCOBOL standalone .EXE files (executables) or .DLL files (dynamic link libraries) do
not have to be physically linked into a Visual Basic application and may be called
dynamically at run- time by Visual Basic.
Doing away with the requirement to link all of this together allows you to work off-
line on either end of the application. That is, you may work on and test the user
interface under Visual Basic, or work on the COBOL component(s) in the COBOL
Project Manager without having first gone through Visual Basic.
Because of the dynamic nature of the two development environments, you do not
necessarily have to follow the above steps in order. You could first develop and test
the COBOL components and then design the user interface in Visual Basic. The
important point is that when it comes time to execute the overall application, you
should have the interface and the COBOL components available.
Chapter 4. Working with Visual Basic and
COBOL
This chapter teaches you how to mix COBOL and Visual Basic. It starts by walking
you through your first Visual Basic COBOL application it then precedes to the details
of integrating COBOL with Visual Basic.

Creating Your First Visual Basic COBOL Application


In this section, you will learn how to create a COBOL application that uses Visual
Basic as a graphical front end.
It is assumed that you have already installed Visual Basic, version 5.0 or higher on
your machine. If you have not, please do so before continuing this section.
It is also assumed that you have gone through the Visual Basic Tutorial entitled
“Your First Visual Basic Application,” located in the “Visual Basic Programmer’s
Guide.” This will help you understand the terminology used later in this chapter.

An Overview of the Visual Basic COBOL Application


The application you are going to develop will be a simple graphical window with a
text box and two command buttons.
It will be named HICOBOL (short for "Hello from COBOL").
The text box will be assigned a text value by Visual Basic. When selected, one of the
command buttons will call your HICOBOL program and pass two parameters.
One parameter is a numeric value. The COBOL program will set this value to 100
each time, but it will be ignored by the Visual Basic part of the application (this
parameter is defined to illustrate the technique). You may enhance the application
on your own to use this value as appropriate.
The second parameter is a character string. The COBOL program will always set this
to "Hello from COBOL". When the COBOL program exits, this value will be returned
to Visual Basic and the graphical text box will be updated with this new string to
illustrate the interaction between Visual Basic and COBOL.
This will happen each time a user clicks on the command button. The second
command button will cause the graphical text box to be reset to display a different
string.

How NetCOBOL Interacts with Visual Basic


NetCOBOL was created to work well with other language environments from a run-
time perspective.
Most programming API’s (application programming interfaces) available on the
Windows platforms support a C-style programming interface. This includes not only
operating system services such as GUI interface components, but also additionally a
vast array of other programming interfaces. These include database systems,
client/server messaging and transaction systems, and even end user applications.
70 Chapter 4. Working with Visual Basic and COBOL

The arrival of NetCOBOL has brought seamless integration with operating system
services and other languages to COBOL developers. Visual Basic integration is but
one example of the excellent mixed language programming support found in
NetCOBOL.
The integration point between NetCOBOL and Visual Basic is the creation of a DLL
(dynamic link library) file from NetCOBOL.
Instead of producing a standalone executable (EXE) file within the COBOL
development environment, you instead produce a DLL file. You can think of a DLL file
as a dynamically callable COBOL subprogram, which does not have to be physically
linked to a program that calls it and may even pass data back and forth.
To create a proper DLL, the linker needs to be given the name of the DLL to be
produced, and the names to export.
Exported names are the names of entry points contained within the DLL file which
are to be made public to allow other applications to call them after the DLL has been
identified to the operating system.
You are going to create a simple DLL file called HICOBOL.DLL, which will contain a
single exported entry point - the program name. The program name entry point will
cause a calling program to enter the program at its first executable statement in the
PROCEDURE DIVISION.
Once you have written the HICOBOL.COB program and have created the
HICOBOL.DLL executable, you will exit NetCOBOL and enter Visual Basic to design
the graphical interface.
While in Visual Basic, you will create a graphical user interface (GUI) for your COBOL
application, which will call the HICOBOL.DLL program. Data will be passed back and
forth as well.
To identify the NetCOBOL DLL to the Visual Basic program, you will code three
simple Visual Basic DEFINE statements. One is for the NetCOBOL run-time load
program. Another is for the NetCOBOL run-time unload program. The final DECLARE
statement is for the HICOBOL.DLL executable you are going to create.
In order to get a NetCOBOL DLL program to work with Visual Basic, you need to first
issue a single call from Visual Basic to ensure that the COBOL run-time support is
properly loaded.
You then code a single Visual Basic statement to call the HICOBOL.DLL program and
pass data to it.
Finally, you code a call to unload the COBOL run-time support when finished.
That’s all there is to it.
From an architectural standpoint, when you develop future, more complex Visual
Basic COBOL applications you will arrange things slightly differently than in the
following example. The main reason for this is that you only need to load the
NetCOBOL run-time once at the start of your application, and issue the unload call
just before you exit your Visual Basic application.
To keep the example simple and straightforward, you are going to code all three
calls together (load the COBOL run-time, call HICOBOL, unload the COBOL run-time).
Chapter 4. Working with Visual Basic and COBOL 71

Developing the COBOL Portion of the Application


In this section you are going to develop the HICOBOL.DLL executable which will be
called by Visual Basic.
You are going to use the COBOL Project Manager to assist you.
1. Start the COBOL Project Manager; the following window appears:

Figure 4.1. COBOL Project Manager

2. From the Project menu, select Open Project.


Project Manager displays the Open dialog.
We will use this dialog to create a new folder (sub-directory) to store the
application you are about to develop.
3. Navigate to the
C:\Program Files\Fujitsu NetCOBOL for Windows\Samples\Cobol
folder.
72 Chapter 4. Working with Visual Basic and COBOL

Figure 4.2. The Open project dialog.

4. Locate and click on the Create New Folder button to the upper right of the
dialog.
This creates a new folder in the Samples folder, called “New Folder”, with the
text already selected.
5. Change the name of the folder to HICOBOL, by typing:
HICOBOL, and click on ENTER.

Figure 4.3. Hicobol folder created in the Open project dialog.

6. Double click on the new folder Hicobol, to switch to that folder.


Chapter 4. Working with Visual Basic and COBOL 73

7. In the File Name field, enter the name of the new project, HICOBOL, and click
on the Open button.
Project Manager displays the following message:

Figure 4.4. Create new project message.

8. Click on the Yes button, and the project file is created.


9. Right click on the project name to display a pop-up menu and select “New File”
to create a new target file.
10. Type over the file name (NewFile.EXE) with HICOBOL.DLL. The COBOL Project
Manager window should now look like this:

Figure 4.5. HICOBOL.DLL created in COBOL Project Manager.

Because we are not planning to create any other executables, you will now define
the files that will be used to create this DLL.
11. Right-click the mouse on HICOBOL.DLL and select Create Folder from the pop-
up menu.
Project Manager displays the Create Folder sub-menu:
74 Chapter 4. Working with Visual Basic and COBOL

Figure 4.6. The Create Folder sub-menu.

This menu offers the types of files upon which HICOBOL.DLL might depend. You
need to create just one folder - for the COBOL source file.
12. Select “COBOL Source File”.
Project Manager adds a “COBOL Source File” folder to the project tree.
13. Right click on “COBOL Source File” and select “New File” from the pop-up menu.
14. Overtype “NewFile.COB” with “HICOBOL.COB”.
The project tree should now look like this:

Figure 4.7. HICOBOL.COB added to the project tree.

You are now ready to create the COBOL source program.


15. Within the COBOL Project Manager, double-click the mouse on the
HICOBOL.COB file name.
This brings up a blank Editor window as shown below:
Chapter 4. Working with Visual Basic and COBOL 75

Figure 4.8. The COBOL Editor.

You are now ready to construct the HICOBOL.COB source program. The
quickest way to do this is to use the Editor’s template function.
16. From the Edit menu select Template.
The Editor displays the Template dialog.
17. Expand the COBOL basic functions, Source unit nodes in the Categorization of
template tree:
76 Chapter 4. Working with Visual Basic and COBOL

Figure 4.9. The Template dialog.

18. Select “Source program structure (External program definition)” from the list of
Template name and click the Expand button.
The Editor inserts the template code.
19. You can now see the code that has been inserted in the Editor:
Chapter 4. Working with Visual Basic and COBOL 77

Figure 4.10. The template code inserted in the Editor.


78 Chapter 4. Working with Visual Basic and COBOL

20. Uncomment the LINKAGE SECTION header, delete other unwanted comment
lines, and insert the text shown below to create the HICOBOL program:

Figure 4.11. The HICOBOL program – showing inserted code.

If you examine the HICOBOL program, you will understand what it does quite
easily. Note that it contains a LINKAGE SECTION and thus needs two
parameters passed to it, each time it is called.
The vbInteger parameter is always set to the value of 100. The vbString
parameter is always set to the value of "Hello from COBOL".
These parameters will be defined in the Visual Basic application and passed to
the COBOL program. They are passed by reference – meaning that the COBOL
program gains access to the data areas in the Visual Basic program. The COBOL
program updates the values and returns control to the Visual Basic program.
21. Select Save from the File menu to save the program.
22. Select Close from the File menu to close the Editor.
You are nearly ready to build the HICOBOL.DLL executable.
23. Return to the COBOL Project Manager window, select HICOBOL.PRJ in the
project tree and, from the Project menu, select Options, Linker Options.
Project Manager displays the Linker Options dialog:
Chapter 4. Working with Visual Basic and COBOL 79

Figure 4.12. The Linker Options dialog.

24. In the Command Line entry field type:


/EXPORT:HICOBOL
This tells the linker to make the HICOBOL entry point available as an external
reference in the DLL file.
25. Click on OK.
26. From the Project menu select Build.
Project Manager compiles and, if there are no compile errors, links the program
into a DLL file.
27. If you receive any compile errors, they will be displayed in red in the Builder
window that is used to display compiler and linker messages.
- Double-click on an error line to be taken to the location of the error in the
editor.
- Any errors should be typos so check your code carefully, correct it and save
the file.
– Return to the COBOL Project Manager window and again select Build from the
Project menu.
80 Chapter 4. Working with Visual Basic and COBOL

Project manager recognizes that the source file has been updated since the last
build and recompiles and relinks the appropriate parts of the project (in this
case all the parts).
Once you have a successful build (or rebuild) of your application, the following is
displayed in the Builder window:

Figure 4.13. The Builder window showing a successful build of HICOBOL.

You have now completed the COBOL development side of this application.

Developing the GUI Interface Using Visual Basic


You will now use Visual Basic version 4.0 or higher to develop the graphical user
interface for the HICOBOL application. The snapshots are taken using Visual Basic
6.0.
1. Bring up the Visual Basic development environment. (Select Visual Basic from
the Windows Start, Programs menu.)
Visual Basic displays a window like this:
Chapter 4. Working with Visual Basic and COBOL 81

Figure 4.14. The Visual Basic start-up window.

This window asks you to specify which type of application you are going to
develop. You are going to develop a Standard EXE so:
2. Select Standard EXE and click on the Open button.
Visual Basic displays the following window (or one like it, depending on which
display options you have set):
82 Chapter 4. Working with Visual Basic and COBOL

Figure 4.15. Visual Basic design window.

You should already be familiar with this environment. If not, it is strongly suggested
that you go through the Visual Basic “Your First Visual Basic Application” tutorial in
“Getting Started with Visual Basic” (or “Visual Basic Programmer’s Guide” – earlier
versions).
In the center of this window is an object named Form1. This will become our main
application window. You are going to create a text box and two command buttons on
this form.

Create the Text Box:


3. Move the mouse to the toolbox (the “object palette”) on the left-hand side of
the window and click once on the text box icon, .
4. Move the mouse over to the Form1 grid. Starting near the upper left hand
corner of the grid, hold the left mouse button down, and drag the mouse down
a bit and to the right to draw a text box to look something like this:
Chapter 4. Working with Visual Basic and COBOL 83

Figure 4.16. Text box placed on the Visual Basic form.

In the Properties window, enter "Hello from Visual Basic" in the Text property
value.
You will notice that it instantly changes in the text box on Form1.

Create the Command Buttons:

5. Click on the command button icon, , in the toolbox.


6. Move the mouse pointer near the bottom left of the Form1 window and draw a
command button by holding the left mouse button down and dragging it.

Figure 4.17. Command button added to the Visual Basic form.


84 Chapter 4. Working with Visual Basic and COBOL

7. Change the Caption value (the text displayed on the command button) from
Command1 to Hello COBOL, in the Properties window.
8. You may reposition the command button or the text box by selecting either with
the mouse and dragging the selected object to the position you desire on the
form.
9. Now place a second command button on Form1 between the text box and the
first command button by repeating steps 3 to 5 above. Change the caption value
to “Reset”.
Form1 should now look like this:

Figure 4.18. Reset command button added to the Visual Basic form.

Create the Programming Logic in Visual Basic


Now that you have created the application’s main window (Form1) and its associated
objects, it’s time to wire this all together by creating some program logic to define
the interaction of the user interface.
Visual Basic makes this process simple and straightforward in the way it has
implemented the event-driven programming paradigm.
In these steps you will take care of the following tasks:
• Writing the actual Visual Basic Script (programming language) to handle events
and to call the supporting COBOL application.
• Declaring the components of the supporting COBOL application to Visual Basic.
• Executing the overall application under the control of Visual Basic.
You will begin by writing the program logic.
The first requirement you will take care of is to implement the functionality for the
Reset push button. The behavior that is desired is to have the text box string reset
with a standard value each time a user clicks this command button.
Chapter 4. Working with Visual Basic and COBOL 85

Implementing the Reset Button


10. Move the mouse pointer over the Reset command button. Double click on this
button.
This brings up a program code window to create a Visual Basic subroutine that
will be executed every time a user clicks on the Reset button. Part of the
subroutine code is created automatically (its definition and exit).
The cursor is automatically placed at the proper spot in the new subroutine to
begin entering commands.
11. Enter the following line of code, which sets the text string to a new value:
Text1.Text = "Text Reset"

Figure 4.19. Command2_Click skeleton event code.

12. Click the mouse on the Close button (the X in the upper right hand corner) of the
program code window you just typed in.
Implementing the Hello COBOL Button
The Hello COBOL command button is where most of the action in this application will
take place. When a user clicks on this button, Visual Basic will call the HICOBOL.DLL
executable and pass two parameters to it.
The COBOL program will update both parameters and return control to the Visual
Basic program. Visual Basic will take the value of the string parameter (which the
COBOL program will change to "Hello from COBOL") and update the text box value
with it.
You need to perform the following steps at this point:
13. Move the mouse over the Hello COBOL command button and double click on it to
open up a program code window.
86 Chapter 4. Working with Visual Basic and COBOL

14. Enter the following code in the new code window between the Private Sub and
End Sub statements:

Dim vbInteger as Integer


Dim vbString as String * 16
Call JMPCINT2
Call HICOBOL (vbInteger, vbString)
Text1.Text = vbString
Call JMPCINT3

Let’s look at what each of the above noted statements does at run-time:
Dim vbInteger as Integer - This defines a numeric data item named
vbInteger.
Dim vbString as String * 16 - This defines a character string of 16
characters named vbString.
Call JMPCINT2 - This calls the supplied NetCOBOL routine which initializes the
COBOL run-time support. It need only be called once per application.
Call HICOBOL (vbInteger, vbString) - This calls the COBOL DLL file you
previously created, and passes the two parameters to it.
Text1.Text = vbString - This command instructs Visual Basic to update the
Text string in the Text1 text box with the value contained in vbString, which will be
returned from the HICOBOL COBOL program.
Call JMPCINT3 - This calls the supplied NetCOBOL routine which unloads (exits)
the COBOL run-time support. It need only be called once per application.
Your Code window should look like this:

Figure 4.20. Code added to Command1_Click event.

Defining the COBOL Components to Visual Basic


The final step that needs to be performed is to define to Visual Basic the external
COBOL executables that are going to be referenced in this application. Do the
following:
Chapter 4. Working with Visual Basic and COBOL 87

15. While the program code window shown above is still open, click on the small
down arrow to activate the drop down box that currently has the value
Command1. This box is located in the upper left of the project code window. It
should appear as follows:

Figure 4.21. Code drop down list.

This drop down list box allows you to navigate through your program source
code for each event subroutine you have defined. You need to go to the General
section of your program to create declarations for the external COBOL
executables.
16. Click on (General).
17. Enter the following 3 statements in any order (as one long string each) in the
program code window in the General Section area:

Private Declare Sub HICOBOL Lib "C:\Program Files\Fujitsu NetCOBOL for


Windows\SAMPLES\COBOL\HICOBOL\HICOBOL.DLL" (vbInteger as Integer, ByVal
vbString as String)
Private Declare Sub JMPCINT2 Lib "C:\Program Files\Fujitsu NetCOBOL for
Windows\F3BIPRCT.DLL" ()
Private Declare Sub JMPCINT3 Lib "C:\Program Files\Fujitsu NetCOBOL for
Windows\F3BIPRCT.DLL" ()

The three statements above have been wrapped to multiple lines because of the
page size of this manual. They should be entered into the program code window
as a single line each (not wrapped).
These statements define to Visual Basic where the external COBOL modules that
will be referenced at run-time reside. Be sure to alter the path settings above
when entering these if you changed the default installation directory when you
installed NetCOBOL to reflect your machine’s installation location.
88 Chapter 4. Working with Visual Basic and COBOL

Your Visual Basic code should now look like this:

Figure 4.22. General section Visual Basic code.

18. Close the program code window.


You have now completed all of the steps required to create this sample application.
You should save the Visual Basic portion you’ve just created.
19. Select Save Project from the File menu.
You will be prompted to save the form.
Change the Form name from Form1.frm to HICOBOL.frm, and save it in the
HICOBOL folder (within the "C:\Program Files\Fujitsu NetCOBOL for
Windows\samples\cobol" folder).
20. Select Save Project again, to save the project.
Name it HICOBOL.vbp and save it.
Chapter 4. Working with Visual Basic and COBOL 89

Executing the HICOBOL Application under Visual Basic


You are now ready to execute the HICOBOL application.
1. From the Run menu of Visual Basic, select Start.
The HICOBOL graphical window you just created appears:

Figure 4.23. The HICOBOL form displayed at run time.

2. Click on the Reset button.


The text in the Text box changes to “Text Reset”.
3. Click on the Hello COBOL command button.
Visual Basic issues a call to the HICOBOL.DLL executable.
The program updates the parameters, which then update the text box in your
graphical window to read "Hello from COBOL".
4. Click on the Reset button.
The text in the Text box returns to “Text Reset”.
5. Close the window to exit the application.
If you want to create a standalone EXE for your application, use the Make command
under the Visual Basic File menu.
Note that Visual Basic applications require you to distribute the Visual Basic Run-time
DLL along with your EXE and the COBOL components required.

Integrating COBOL Programs with Visual Basic


This section contains detailed information regarding the integration of NetCOBOL
programs with Visual Basic applications.

COBOL DLL Interaction with Visual Basic


To access any COBOL routines from Visual Basic, you need to first declare the
COBOL PROGRAM-ID or the COBOL ENTRY routine using the Basic DECLARE
statement with the special LIB keyword to specify the path name of the COBOL DLL.
These declarations can be made in the declaration section of any form module or in
90 Chapter 4. Working with Visual Basic and COBOL

the global module, and must be declared as Private. You can then call these routines
from your Visual Basic code like any other function call.
When a Visual Basic program calls a COBOL routine, you should call JMPCINT2
before calling the first COBOL routine, and call JMPCINT3 after calling the last COBOL
routine. JMPCINT2 is a subroutine that initializes the COBOL Run Time Environment
and JMPCINT3 is a subroutine that exits the COBOL Run Time Environment.
Calling COBOL routines without calling JMPCINT2 and JMPCINT3 may degrade
performance because the COBOL Run Time Environments will be initialized and
exited on every COBOL routine called.

Visual Basic Declarations of COBOL Modules


Private Declare Sub PROG Lib "c:\mycobol.dll" (vbInteger as Integer, ByVal vbString as
String)
Private Declare Sub JMPCINT2 Lib "C:\Program Files\Fujitsu NetCOBOL for
Windows\F3BIPRCT.DLL" ()
Private Declare Sub JMPCINT3 Lib "C:\Program Files\Fujitsu NetCOBOL for
Windows\F3BIPRCT.DLL" ()

NOTE: The above Declare statements should all be on a single line, they have been
wrapped in this text to fit the width of the page.

Visual Basic Call to a COBOL Program


Sub Form_Click ( )
Dim vbInteger as Integer
Dim vbString as string * 15
Call JMPCINT2 ‘ Initialize COBOL Runtime Environment
Call PROG (vbInteger, vbString)
Call JMPCINT3 ‘ Terminate COBOL Runtime Environment
End Sub

COBOL LINKAGE SECTION and PROCEDURE DIVISION


Identification Division.
Program-ID. "PROG".
Data Division.
Linkage Section.
01 vbInteger pic s9(4) comp-5.
01 vbString pic x(15).
Procedure Division with STDCALL Linkage Using vbInteger, vbString.
move 100 to vbInteger
move "NetCOBOL" to vbString
exit program.

Parameter Passing Between Visual Basic and NetCOBOL


Visual Basic treats a COBOL DLL as a “black box.” It only needs to know the COBOL
program's LINKAGE SECTION parameter list to pass data to and from the COBOL
DLL. Parameters can only be passed from Visual Basic to COBOL BY REFERENCE,
except strings which must be passed using the ByVal keyword in the DECLARE
statement. Passing parameters BY REFERENCE is the default in Visual Basic.
Chapter 4. Working with Visual Basic and COBOL 91

Parameter names need not be the same between Visual Basic and COBOL, however,
attribute, length, and number of corresponding data items must be identical.
Visual Basic declarations must exactly match the COBOL parameter lists defined in
the USING statement of the COBOL PROCEDURE DIVISION or ENTRY statements. No
parameter checking is done because the two are separate compilation units.
Visual Basic incorporates a rich assortment of data types, some of which are
currently not supported by NetCOBOL. These data types include variable-length
strings, Variants, and objects.
Similarly not all COBOL data types are supported by Visual Basic.

Visual Basic Strings


All strings passed between Visual Basic and COBOL must be declared as fixed-length
strings and passed using the ByVal keyword in the DECLARE statement. To avoid
possible memory corruption, ensure that all strings passed between Visual Basic and
COBOL occupy the same size.

Dim vbString as string * 15 ‘ Equivalent to PIC X(15)

Corresponding Visual Basic and COBOL Data Types


When passing parameters, associate the data types as follows:

Table 5.1. Corresponding Visual Basic and COBOL data types


Visual Basic COBOL
Type Name Storage Size PICTURE Clause
Boolean (16bit) 2 Bytes S9(4) COMP-5
Boolean (32bit) 4 Bytes S9(9) COMP-5
Byte 1 Byte X
Currency 8 bytes S9(10)V9(4) COMP-5
Double 8 Bytes COMP-2
Integer 2 Bytes S9(4) COMP-5
Long 4 Bytes S9(9) COMP-5
Single 4 Bytes COMP-1
String 1 Byte per Character X(n)

Passing Arrays
Visual Basic and COBOL can pass numeric arrays. This works because numeric array
data is always laid out sequentially in memory. A COBOL routine, if given the first
element of an array, has access to all of its elements.
92 Chapter 4. Working with Visual Basic and COBOL

Passing the Currency Data Type


The Currency data type in Visual Basic equates to

PIC S9(14)V9(4) COMP-5.

You can pass Currency parameters if the receiving Linkage Section item has this
picture clause.
A Visual Basic declaration of a COBOL subroutine receiving the Currency data type
would look like the following:

Private Declare Sub CSPRINT Lib "c:\CSPRINT.dll" (cc As Currency)

Private Declare Sub JMPCINT2 Lib "c:\Program Files\Fujitsu NetCOBOL for


Windows\f3biprct.dll" ()

Private Declare Sub JMPCINT3 Lib "c:\Program Files\Fujitsu NetCOBOL for


Windows\f3biprct.dll" ()

NOTE: The above Declare statements should all be on a single line, they have been
wrapped in this text to fit the width of the page.

CSPRINT is the COBOL subroutine; JMPCINT2 and JMPCINT3 are required for mixed
language applications including Visual Basic and COBOL.
The code leading to, and including the subroutine call would look like this:

Dim cc As Currency
cc = 100.0001
Call CSPRINT(cc)

The COBOL program processing the COMP-5 equivalent of Currency would look the
following:

IDENTIFICATION DIVISION.
000020 PROGRAM-ID. CSPRINT.
000030 ENVIRONMENT DIVISION.
000061 DATA DIVISION.
000101 LINKAGE SECTION.
000102 01 CC PIC S9(14)V9(4) COMP-5.
000110 PROCEDURE DIVISION WITH STDCALL USING CC.
000150 ADD 1 TO CC.
000160 EXIT PROGRAM.

NOTE: If the data type is converted (for instance, if it is moved to a PACKED-


DECIMAL field, or DISPLAYed), then the value of the
data may be truncated.

Returning Control and Exiting Programs


To return control from COBOL to Visual Basic, execute the EXIT PROGRAM
statement. When the EXIT PROGRAM statement is executed, control returns
immediately to the calling program.
Chapter 4. Working with Visual Basic and COBOL 93

Compiling and Linking the COBOL Programs


To build the COBOL DLL, you must tell the linker which entry points are to be made
external (i.e. those entry points that will be called from Visual Basic). You can do this
by:
1. Specifying the /ENTRY:entry-name linker option for each entry point, or:
2. Creating a module definition file (.DEF) that lists the attributes of the DLL
library. You can modify the example below, changing the “PROG” to match the
COBOL PROGRAM-ID (or other ENTRY name that you call).

LIBRARY "dll-name"
EXPORTS PROG

You then save this file as “prog-name.DEF”, where prog-name is the name of your
program.
Add it to your project by right clicking on the DLL file node and select New Folder,
Module Definition File, from the pop-up menu. You then add the .DEF file to that
folder.
94 Chapter 4. Working with Visual Basic and COBOL
Chapter 5.
Using SQL with COBOL
This chapter describes the steps required to setup your environment to access SQL
databases using ODBC. It first gives you an overview of ODBC and the general mode
of working with SQL databases, then goes into the details of setting up your
environment. In particular it covers:
• Defining SQL databases.
• Defining an ODBC data source.
• Setting up NetCOBOL runtime information.

Introduction
NetCOBOL applications can contain sophisticated processing logic that interacts with
SQL database systems. The technology that is used to interface to these databases is
known as ODBC (Open Database Connectivity), which provides a standard and
generic mechanism for any number of applications to access SQL data in a relational
matter.
The following sections deal specifically with how to set up the interface between a
NetCOBOL SQL program and SQL databases. It concludes with some tips identifying
potential runtime problems with NetCOBOL SQL applications.
Note that the samples and screen pictures are from a Microsoft Windows 2000
environment. You may experience some differences on Windows 98, Windows Me,
Windows XP, Windows NT 4.0 and Windows Server 2003 systems, but these should
be easy to determine.

Typical SQL Program Structure


When you create a NetCOBOL program using embedded SQL that will access one or
more SQL databases, your application logic typically goes through the following steps
in the following order:
a. Connect to an SQL database
b. Declare a cursor (for multiple Selects if desired)
c. Open the cursor (or if no cursor is desired, simply execute an SQL SELECT
statement).
d. Perform any other desired SQL operations on the data, such as fetching
from an open cursor, reading, writing, deleting and updating any specific
data.
e. Closing any open cursors.
f. Transaction processing, such as committing or rolling back changes.
g. Disconnecting from SQL databases.
96 Chapter 5. Using SQL with COBOL

In NetCOBOL, you code your programs using embedded SQL statements and host
variables. For information on writing SQL code in NetCOBOL programs, refer to
“Chapter 19. Database (SQL)” of the “NetCOBOL User’s Guide for Windows”. This
chapter covers the information you need to know to set up an environment that
supports such programs.
Note that when creating NetCOBOL SQL applications you can access any number of
SQL databases available to you within a single application.

Setting up the SQL Execution Environment


In order for a NetCOBOL SQL application to execute successfully, you must ensure
that three separate setup oriented tasks have taken place prior to execution:
1. Define the SQL databases and tables.
2. Define ODBC data sources.
3. Setup NetCOBOL runtime information.
This section gives an overview of these three tasks. The following sections then step
you through the details.

1. Define the SQL Databases and Tables.


The SQL database(s) and table(s) must typically be defined prior to execution. Note
that the SQL language itself is divided into two separate categories – DDL (Data
Definition Language) and DML (Data Manipulation Language). DDL is used to create
and delete (drop) tables and apply certain access privileges. DML is used to
manipulate data and includes reading, writing, deleting and updating tables, among
other things.
NetCOBOL applications generally are DML specific. NetCOBOL does not allow you to
code embedded DDL in a COBOL program. You may, however, write NetCOBOL
applications using dynamic SQL, which contains DDL statements. See “Using
Dynamic SQL” in “Chapter 19. Database (SQL)” of the “COBOL User’s Guide for
Windows” for more information on dynamic SQL in NetCOBOL programs.
Once your SQL database(s) and table(s) have been set up properly, you must
additionally ensure that you have been granted appropriate access and that you are
aware of any required user id(s) and password(s) required for accessing the SQL
data you desire. If you are unfamiliar with this process, you should consult with your
SQL database administrator.

2. Define ODBC Data Sources.


After your SQL database(s) and table(s) have been set up properly, you must define
an ODBC data source to be used by your NetCOBOL application. It is important to
understand that ODBC acts as a sort of “middle man” between your NetCOBOL
application and the actual SQL database(s). The embedded SQL statements
contained in your NetCOBOL application are actually compiled into ODBC API
(Application Programming Interface) calls to the ODBC software. ODBC then takes
care of interfacing to the actual SQL database(s).
The advantage to using ODBC from NetCOBOL is that you are thus able to write a
single COBOL program that can access any number of different SQL databases, as
Chapter 5. Using SQL with COBOL 97

this connection is made at runtime. Switching a NetCOBOL application from one


vendor’s SQL database system to another is as easy as switching the ODBC data
source – no changes are required to the COBOL program itself; it does not have to
be recompiled or linked.
The details of creating an ODBC data source are described in the section “Defining
an ODBC Data Source” below.

3. Setup NetCOBOL Runtime Information.


Once your SQL database(s) and table(s) have been set up properly, and you have
created a proper ODBC data source for your application, you must set up NetCOBOL
specific runtime information. This step connects the NetCOBOL application to the
proper ODBC data source and contains other information about the SQL database
environment you wish to access. This information is contained in a NetCOBOL
information (.inf) file which is pointed to at runtime. See the section “Setting Up the
COBOL Runtime Information” below for details.

Setting up SQL Databases and Tables


Because there are a wide array of different SQL database systems from a variety of
different vendors it is not feasible to give detailed instructions in this chapter. See
your SQL database administrator if you have any questions.
It is worth noting, however, that one of the most common problems that developers
experience is not being able to access a SQL database because security and
permissions have not been set up correctly.
If you receive unexplained errors at runtime when you attempt to access an SQL
database, it is a good idea to check the user id and password you are using. Ensure
that they have the appropriate permissions set up in the SQL database system to
allow you access to the database and tables you are using.

Defining an ODBC Data Source


Creating an ODBC data source is a simple and straightforward process. You first
check that you have a 32 bit ODBC manager installed, then use the Data Source
Administrator to create the ODBC data source. The following topics take you through
these steps.

Checking for a 32 Bit ODBC Manager


To check for a 32 bit ODBC manager, you access the Windows Control Panel by
clicking on the Windows Start button, moving the mouse over the Settings option
and left clicking on the Control Panel option. Then you access Administrative Tools
by clicking Administrative Tools icon on Windows Control Panel. Look in your
Administrative Tools and ensure that you have a Data Sources (ODBC) icon as shown
in Figure 5.1 below:
98 Chapter 5. Using SQL with COBOL

Figure 5.1. Checking for Data Sources (ODBC) software under Windows 2000.

If you do not have Data Sources (ODBC) installed on your Windows machine, check
with your software administrator about obtaining it and installing it properly.

An Overview of the ODBC Data Source Administrator


Double click on the Data Sources (ODBC) icon to bring up the ODBC Data Source
Administrator as shown in Figure 5.2.
Chapter 5. Using SQL with COBOL 99

Figure 5.2. The ODBC Data Source Administrator under Windows 2000.

Note that your Data Source Administrator may appear differently depending on the
Windows operating system that you are running and the version of ODBC you have
installed.
There are three tabs in this window for accessing User DSN’s, System DSN’s, and File
DSN’s.
A User DSN (Data Source Name) will only be visible to the same User ID who was
logged onto the current machine and previously created the DSN. For example, if
you log onto your Windows machine with a user ID of “Fred” create a new ODBC
User DSN, log off and log back on as “James”, you will not see or have access to the
just-created User DSN.
If you had instead logged on as Fred and created a System DSN, any subsequent
user who logged onto the same machine, regardless of their user ID would both see
and be able to access the newly created DSN.
While User and System DSN’s are specific to the current machine only, File DSN’s
may be shared by other users who have the same ODBC drivers installed on their
machines.
You can create ODBC DSN’s for any COBOL application using any of these three tabs,
but remember the ramifications if you change user ID’s or wish to export the DSN to
another machine.
Prior to creating a new DSN, you should click on the Drivers tab and ensure the
ODBC database driver for the actual type of database you wish to access is installed.
In the example below, we will set up a new DSN for connecting to a Microsoft SQL
Server database. Note in the Drivers panel shown in Figure 5.3 below that a SQL
Server driver is indeed installed as required.
100 Chapter 5. Using SQL with COBOL

Figure 5.3. The ODBC Data Source Administrator Drivers panel under Windows 2000.

If your system does not contain the required ODBC driver for the type of SQL
database system you wish to access, check with your SQL database administrator
about obtaining and installing the current driver.
Once you are satisfied that you have a proper driver installed, click back on one of
the three DSN tabs.
Chapter 5. Using SQL with COBOL 101

Creating a Data Source Name (DSN)


In the example below, a new System DSN will be created. The System DSN tab
appears as shown in Figure 5.4.

Figure 5.4. ODBC Data Source Administrator System DSN tab under Windows 2000.
102 Chapter 5. Using SQL with COBOL

To create a new ODBC data source:


1. Click on the Add button. A dialog box appears as shown in Figure 5.5.

Figure 5.5. The ODBC Data Source Administrator “Create New Data Source” dialog
box under Windows 2000.

2. Move down the list of available ODBC drivers and select the one for which you
wish to create the ODBC data source (in this example we will pick SQL Server).
3. Click on the Finish button. A dialog box appears as shown in Figure 5.6.
Chapter 5. Using SQL with COBOL 103

Figure 5.6. The ODBC Data Source Administrator “Create a New Data Source to SQL
Server” dialog box under Windows 2000.

4. In the Name field, enter the name you wish to use to access this DSN. In this
example, we will use “MySQL”.
5. In the Description field (which is optional), enter a meaningful description.
6. In the Server field, you can enter “(local)” if the database system resides on the
same system you are developing your application on, or enter the name of any
valid database server on your network. In this example, we will enter “(local)” to
indicate that the SQL Server resides on the same server as the application being
developed as shown in Figure 5.7.
104 Chapter 5. Using SQL with COBOL

Figure 5.7. The ODBC Data Source Administrator “Create a New Data Source to SQL
Server” dialog box with parameters entered under Windows 2000.
Chapter 5. Using SQL with COBOL 105

7. Now click on the Next button to bring up the dialog box shown in Figure 5.8.

Figure 5.8. The ODBC Data Source Administrator “Create a New Data Source to SQL
Server” subsequent dialog box under Windows 2000.

8. This is an important step in the process regarding security and user privileges.
The default for this is to login to the SQL Server you have previously specified
using your Windows user ID and password (With Windows 2000 authentication
using the network login ID).
The second option is “With SQL Server authentication using a login ID and
Password entered by the user”. If you wish to have the user of this data source
log in to SQL Server directly, you should select this second option. If you do so,
you will be able to specify a Login ID and Password. In this example, we will do
this and change the User ID to “sa” and leave the password blank.
Note that the Client Configuration button is for advanced users and will allow
you to check certain driver information and to change the default network
protocol to be used. You generally should not have to use this facility.
9. Once you have changed the authenticity option and altered the User ID and
Password, click on the next button.
Because we did not change the selection for the “Connect to SQL Server to
obtain default settings for the additional configuration options” checkbox, the
ODBC DSN setup process will connect to SQL Server and allow us to specify
some additional information. A dialog box appears as shown in Figure 5.9.
106 Chapter 5. Using SQL with COBOL

Figure 5.9. The ODBC Data Source Administrator “Create a New Data Source to SQL
Server” subsequent dialog box under Windows 2000.

This dialog box allows you to change certain installation specific options, and to
specify how to create and deal with temporary stored procedures. It also allows
you to change the default database to be accessed. In this case, we will change
the default database to be accessed to the Pubs database.
10. Select the check box “Change the default database to:”.
11. Select the Pubs database from the dropdown list that is enabled.
12. Click on the Next button and a subsequent dialog box appears as shown in
Figure 5.10.
Chapter 5. Using SQL with COBOL 107

Figure 5.10. The ODBC Data Source Administrator “Create a New Data Source to
SQL Server” subsequent dialog box under Windows 2000.

Once again some implementation specific options are made available to you.
This dialog box allows you to turn on some tracing options and to specify where
you want the trace information to be output.
The “Save long running queries to the log file:” option allows you to identify
long running queries so that you may examine them later. This is generally used
for performance tuning.
“Log ODBC driver statistics to the log file:” option allows you to have ODBC
statistical information recorded at run time. This can be useful in debugging
ODBC specific problems. You may also turn a tracing option on or off at any
time directly in the main ODBC Data Source Administrator window under the
Tracing tab.
13. When you have finished with the configuration of the new ODBC DSN, you
should click on the Finish button.
You will be presented with a dialog box containing an overview of the
information associated with the new ODBC DSN you’ve just created as shown in
Figure 5.11. More importantly, there is a Test Data Source button available.
Note that some versions of the ODBC software do not provide this option, so
don’t be alarmed if you do not have it.
108 Chapter 5. Using SQL with COBOL

Figure 5.11. The ODBC Microsoft SQL Server Setup overview dialog box under
Windows 2000.

14. If you have it available, go ahead and click on the Test Data Source button. This
connects to the SQL Server specified and tries out the data source. If it tests
correctly, you will be presented with the dialog box shown in Figure 5.12.
Chapter 5. Using SQL with COBOL 109

Figure 5.12. The SQL Server Setup ODBC Data Source Test results dialog box under
Windows 2000.

If you do not have the Test Data Source option available in your version of ODBC,
you can use a generic query tool such as Microsoft’s Query (MS Query) to quickly try
out a new data source.
Once you have successfully set up a new ODBC DSN for your NetCOBOL application,
you are ready to set up the NetCOBOL specific runtime configuration.

Setting Up the COBOL Runtime Information


For the purpose of demonstrating a NetCOBOL application and how to set it up, we
will use a simple single-program application that reads three fields from the
Employee table of the Pubs database that comes with SQL Server. This project is
named “MySQL”. It consists of a single .exe file named “MySQL.EXE”, which is
created from the following COBOL program:
110 Chapter 5. Using SQL with COBOL

Identification Division.
Program-ID. MySQL
Environment Division.
Data Division.
Working-Storage Section.
01 Loop-Flag Pic 9 Value 0.
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
01 EmpID Pic X(9) Value Spaces.
01 FName Pic X(20) Value Spaces.
01 LName Pic X(30) Value Spaces.
01 SQLSTATE Pic X(5) Value Spaces.
EXEC SQL END DECLARE SECTION END-EXEC.

Procedure Division.
Move 0 To Loop-Flag
SQLSTATE.

EXEC SQL
CONNECT TO Default
END-EXEC.

EXEC SQL
DECLARE CUR1 CURSOR FOR
SELECT emp_id, fname, lname FROM employee
END-EXEC.

EXEC SQL
OPEN CUR1
END-EXEC.

If SQLSTATE Not = Zeroes


Display "Open Cursor Error! SQLSTATE: ", SQLSTATE
Exit Program
End-If.
Move 0 To Loop-Flag
Perform Until Loop-Flag Not = 0
EXEC SQL
FETCH CUR1 INTO :EmpID,
:FName,
:LName
END-EXEC

IF SQLSTATE = Zeroes
Display EmpID, " ", FName, " ", LName
Else
Move 1 To Loop-Flag
EXEC SQL CLOSE CUR1 END-EXEC
End-IF
End-Perform.

EXEC SQL
DISCONNECT DEFAULT
END-EXEC.

Exit Program.

Prior to running a NetCOBOL SQL application, you must set up the SQL database(s)
and table(s), ensure that your security and authorization information is properly
configured and available to you, and set up an ODBC DSN.
Once you have accomplished these tasks, you must make this information available
to your NetCOBOL SQL application at run time. This is done in 2 separate steps:
1. Create a NetCOBOL specific information file (.inf file) which will contain required
information.
Chapter 5. Using SQL with COBOL 111

2. Point the application at the appropriate .inf file by creating/adding it to a


COBOL85.CBR file.
You create a .inf file using the NetCOBOL utility named “SQLODBCS.EXE”. This utility
will help you specify the required parameters and write them out in the proper
format. It also encrypts the Password value specified. You can view a .inf file in a
text editor, but you cannot modify any password fields because the are encrypted.
However, you may modify other fields in a text editor.
If you have not done so already, you may want to take advantage of the COBOL
Project Manager’s configuration features and add the SQLODBCS.EXE utility into the
Tools pull down menu. If you do not care to do this, you will have to manually find
and execute this utility each time you wish to execute it.

Adding SQLODBCS.EXE to the Tools Menu


To add the SQLODBCS.EXE utility to the Tools pull down menu of the COBOL Project
Manager, perform the following steps:
1. Bring up the COBOL Project Manager. Click on the Tools pull-down menu and
select the Customize Menu option as shown in Figure 5.13.

Figure 5.13. Choosing the Customize Menu option from the Tools pull-down menu in
the COBOL Project Manager.

A dialog box appears as shown in Figure 5.14.


112 Chapter 5. Using SQL with COBOL

Figure 5.14. The Customize Menu dialog box.

2. Click on the Add button to add a new menu item. Project Manager displays a
dialog box that allows you to enter the name of the executable file to be
associated with the new menu item. Fill in the fields as shown in Figure 5.15.

Figure 5.15. Setting customized command information.

3. Click on the OK button.


Project Manager returns you to the Customize menu dialog box displaying the
information about the newly added item as shown in Figure 5.16.
Chapter 5. Using SQL with COBOL 113

Figure 5.16. The Customize Menu dialog box with the new menu item added.

4. Click on the OK button.


The SQLODBCS utility has now been added to the Tools pull-down menu of the
COBOL Project Manager. When you click on the Tools pull-down menu it should
appear as shown in Figure 5.17.

Figure 5.17. The Tools pull-down menu with the SQL .inf File Utility added.
114 Chapter 5. Using SQL with COBOL

Creating a .INF File


To create a .inf file for a NetCOBOL SQL application:
1. Select the SQL .inf File Utility from the Tools menu (or find the SQLODBCS.EXE
utility in the "C:\Program Files\Fujitsu NetCOBOL for Windows" folder and
execute it).
The utility displays a file name dialog box to allow you to specify the name of
the .inf file you wish to create or modify as shown in Figure 5.18.

Figure 5.18. The .inf file name specification dialog box for the SQLODBCS Utility.

Note that it is important to fully specify the drive and path of the .inf file you
wish to create or modify, to ensure it gets saved to the proper location. If you
fail to do this, your new .inf file may be saved to a different directory than
where you expect to find it later. For our example, we are going to create a .inf
file named C:\MYSQL\MySQL.inf.
2. Enter the name of the .inf file you wish to create as shown in Figure 5.19.

Figure 5.19. The .inf file name specification dialog box for the SQLODBCS Utility,
with a fully path qualified file name entered.

3. Click on the OK button, respond Yes to the message asking you if you wish to
create a new file, and OK to the message confirming that a file was created.
The SQLODBCS utility window appears as shown in Figure 5.20.
Chapter 5. Using SQL with COBOL 115

Figure 5.20. The dialog box for the .inf file configuration facility (SQLODBCS.EXE).

In order to understand how to specify parameters for a .inf file in the


SQLODBCS utility, you need to understand about connections to servers from a
NetCOBOL application.
To establish a connection to an actual SQL database in a NetCOBOL program
(as shown in the example COBOL program above), you must code an SQL
CONNECT statement such as:
EXEC SQL
CONNECT TO 'FRED'
END-EXEC.

In the above code example, the NetCOBOL program is attempting to connect to


a server name “FRED”. This is a bit misleading as “FRED” is not the name of an
actual SQL or 2000 Server. Instead, “FRED” is a symbolic name to be associated
with an entry in a NetCOBOL .inf file.
Remember that when you created an ODBC Data Source in the ODBC Data
Sources administrator, you specified the actual SQL Server to connect to. This
connection is managed by ODBC and it does not make sense for a COBOL
program to specify a physical server, as conflicts could arise with ODBC Data
Sources.
The name “FRED” therefore simply refers to an entry in the .inf file, which will
contain other parameters, including the name of the actual ODBC data source to
116 Chapter 5. Using SQL with COBOL

use at runtime. Another reason for this level of abstraction from the physical
server name is to allow you to set up multiple entries for the same physical
server. You might have many different ODBC data sources defined for a variety
of different databases and/or tables that exist on a single SQL Server. You
would thus require a method to be able to specify these different ODBC data
sources.
Another important point is the ability to connect to a default server. The code
for this looks like:
EXEC SQL
CONNECT TO DEFAULT
END-EXEC.

In the above case, “DEFAULT” is a special symbolic name. It allows you to


abstract the name of the symbolic connection out of the program. You can
specify a DEFAULT server entry in the SQLODBCS utility to point at any data
source (of course you could also make ‘FRED’ your default entry and always
connect to ‘FRED’ as well). You can thus think of “DEFAULT” as just another
symbolic entry in the .inf file. Simply notice that “DEFAULT” does not require
quotes around it like other symbolic names in an SQL CONNECT statement.
Now that you understand symbolic server names and how they are used in SQL
CONNECT statements, take a look back at the SQLODBCS window as shown in
Figure 5.20.
Notice that there are two areas for entering parameters. Near the bottom is an
area entitled “Default Connection Information”. You must enter the name of a
default server, a User ID and a Password. These three fields are required and
will be used at runtime. You may remember that you specified a User ID and
Password in the ODBC data source when you created it under the ODBC Data
Sources Administrator. Note that NetCOBOL will ignore these, however, and will
instead always use the User ID and Password you specify in the SQLODBCS
utility (the User ID and Password stored in the .inf file you create). Note that the
Password will be encrypted and not viewable.
You can override the User ID and Password from within the actual COBOL
program in the CONNECT Statement. For example, if you code the following:
EXEC SQL
CONNECT TO ‘FRED’
USER ‘ID0001/PW01’
END-EXEC.

The value “ID0001” will be used as the User ID, and the value “PW01” will be
used as the password when connecting to the actual database server, regardless
of the User ID and Password currently specified in the .inf file.
Chapter 5. Using SQL with COBOL 117

If you code:
EXEC SQL
CONNECT TO ‘FRED’
USER ‘ID0001’
END-EXEC.

The value “ID0001” will be used for the User ID, but since you did not specify a
Password, the Password currently specified in the .inf file will be used when
connecting to the actual database server.
In neither case will the User ID or Password specified in the actual ODBC data
source be used – NetCOBOL will always override this.
This means that you must specify a User ID and Password in the SQLODBCS
utility for any .inf file you wish to create. If you leave these out, the SQLODBCS
utility will refuse to create your .inf file. If you manually delete these entries in a
.inf file, you will receive an error at runtime.
Our simple SQL COBOL application noted above connects to DEFAULT as shown
in the code below.
EXEC SQL
CONNECT TO DEFAULT
END-EXEC.

We now continue with the steps for creating the .inf file.
4. To create the appropriate .inf file, enter the parameters shown in Figures 5.21 to
5.24. If you are using multithreading or object oriented programs, see the
SQLODBCS Help for the appropriate Connection Scope settings.
118 Chapter 5. Using SQL with COBOL

Figure 5.21. The parameters entered for the sample application to create a .inf file
in the configuration facility (SQLODBCS.EXE) – Server Info tab.
Chapter 5. Using SQL with COBOL 119

Figure 5.22. The parameters entered for the sample application to create a .inf file
in the configuration facility (SQLODBCS.EXE) – Default Connection Info tab.
120 Chapter 5. Using SQL with COBOL

Figure 5.23. The parameters entered for the sample application to create a .inf file
in the configuration facility (SQLODBCS.EXE) – Connection Scope tab.

5. Click on the Apply button to create the .inf file and write it to disk.
The .inf file created by the above parameters will contain the following entries:

[SERVER LIST]
Default=Default
[Default]
@SQL_DATASRC=MySQL
@SQL_USERID=MySQL
@SQL_PASSWORD=BEINKAOHLEPJENBABBMM
@SQL_ACCESS_MODE=READ_ONLY
@SQL_COMMIT_MODE=MANUAL
@SQL_CONCURRENCY=READ_ONLY
@SQL_ODBC_CURSORS=USE_DRIVER
@SQL_QUERY_TIMEOUT=0
[SQL_DEFAULT_INF]
@SQL_SERVER=Default
@SQL_USERID=MySQL
@SQL_PASSWORD=BEINKAOHLEPJENBABBMM
[CONNECTION_SCOPE]
@SQL_CONNECTION_SCOPE=PROCESS

The Server List contains a line indicating that the server specified as the Default
connection (for any program trying to connect to DEFAULT) will be the symbolic
entry “Default” (Default=Default).
Following this is the entry information for the symbolic server name “Default”.
Note that Entry headings are designated with square brackets around them.
This entry contains the name of the ODBC data source to be used
Chapter 5. Using SQL with COBOL 121

(@SQL_DATASRC=MySQL), the access mode to be used


(@SQL_ACCESS_MODE=READ_ONLY), and the commit mode to be used
(@SQL_COMMIT_MODE=MANUAL).
Following the entry for the symbolic server “Default” is an entry for the default
information to be used (SQL_DEFAULT_INF). This contains the name of the
default symbolic server and a User ID and Password to be used to access it.
Note that the Password value has been encrypted.

Setting up the Runtime Environment Information


Once you have set up your SQL databases and tables, created the appropriate ODBC
data source, and created the appropriate .inf file, you are ready to execute your
application.
You must, however, ensure that you point the NetCOBOL runtime at the appropriate
.inf file. By finding the .inf file, the NetCOBOL runtime will have all of the information
needed to interface with ODBC at runtime to access the database tables your
application references.
To point the runtime to the appropriate .inf file, you need to create a COBOL85.CBR
file (or add a new entry for your application to an existing COBOL85.CBR file). You
should create this file in the directory in which your main application executable
(.exe) file resides.
It should include the following lines of code:

[MYSQL]
@ODBC_Inf=c:\mysql\mysql.inf

The entry “[MYSQL] designates the name of the main program. The line
“@ODBC_Inf=c:\mysql\mysql.inf” tells the runtime which .inf file to use and where it
is located. That’s all there is to it.
Instead of editing the COBOL85.CBR file using a text editor, you can use the Runtime
Environment Setup Tool, available from COBOL Project Manager’s Tools menu. Keys
to using this tool are that:
• The file you want to edit/create is COBOL85.CBR.
• The Section you want to edit/create is MySQL.
• You can select @ODBC_Inf from the pull-down list.
• You can use the browse (…) button to select the .inf file.
• You MUST click Set to add this variable to the list.
• You MUST click Apply to save the setting.
122 Chapter 5. Using SQL with COBOL

Potential Execution Errors Caused by Incorrect Setups


There are a number of errors that may occur at runtime if you do not set up the
ODBC environment for a NetCOBOL SQL application properly.
For Example, if you code an erroneous SQL statement, within an EXEC SQL …. END-
EXEC statement, the NetCOBOL compiler may catch the error and issue an error
message.
In some cases, however, the error will not be determined until runtime. Some of the
most common examples are listed below.
1. A common runtime error comes from not specifying to the Runtime Environment
the name of a valid .inf file. For example, in the above example application, if
you forget to specify the line:

@ODBC_Inf=c:\mysql\mysql.inf

in COBOL85.CBR, you will receive an error dialog box as shown in Figure 5.24.
This error occurs whenever no @ODBC_inf parameter is specified.

Figure 5.24. Error dialog for not specifying an @ODBC_inf parameter to the
Runtime Environment.

2. If you specify an @ODBC_inf parameter and it is found, but it contains the name
of a file that does not exist or cannot be found, you receive the error dialog box
shown in Figure 5.25.

Figure 5.25. Error dialog for specifying an invalid .inf filename in an


@ODBC_inf=filename parameter.

3. If your COBOL program attempts to connect to a server that is not specified in


your .inf file, you will receive the error dialog shown in Figure 5.26.
Chapter 5. Using SQL with COBOL 123

Figure 5.26. Error Dialog for attempting to connect in COBOL program to server
that is not specified in your .inf file.

Capturing Other Errors


There are other errors that will not produce an Error Message dialog box. In fact,
they will only produce errors by returning non-zero values in SQLSTATE and/or
SQLCODE. You may notice that the simple application above only checks SQLSTATE.
You will find two additional data items useful if you add them to your EXEC SQL
BEGIN DECALRE SECTION END-EXEC group. They are SQLCODE and SQLMSG. They
are defined as follows:

EXEC SQL BEGIN DECLARE SECTION END-EXEC.


01 EmpID Pic X(9) Value Spaces.
01 FName Pic X(20) Value Spaces.
01 LName Pic X(30) Value Spaces.
01 SQLSTATE Pic X(5) Value Spaces.
01 SQLCODE Pic S9(9) Comp-5 Value Zeroes.
01 SQLMSG Pic X(254) Value Spaces.
EXEC SQL END DECLARE SECTION END-EXEC.

SQLMSG is of particular use. The ODBC driver will place descriptive error messages
in this variable. For example, if you code the name of an ODBC data source that does
not exist in the “@SQL_DATASRC=” parameter in your .inf file, you will not receive
an error dialog box. Instead you will receive a value of IM002 in SQLSTATE. This is
not very helpful. But if you look at the value contained in SQLMSG, it will be:

[Microsoft] [ODBC Driver Manager] Data source name not found and no
default driver specified.

There are some SQL syntax errors that will not be caught by the NetCOBOL compiler
and will only show up at runtime. For example, if you code an erroneous Where
clause in a Select statement such as:
124 Chapter 5. Using SQL with COBOL

EXEC SQL
DECLARE CUR1 CURSOR FOR
SELECT emp_id, fname, lname FROM employee
Where emp_id Not NULL
END-EXEC.

Note that “Not NULL” is invalid SQL Code. When this statement is executed at
runtime, you will receive a value of 37000 in SQLSTATE (again, not very helpful), but
SQLMSG will contain:

[Microsoft] [ODBC SQL Server Driver] [SQL Server] Incorrect syntax near
the keyword ‘Null’.

It is thus a good idea to always include SQLMSG in your COBOL program and to
interrogate it whenever error messages occur. You may wish to display SQLMSG
when debugging after key SQL statements in your program.
Appendix A. Sample Programs
The sample programs shipped with NetCOBOL are intended to give an overview of
the capabilities of NetCOBOL and COBOL Project Manager. Refer to the “NetCOBOL
User’s Guide for Windows” for further details on using NetCOBOL. The following table
details the sample programs available with NetCOBOL.

Table A.1. NetCOBOL Sample Programs


Sample Purpose of sample Functions Used
Sample 01 Data Processing Using Standard - ACCEPT/DISPLAY (console window)
Input-Output - Debugger
Sample 02 Using Line Sequential and Indexed - Line sequential file (reference)
Files - Indexed file (creation)
Sample 03 Screen Input-Output Operation Sample 3 is not available in the English version of
Using the Presentation File Function NetCOBOL.
Sample 04 Screen Input-Output Using the - Screen handling
Screen Section - Indexed file (reference)
Sample 05 Calling COBOL Subprograms - Inter-program communication
- Library fetch
- ACCEPT/DISPLAY (message box)
- Print file
- Indexed file (reference)
- Line sequential file (creation)
- Passing parameters for execution
- Free format
Sample 06 Receiving a Command Line - Fetching command line arguments
Argument - Internal program
Sample 07 Environment Variable Handling - Environment variable handling
Sample 08 Using a Print File - Print file
- ACCEPT/DISPLAY
(console window)
Sample 09 Using a Print File (Advanced usage) - Print file
(changing print form, letter form, and layout)
Sample 10 Using a Print File with FORMAT Sample 10 is not available in the English version of
clause NetCOBOL.
Sample 11 Remote database access - Embedded SQL statements
(reference)
Sample 12 Remote database access - Embedded SQL statements
(multiple row processing) (reference, update and delete)
126 Appendix A. Sample Programs

Table A.1. NetCOBOL Sample Programs (cont.)


Sample Purpose of sample Functions Used
Sample 13 Calling COBOL from Visual Basic - Visual Basic
- COBOL subprograms
Sample 14 Visual Basic calling COBOL –Simple - Visual Basic
ATM Example - COBOL subprograms
Sample 15 Basic Object-Oriented Programming - Class definition (encapsulation)
- object generation
- method invocation
Sample 16 Collection Class (Class Library) - Class definition (encapsulation)
- object generation
- method invocation
Sample 17 Object-Oriented Cobol (Aggregation, - Using Class Library
Singleton, and Iteration) - Multiple inheritance
- polymorphism
Sample 18 Advanced Object-Oriented - Using Class Library
Programming - Multiple inheritance
- polymorphism
Sample 19 Object Persistence (Indexed File) - polymorphism
- Indexed file
Sample 20 Object Persistence (Database) - polymorphism
- Remote database access (ODBC)
Sample 21 Multithread Programming - Multithread programming
- Index file (creation, reference, update, and rewrite)
- External data/file
- Data locking
- COBOL ISAPI Subroutine
Sample 22 Multithread Programming (Advanced - Multithread programming
usage) - COBOL ISAPI Subroutine
- Event log (output of user definition information)
- External data/file
- Data locking
- Sharing data between threads
Sample 23 COM Program to Control Excel (Late - COM Client functions (Late binding)
Binding)
Sample 24 COM Program to Control Excel (Early - COM Client functions (Early binding)
Binding)
Sample 25 Creating a COBOL COM Server - COM Server functions
Program - Remote Database Access
- *COM-ARRAY class
Appendix A. Sample Programs 127

Table A.1. NetCOBOL Sample Programs (cont.)


Sample Functions Used File Name
Sample 26 Using the COM Linkage-COBOL - Using COBOL COM Server
Server Program (COBOL Client) - COM Client Functions
- *COM-ARRAY class
- Screen handling
Sample 27 Using the COM Linkage-COBOL - Using COBOL COM Server
Server Program (ASP Client) - ASP client
Sample 28 Transaction Management with COM - COM Server functions
Linkage-MTS - Object context object(MTS)
- Remote Database Access
- *COM-ARRAY class
Sample 29 Inter-application Communication - Inter-application communication
Function

Sample 30 Using UNICODE Sample 30 is not available in the English version of


NetCOBOL.
Sample 31 Windows System Function Call - Calling Windows API’s
- STDCALL calling convention
- BY VALUE parameters
- RETURNING phrase of a CALL statement
- PROGRAM-STATUS special register

Sample 32 Starting other programs - Calling Windows API’s


- STDCALL calling convention
- STORED-CHARACTER-LENGTH function

Using the Sample Program Projects


There are project files for each sample COBOL application. You can compile and link
any of them through the following steps:
1. Select the project file (by double clicking on it) under the appropriate samples
directory. For instance, the project file for Sample 1 will be on the drive where
you installed the product, under
%install_path%\Samples\cobol\Sample01; the filename is sample01.prj

(%install_path% is “C:\Program Files\Fujitsu NetCOBOL for Windows“ in default).


2. Once you have loaded the project file into COBOL Project Manager, you can
build (compile and link) the programs in the project by selecting Build from the
Project menu, or by pressing the F7 key. Refer to the section on Using Project
Manager in Chapter 2, “A Quick Tour” for more details as well as the “NetCOBOL
User’s Guide for Windows.”
3. All sample projects have the TEST compiler option set, so once you build the
projects you can monitor their execution under the COBOL Debugger by
selecting Debug from the Project menu in COBOL Project Manager. See the
128 Appendix A. Sample Programs

description of debugging in Chapter 2, “A Quick Tour” for more details as well as


the “NetCOBOL Debugging Guide.”
4. To execute the program select Execute from the Project menu. You need to
have the Project or the .EXE file selected in the project tree for the Execute
function to be enabled.
The description for each sample usually includes instructions to step you through one
or more aspects of the program and include different demonstrations of using the
tools that come with NetCOBOL.

Sample 1: Data Processing Using Standard Input-Output


Sample 1 demonstrates using the ACCEPT/DISPLAY function to input and output
data. Refer to the “NetCOBOL User’s Guide for Windows” for details on how to use
the ACCEPT/DISPLAY statements.

Function
Inputs an alphabetic character (lowercase character) from the console window, and
outputs a word beginning with the input alphabetic character to the console window.

Files Included in Sample 1


• SAMPLE1.COB (COBOL source program)
• SAMPLE01. PRJ (COBOL project file)
• SAMPLE01. CBI (COBOL compilation option file)

COBOL Statements Used


ACCEPT, DISPLAY, EXIT, IF, and PERFORM statements are used.

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, project file "SAMPLE01.PRJ" is opened.
Appendix A. Sample Programs 129

Figure A.1. The Sample 1 project in COBOL Projrct Manager

2. The project file is selected, and "Compiler options" is selected from "Project"-
"Options" menu.
→ The "Compiler options" dialog is displayed.

Figure A.2. Complier Options Dialog in the Sample 1 project

3. Confirm compiler option TEST is specified. After confirming the information, click
the OK button.
→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 SAMPLE01.EXE is created.
130 Appendix A. Sample Programs

Figure A.3. The Builder window after Sample 1 is built into executable format

NOTES:
1. If the sample has already been built you will just see the “Build Start” and “Build
End” messages.
2. The MAIN compile option is needed to indicate the main program in a NetCOBOL
application run-unit. In COBOL Project Manager you select Main - Window or Main –
Console from the pop-up menu when the COBOL source program is selected. All
sample programs have the appropriate program set as the main program.

Debugging the Program


These instructions demonstrate starting the debugger, stepping through a program,
changing a data item, setting and running to a breakpoint.
To start Sample 1 using the interactive debugger, do the following:
1. In COBOL Project Manager, from the Project menu select Debug.
The debugger starts and displays the Start Debugging dialog:
Appendix A. Sample Programs 131

Figure A.4. The Start Debugging dialog box.

2. Click on the OK button to enter the COBOL Debugger window.

Figure A.5. The COBOL Debugger window

3. Step through the program.


The program automatically positions at the first executable statement (Line 46).
Select Step Into, , from the Toolbar or press the F8 key to step (advance) to
132 Appendix A. Sample Programs

the next executable statement (Line 48). Step again to execute the Accept
statement on Line 48.
NOTE: The Accept statement requires user input.
4. Input the data item (a letter of the alphabet).
Make the console window active.
NOTE: The console window may be hidden behind the COBOL Debugger
window.
Type in any lowercase alphabetic character and then press the ENTER key. The
debugger automatically returns to the next executable statement (Line 52).

Figure A.6. The console window

5. Change the data name (input-character) to “A”.


Move the cursor to Line 48, inside the data name INPUT-CHARACTER. Click on
the magnifying glass in the toolbar or select Data from the Debug menu.
Alternatively, you can click on the right mouse button to select Data in the pop-
up menu. The Data dialog box is displayed.
Appendix A. Sample Programs 133

Figure A.7. The Data dialog box

Change the value of the data in the data value area and click on the Modify
button. Click on the Close button to exit the Data dialog box. (Answer “Yes” to
save the changed data.)
6. Specify a breakpoint.
Move the cursor to Line 62, click on the right mouse button and select Set
Breakpoint from the pop-up menu.
Note that the statement changes color when a breakpoint has been applied.
To delete a breakpoint, right click on the line containing the breakpoint and
select Delete Breakpoint from the pop-up menu. You can also use the
Breakpoint dialog box that is displayed by selecting Breakpoint from the Debug
menu. Highlight the breakpoint you want to delete, click on the Delete button,
and then click on the Close button to exit the Breakpoint dialog box.
NOTE: You can delete more than one breakpoint at a time by highlighting multiple
items in the Breakpoint dialog box and clicking on the Delete button.
134 Appendix A. Sample Programs

7. Execute the program up to the breakpoint.


Select Go in the Continue menu to execute the program to the breakpoint.
Alternatively, you can press the F5 key. The program stops at Line 62.

Figure A.8. Executing the program up to a breakpoint

Select Go or press the F5 key again to complete the debugging session.


8. To exit the Debugger, select Exit from the File menu.
Appendix A. Sample Programs 135

Sample 2: Using Line Sequential and Indexed Files


Sample 2 demonstrates a program that reads a data file (line sequential file) created
with the Editor, then creates a master file (indexed file). For details on how to use
line sequential files and indexed files, refer to the “NetCOBOL User’s Guide for
Windows”.

Overview
Reads a data file (line sequential file) that contains product codes, product names,
and unit prices, and creates an indexed file with the product code as a primary
record key and the product name as an alternate record key.

Figure A.9. Creating an indexed file from a line sequential file

Files Included in Sample 2


• SAMPLE2.COB (COBOL source program)
• SAMPLE02. PRJ (COBOL project file)
• SAMPLE02. CBI (COBOL compilation option file)
• COBOL85.CBR (COBOL run-time initialization file)
• DATAFILE(Data file)

COBOL Statements Used


The CLOSE , EXIT , GO TO , MOVE , OPEN , READ , and WRITE statements are used.

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 "SAMPLE02.PRJ" is opened.


136 Appendix A. Sample Programs

Figure A.10. The Sample 2 project in the COBOL Project Manager

2. 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 SAMPLE02.EXE is created.

Setting Run-time Environment Information


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 (SAMPLE02.EXE).
3. Select the Common tab and enter data as shown below:
- For the file-identifier INFILE, specify DATAFILE that is the file name of data
file (line sequential file).
- For the file-identifier OUTFILE, specify MASTER that is the file name of
master file (indexed file).
4. If MASTER is specified for OUTFILE, input data as follow and push the "Set"
button.
Appendix A. Sample Programs 137

Figure A.11. The Run-time Environment Setup Tool with COBO85.CBR for Sample02.

5. Click the Apply button.


→ The data is saved in the object initialization file.
6. Select "Exit" on the "File" menu to terminate the run-time environment setup
tool.

Debugging the Program


To run this program under the control of the Debugger, select Debug from the
Project menu. The Debugger starts and displays the “Start Debugging” dialog box.
Press the ENTER key here and you will be taken into the Debugger.

Executing the Program


Once the project is built, you can run Sample 2 by double-clicking the mouse on
SAMPLE2.EXE in the COBOL Project Manager window.
138 Appendix A. Sample Programs

Execution Result
No termination message is displayed.
After completion of execution, an indexed file (MASTER) with a product code as a
key is created in the SAMPLE02 directory. Use Windows Explorer or File Manager to
check whether the indexed file was created.

Sample 3: Screen Input-Output Using the Presentation File


Function
Sample 3 is not available in the English version of NetCOBOL.

Sample 4: Screen Input-Output Using the Screen Section


Sample 4 demonstrates using the Screen Section (the “screen handling function”)
to display and accept data. Refer to the “NetCOBOL User’s Guide for Windows” for
details on how to use the screen handling function.

Overview
When an employee's number and name are written to the screen, the program
creates an indexed file with the employee's number as a primary record key and the
name as an alternate record key.

Files Included in Sample 4


• SAMPLE4.COB (COBOL source program)
• SAMPLE04. PRJ (COBOL project file)
• SAMPLE04. CBI (COBOL compilation option file)
• COBOL85.CBR (COBOL run-time initialization file)
• SAMPLE4.KDB(key definition file)

COBOL Statements Used


The ACCEPT, CLOSE, DISPLAY, EXIT, GO TO, IF, MOVE, OPEN, and WRITE
statements are used.
Appendix A. Sample Programs 139

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 "SAMPLE04.PRJ" is opened.

Figure A.12. The Sample 4 project in the COBOL Project Manager

2. 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 SAMPLE04.EXE is created.

Setting Run-time Environment Information


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 (SAMPLE04.EXE).
3. Select the Common tab and enter data as shown below:
- For the file-identifier OUTFILE, specify MASTER that is the file name of
master file (indexed file).
- For the environment variable @CBR_SCR_KEYDEFFILE, specify
SAMPLE4.KDB that is the file name of key definition file defined only for the
input F2 key to become effective.
140 Appendix A. Sample Programs

Figure A.13. The Run-time Environment Setup Tool with COBO85.CBR for Sample04.

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.

Debugging the Program


To run this program under the control of the Debugger, select Debug from the
Project menu. The Debugger starts and displays the “Start Debugging” dialog box.
Press the ENTER key here and you will be taken into the Debugger.
Executing the Program
Execute Sample 4 by double-clicking the mouse on SAMPLE04.EXE in the COBOL
Project Manager window.
The screen for entering an employee's number and name is displayed.
Appendix A. Sample Programs 141

Figure A.14. Sample 4 employee number and name input screen

Input the data:


Enter an employee's number (6 digit number) and name (up to 40 alphanumeric
characters).

Figure A.15. Entering data for the master file


142 Appendix A. Sample Programs

To register data, press the ENTER key. The input data is written to the master file,
and the screen is cleared for input of subsequent data.
To quit processing, press the F2 key.

Execution Result
Because the employee number becomes a main record key and the employee name
becomes a record sub key, you should use Windows Explorer to confirm that the
index file (MASTER) was created in the SAMPLE04 directory.
NOTE: You can edit the MASTER file using the Edit command in the COBOL File
Utility (available from the Tools menu in the COBOL Project Manager window).
Appendix A. Sample Programs 143

Sample 5: Calling COBOL Subprograms


Sample 5 demonstrates an application that calls a subprogram from the main
program. Sample 5 was created using free format source.
It also demonstrates how to pass an argument string to a program and how to
display a message box.

Overview
Reads the contents of the master file (indexed file created in Sample 2), stores the
records in a work file whose name is provided in the @MGPRM environment variable
(a way of passing information to a main program’s linkage section), then passes the
work file to a subprogram which prints the records.
The master file stores product codes, product names, and unit prices. The work file
name must be specified in the @MGPRM parameter at program execution.

Files Included in Sample 5


• SAMPLE5.COB (COBOL source program)
• PRINTPRC.COB (COBOL source program)
• S_REC.CBL (COBOL library file)
• SAMPLE05. PRJ (COBOL project file)
• SAMPLE04. CBI (COBOL compilation option file)
• COBOL85.CBR (COBOL run-time initialization file)

COBOL Statements Used


The CALL, DISPLAY, EXIT, GO TO, MOVE, OPEN, READ, and WRITE statements are
used.
144 Appendix A. Sample Programs

Using Free Format in a COBOL Source Program


The following is an example of using free format in a COBOL source program.

Figure A.16. A COBOL source program using FREE format

NOTE: In the above figure, colons are used to denote sections of source code that
have been omitted.
In free format, COBOL statements can be written in any character position on the
line. Lines beginning with “*>“ are treated as comments.
NOTE: You must specify the SRF compiler option in order to use free format. The
SRF compiler option has two parameters; the first specifies the format for the source
program and the second specifies the format of copybooks. All copybooks must have
the same format type. The available types are FIX, for fixed format source, VAR, for
variable format source, and FREE, for free format source.
Appendix A. Sample Programs 145

File Interdependence
Figure A.17 shows the relationship between sources files used in Sample05.

Source file – SAMPLE5.COB Library text – S_REC.CBL


IDENTIFICATION DIVISION.
PROGRAM-ID. SAMPLE5. Record definition of work file

COPY "S_REC".
Source file – PRINTPRC.COB
[Read master file]
IDENTIFICATION DIVISION.
[Create work file] PROGRAM-ID. PRINTPRC.

CALL "PRINTPRC" … COPY "S_REC".

[Print processing]

Figure A.17. Interdependence between SAMPLE5.COB and PRINTPRC.COB

Prerequisite to Executing the Program


The master file created in Sample 2 is used. Therefore, execute the program in
Sample 2 before executing Sample 5.

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, project file "SAMPLE05.PRJ" is opened.
146 Appendix A. Sample Programs

Figure A.18. The Sample 5 project in the COBOL Project Manager

2. This project file contains the following two TARGET.


- SAMPLE05.EXE (executable file name of main program)
- PRINTPRC.DLL (DLL file name of subprogram)

3. The project file is selected, and "Compiler options" is selected from "Project"-
"Options" menu.
→ The "Compiler options" dialog is displayed.

Figure A.19. Complier Options Dialog in the Sample 5 project


Appendix A. Sample Programs 147

4. Confirm compiler option SRF(FREE,FREE) is specified. After confirming the


information, click the OK button.
→You are now returned to the Project Manager window.

3. 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 SAMPLE05.EXE and PRINTPRC.DLL are
created.

Setting Run-time Environment Information


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 (SAMPLE05.EXE).
3. Select the Common tab and enter data as shown below:
- For the file-identifier INFILE, specify the path name of the master file
(MASTER) created in Sample 2.
- A work file name in the @MGPRM parameter. The string in this parameter is
passed to the first linkage section item in the executing program. The work
file name can contain up to 8 alphanumeric characters. The extension “TMP”
is added to the work file name before the file is created.
148 Appendix A. Sample Programs

Figure A.20. The Run-time Environment Setup Tool with COBO85.CBR for Sample05.

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.

Debugging the Program


To run this program under the control of the Debugger, select Debug from the
Project menu. The Debugger starts and displays the “Start Debugging” dialog box.
Press the ENTER key here and you will be taken into the Debugger.

Executing the Program


To execute the sample program, click on the Execute button in the Project Manager
window.
The message “GENERATE WORK-FILE=sample5.TMP” is displayed.
Appendix A. Sample Programs 149

Figure A.21. The Sample 5 message box

Confirm the contents, then click on the OK button to close the message box.

Execution Result
The master file contents are written to the default printer at the completion of
program execution.
150 Appendix A. Sample Programs

Sample 6: Receiving a Command Line Argument


Sample 6 demonstrates a program that receives an argument specified at program
execution, using the command line argument handling function (ACCEPT FROM
argument-name/argument-value). Refer to “Using ACCEPT and DISPLAY
Statements” in the “NetCOBOL User’s Guide for Windows” for details on how to
use the command line argument handling function.
Sample 6 also calls an internal program.

Overview
The sample program calculates the number of days from the start date to the end
date. The start and end dates are specified as command arguments in the following
format:

command-name start-date end-date

START-DATE , END-DATE:
Specify a year, month, and day between January 1, 1900 and December 31,
2172 in the YYYYMMDD format.

Files Included in Sample 6


• SAMPLE6.COB (COBOL source program)
• SAMPLE06. PRJ (COBOL project file)
• SAMPLE06. CBI (COBOL compilation option file)

COBOL Statements Used


The ACCEPT, CALL, COMPUTE, COPY, DISPLAY, DIVIDE, EXIT, GO TO, IF, MOVE,
and PERFORM statements are used.

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 defferent.
1. The project manager is started, and project file "SAMPLE06.PRJ" is opened.
Appendix A. Sample Programs 151

Figure A.22. The Sample 6 project in the COBOL Project Manager

2. 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 SAMPLE06.EXE is created.

Debugging the Program


To run this program under the control of the Debugger, select Debug from the
Project menu. The COBOL Debugger starts and displays the Start Debugging dialog
box. The application name is already entered for you, you need to enter the
execution-time options (command line arguments) the program expects – a start and
end date. For example:

Figure A.23. Setting the Execution-time options for Sample 6


152 Appendix A. Sample Programs

After you type in the command-line parameters for the program in the “Execution-
time options” field, press the ENTER key. The Debugger starts as shown in the
following figure.

Figure A.24. The COBOL Debugger at the start of Sample 6

Executing the Program


To execute the program from COBOL Project Manager, you need to select “Execute
with Arguments” from the Project menu.
Enter the start date and end date arguments in the “Specify Arguments” dialog.

Figure A.25. The Specify Arguments dialog

Refer to Chapter 2, “A Quick Tour” for details on using the COBOL Project Manager.
Appendix A. Sample Programs 153

Execution Result
Sample 6 displays the number of days from the specified start date to the specified
end date.

Figure A.26. Execution results are displayed in the console window


154 Appendix A. Sample Programs

Sample 7: Environment Variable Handling


Sample 7 demonstrates a program that changes the value of an environment
variable during COBOL program execution, using the environment variable handling
function (ACCEPT FROM/DISPLAY UPON environment-name/environment-value).
Refer to “Using ACCEPT and DISPLAY Statements” in the “NetCOBOL User’s Guide
for Windows” for details on how to use the environment variable handling function.

Overview
The sample program divides a master file (the indexed file created in Sample 2),
storing product codes, product names, and unit prices, into two master files
according to product codes. The following table shows the division method and the
names of the two new master files:

Table A.2. Division of the master files


Product Code File Name
Code beginning with 0 master-file-name.A
Code beginning with a non-zero value master-file-name.B

Files Included in Sample 7


• SAMPLE7.COB (COBOL source program)
• SAMPLE07. PRJ (COBOL project file)
• SAMPLE07. CBI (COBOL compilation option file)
• COBOL85.CBR (COBOL run-time initialization file)

COBOL Statements Used


The ACCEPT, CLOSE, DISPLAY, EXIT, GO TO, IF, OPEN, READ, STRING, and WRITE
statements are used.

Prerequisite to Executing the Program


The master file created in Sample 2 is used. Therefore, execute the program in
Sample 2 before executing Sample 7.

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 "SAMPLE07.PRJ" is opened.


Appendix A. Sample Programs 155

Figure A.27. The Sample 7 project in the COBOL Project Manager

2. 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 SAMPLE07.EXE is created.

Setting Run-time Environment Information


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 (SAMPLE07.EXE).
3. Select the Common tab and enter data as shown below:
- For the file-identifier INFILE, specify the path name of the master file
(MASTER) created in Sample 2.
156 Appendix A. Sample Programs

Figure A.28. The Run-time Environment Setup Tool with COBO85.CBR for Sample07.

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.

Debugging the Program


To run this program under the control of the Debugger, select Debug from the
Project menu. The Debugger starts and displays the Start Debugging dialog box.
Click the OK button or press the ENTER key. You will be taken into the Debugger as
shown in the following figure.
Appendix A. Sample Programs 157

Figure A.29. The COBOL Debugger at the start of Sample 7

Executing the Program


To execute the program from COBOL Project Manager, select Execute from the
Project menu. There are no command line arguments.
NOTE: Execute the program in Sample 2 beforehand.

Execution Result
The following two files are created in the directory of the master file created in
Sample 2:
• MASTER.A: Stores the data of products whose codes begin with 0.
• MASTER.B: Stores the data of products whose codes begin with a non-zero
value.
The contents of the newly created master files (MASTER.A and MASTER.B) can be
checked with the program in Sample 5, in the same manner as for the master file
created in Sample 2.
158 Appendix A. Sample Programs

Sample 8: Using a Print File


Sample 8 demonstrates a program that outputs data (input from the console
window) to a printer using a print file. Refer to “Printing” in the “NetCOBOL
User’s Guide for Windows” for details on using a print file.

Overview
The sample program inputs data of up to 40 alphanumeric characters from the
console window, and outputs the data to the printer.

Files Included in Sample 8


• SAMPLE8.COB (COBOL source program)
• SAMPLE08. PRJ (COBOL project file)
• SAMPLE08. CBI (COBOL compilation option file)

COBOL Statements Used


The ACCEPT, CLOSE, EXIT, GO TO, IF, OPEN, and WRITE statements are used.

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 "SAMPLE08.PRJ" is opened.

Figure A.30. The Sample 8 project in the COBOL Project Manager

2. Select "Build" from Project Manager's "Project" menu or click on the Build
button in the Project Manager toolbar.
Appendix A. Sample Programs 159

→ After the build terminates, check that SAMPLE08.EXE is created.

Debugging the Program


To run this program under the control of the Debugger, select Debug from the
Project menu. The Debugger starts and displays the “Start Debugging” dialog box.
Press the ENTER key here and you will be taken into the Debugger, as shown in the
following figure.

Figure A.31. The COBOL Debugger at the start of Sample 8

Executing the Program


To execute in COBOL Project Manager, select Execute from the Project menu. There
are no command line arguments.
A console window is then displayed.
160 Appendix A. Sample Programs

In the console window, enter the data to be printed . Data of up to 40 characters can
be entered at a time.

Figure A.32. Entering data to be printed in the console window

To terminate the program, press the RETURN key, type /END and press the
RETURN key. Click on the OK button to close the message window.

Execution Result
The input data is written to the printer at termination of the program.

12345678901234567890123456789
01234567890
abcd
*************SAMPLE8*********
***********

Figure A.33. Input data is written to the printer


Appendix A. Sample Programs 161

Sample 9: Using a Print File (Advanced usage)


Sample 9 demonstrates:
• Using a print file without a FORMAT clause;
• Using the I control record to set and change page forms, in combination with
Forms Control Buffers (FCBs);
• Using the CHARACTER TYPE clause to control letter size and pitch; and
• Using the PRINTING POSITION clause to control the layout (line / column).

Refer to “Printing” in the “NetCOBOL User's Guide for Windows” for details on using
“print file 1” and “print file 2”.

Overview
Table A.3 below describes each of the tasks performed by this sample. The tasks
show a number of printing features. There are essentially four elements that give
you control over the various printing features:
1. COBOL syntax
- PRINTING MODE clauses in the SPECIAL-NAMES paragraph.
- ASSIGN TO PRINTER in the SELECT statement.
- CHARACTER TYPE and PRINTING POSITION clauses in data definitions.
2. The I-Control Record
A record that you write to the print file using the syntax:
“CTL IS page-cntl” in SPECIAL-NAMES
and
WRITE I-Control-Record AFTER ADVANCING page-cntl
3. Forms Control Buffers (FCBs)
These are form information buffers stored by the COBOL run-time system, using
information defined in environment variables of the form “FCBxxxx=”.
4. Environment variables
Environment variables define fonts, FCBs, document names and other printing
details.
Table A.3 indicates which of the above are used to provide a particular feature. You
will need to read the table, inspect the COBOL code, and consult the chapter on
“Printing” in the “NetCOBOL User's Guide for Windows” to fully understand all the
features being demonstrated.
162 Appendix A. Sample Programs

Table A.3. Features demonstrated in Sample 9


Task Description Detailed features Controlled by Related Environment
I-Control field / Variable(s)
COBOL clause
1a. Prints a page at 6 6 LPI – defined in FCB I-Control: FCBLT6L=…
LPI, 10 CPI on a FCB-NAME (=”LT6L”)
PowerFORM overlay grid
10 CPI PRINTING MODE x …
AT PITCH …
+
CHARACTER TYPE x
Letter size paper I-Control:
PAPER-SIZE (=”LTR”)
Impact font PRINTING MODE x @PrinterFontName=
… WITH FONT GOTHIC (…, Impact)

+
CHARACTER TYPE x
Courier New font PRINTING MODE x @PrinterFontName=
… WITH FONT (Courier New, …)
MINCHOU …
+
CHARACTER TYPE x
Grid (PowerFORM I-Control: FOVLTYPE=KOL6
overlay – FOVL-NAME (=”LT6L”) OVD_SUFFIX=OVD
KOL6LT6L.OVD)
FOVL-R (= 1 – to use
overlay on a single
page)
Data item position PRINTING POSITION
within line
Different character type PRINTING MODE x
forms … FORM …
+
CHARACTER TYPE x
Document name I-Control: @CBR_DocumentName_
displayed by Windows DOCUMENT-NAME DOC1=<document
(=DOC1) name string>
Appendix A. Sample Programs 163

Table A.3. Features demonstrated in Sample 9 (cont.)


Task Description Detailed features Controlled by Related Environment
I-Control field / Variable(s)
COBOL clause
1b. Prints a page at 8 8 LPI – defined in FCB I-Control: FCBLT8L=…
LPI, 10 CPI on a FCB-NAME (=”LT8L”)
PowerFORM overlay grid
10 CPI PRINTING MODE x …
AT PITCH …
+
CHARACTER TYPE x
Letter size paper I-Control:
PAPER-SIZE (=”LTR”)
Impact font PRINTING MODE x @PrinterFontName=
… WITH FONT GOTHIC (…, Impact)

+
CHARACTER TYPE x
Courier New font PRINTING MODE x @PrinterFontName=
… WITH FONT (Courier New, …)
MINCHOU …
+
CHARACTER TYPE x
Grid (PowerFORM I-Control: FOVLTYPE=KOL6
overlay – FOVL-NAME (=”LT8L”) OVD_SUFFIX=OVD
KOL6LT8L.OVD)
FOVL-R (= 1 – to use
overlay on a single
page)
Data item position PRINTING POSITION
within line
Different character type PRINTING MODE x
forms … FORM …
+
CHARACTER TYPE x
164 Appendix A. Sample Programs

Table A.3. Features demonstrated in Sample 9 (cont.)


Task Description Detailed features Controlled by Related Environment
I-Control field / Variable(s)
COBOL clause
2a. Prints letters in font Document name I-Control: @CBR_DocumentName_
sizes: 3, 7.2, 9, 12, 18, displayed by Windows DOCUMENT-NAME DOC1=<document
24, 36, 50, 72, 100, (=DOC1) name string>
200, and 300 points.
On legal-sized paper Different font sizes PRINTING MODE x
(After printing a header … IN SIZE nn POINT …
page) +
CHARACTER TYPE x
COBOL run time system Legal size paper I-Control: @PRN_FormName_XXX
automatically calculates =Legal 8 1/2 x 14 in
PAPER-SIZE (=”XXX”)
the best character pitch
FCB-NAME (=”LPI6”) FCBLPI6=…
fitted to the character
size (character pitch
specification is omitted). Impact font Default – Gothic font @PrinterFontName=
(…, Impact)
Shaded background I-Control: FOVLTYPE=KOL6
(PowerFORM overlay – FOVL-NAME (=”LGLT”) OVD_SUFFIX=OVD
KOL6LGLT.OVD)
FOVL-R (= 1 – to use
overlay on a single
page)
Document name I-Control: @CBR_DocumentName_
displayed by Windows DOCUMENT-NAME DOC2=<document
(=DOC2) name string>

2b. Prints characters at Different pitches PRINTING MODE x


pitches: 1, 2, 3, 5, 6, … AT PITCH n CPI …
7.5, 20, and 24 CPI.
+
CHARACTER TYPE x
Here, the COBOL run
time system Legal size paper I-Control: @PRN_FormName_XXX
automatically calculates PAPER-SIZE (=”XXX”) =Legal 8 1/2 x 14 in
the best character size FCBLPI6=…
FCB-NAME (=”LPI6”)
fitted to the character
pitch (the character size
specification is omitted). Impact font Default – Gothic font @PrinterFontName=
(…, Impact)
Shaded background I-Control: FOVLTYPE=KOL6
(PowerFORM overlay – FOVL-NAME (=”LGLT”) OVD_SUFFIX=OVD
KOL6LGLT.OVD)
FOVL-R (= 1 – to use
overlay on a single
page)
Document name I-Control: @CBR_DocumentName_
displayed by Windows DOCUMENT-NAME DOC2=<document
(=DOC2) name string>
Appendix A. Sample Programs 165

Table A.3. Features demonstrated in Sample 9 (cont.)


Task Description Detailed features Controlled by Related Environment
I-Control field / Variable(s)
COBOL clause
2c. Prints characters in: Impact font PRINTING MODE x @PrinterFontName=
Impact, … WITH FONT {GOTHIC (…, Impact)

Impact half-size, }
Courier New, Courier {GOTHIC-HANKAKU} …
New half size. +
CHARACTER TYPE x
Courier New font PRINTING MODE x @PrinterFontName=
… WITH FONT (Courier New, …)
{MINCHOU
}
{MINCHOU-HANKAKU}

+
CHARACTER TYPE x
Full size PRINTING MODE x
… BY FORM F …
+
CHARACTER TYPE x
Half size PRINTING MODE x
… BY FORM H …
+
CHARACTER TYPE x
Legal size paper I-Control: @PRN_FormName_XXX
PAPER-SIZE (=”XXX”) =Legal 8 1/2 x 14 in

FCB-NAME (=”LPI6”) FCBLPI6=…

Shaded background I-Control: FOVLTYPE=KOL6


(PowerFORM overlay – FOVL-NAME (=”LGLT”) OVD_SUFFIX=OVD
KOL6LGLT.OVD)
FOVL-R (= 1 – to use
overlay on a single
page)
Document name I-Control: @CBR_DocumentName_
displayed by Windows DOCUMENT-NAME DOC2=<document
(=DOC2) name string>
166 Appendix A. Sample Programs

Table A.3. Features demonstrated in Sample 9 (cont.)


Task Detailed features Controlled by Related Environment
Description I-Control field / Variable(s)
COBOL clause
2d. Prints Em-size PRINTING MODE x
characters in … BY FORM F …
different form
sizes: +
Em-size, CHARACTER TYPE x
en-size, En-size As above with:
expanded em- … BY FORM H …
size,
Expanded em-size As above with:
expanded en-
… BY FORM F0201 …
size,
tall em-size, Expanded en-size As above with:
tall en-size, … BY FORM H0201 …
double em-size Tall em-size As above with:
and
… BY FORM F0102 …
double en-size.
Tall en-size As above with:
… BY FORM H0102 …
Double em-size As above with:
… BY FORM F0202…
Double en-size As above with:
… BY FORM H0202…
Legal size paper I-Control: @PRN_FormName_XXX
PAPER-SIZE (=”XXX”) =Legal 8 1/2 x 14 in

FCB-NAME (=”LPI6”) FCBLPI6=…

Shaded background I-Control: FOVLTYPE=KOL6


(PowerFORM overlay – FOVL-NAME (=”LGLT”) OVD_SUFFIX=OVD
KOL6LGLT.OVD)
FOVL-R (= 1 – to use
overlay on a single
page)
Document name I-Control: @CBR_DocumentName_
displayed by Windows DOCUMENT-NAME DOC2=<document
(=DOC2) name string>

2e. Prints a
mixture of the
above
features: font
size, pitch,
half/full size
characters.
Appendix A. Sample Programs 167

Files Included in Sample 9


• SAMPLE9.COB (COBOL source program)
• COBOL85.CBR (Environment variable initialization file)
• KOL5A4L6.OVD (Form overlay pattern)
• KOL5A4L8.OVD (Form overlay pattern)
• KOL5B4OV.OVD (Form overlay pattern)
• KOL5A4L6.PDM (Form descriptor)
• KOL5A4L8.PDM (Form descriptor)
• KOL5B4OV.PDM (Form descriptor)

COBOL Statements Used


The ADD, CLOSE, DISPLAY, IF, MOVE, OPEN, PERFORM, STOP, and WRITE
statements are used.

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 "SAMPLE09.PRJ" is opened.

Figure A.34. The Sample 9 project in the COBOL Project Manager

2. 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 SAMPLE09.EXE is created.
168 Appendix A. Sample Programs

Checking Execution Environment Information


Sample 9 uses a number of run-time environment variables that play an important
part in achieving the demonstrated features.
To check these variables:
1. From the COBOL Project Manager Tools menu, select “Run-time Environment
Setup Tool”.
The Run-time Environment Setup Tool Window opens.
2. From the File menu select Open, and select COBOL85.CBR in the sample09
folder. The window should look like Figure A.35 below.

Figure A.35. COBOL85.CBR for Sample 9 in Run-Time Environment Setup Tool

3. Check the setting of environment variable FOVLDIR in the list of environment


variables. If it is not set to your location for the sample09 folder, change it to
that value by:
a) Selecting FOVLDIR in the environment variable list. “FOVLDIR” will be
displayed in the Variable Name field, and its current setting in the Variable Value
field.
b) Use the browse (“…”) button to navigate to the sample09 folder, select any
file, and click OK. The path and filename are returned to the Variable Value
field.
c) Delete the last “\” and the file name that follows it from the string in the
Appendix A. Sample Programs 169

Variable Value field.


d) Click the Set button, to set your change in the Section list of environment
variables.
e) Click the Apply button, to save your changes to the COBOL85.CBR file.
4. When you have finished reviewing the environment variables, select Exit from
the File menu.

Debugging the Program


To run this program under the control of the Debugger, select Debug from the
Project menu. The Debugger starts and displays the “Start Debugging” dialog box.
Press the ENTER key here and you will be taken into the Debugger.

Executing the Program


To execute in COBOL Project Manager, select Execute from the Project menu. There
are no command line arguments.

Execution Results
The sample pages described in table A.3 above are printed to the default printer.

Sample 10: Using a Print File with FORMAT clause


Sample 10 is not available in the English version of NetCOBOL.
170 Appendix A. Sample Programs

Sample 11: Remote Database Access


Sample 11 extracts data from a database and assigns it to a host variable using the
SQL database function.
In normal operation, the database is placed on a server and is accessed by the client
via an ODBC driver.
However, a database file is included with this sample to enable you to use a
relational database off-line. This database file—called STOCK.MDB-- includes the
Microsoft Access run-time support. It can therefore be used as long as Open
Database Connectivity (ODBC) is installed and configured properly.
For more about using ODBC drivers, refer to the “Database (SQL)” chapter in the
“NetCOBOL User’s Guide for Windows”, and the relevant documentation from your
database vendor.
To run this sample program in a true distributed configuration, the following products
are required:
• Client
− ODBC driver manager
− ODBC driver
− Products needed for the ODBC driver
• On the server
− Database
− Products needed for accessing the database via ODBC

Overview
The sample program accesses the database on the server and outputs all data stored
in the database table "STOCK" to the client console. When all data has been
referenced, the link to the database is disconnected.

Files Included in Sample 11


• SAMPLE11.COB (COBOL source program)
• SAMPLE11.PRJ (COBOL project file)
• SAMPLE11.CBI (COBOL compiler option file)
• COBOL85.CBR (COBOL run-time initialization file)
• SAMPLE11.INF (ODBC information file)

• STOCK.MDB (Sample database for Microsoft Access)

COBOL Statements Used


The DISPLAY, IF and PERFORM statements are used.
Appendix A. Sample Programs 171

Embedded SQL statements (embedded exception declarations and CONNECT,


DECLARE CURSOR, OPEN, FETCH, CLOSE, ROLLBACK, and DISCONNECT
statements) are also used.

Prerequisite to Executing the Program


ODBC is a defined interface that attempts to provide a highly generic API into any
database system that provides compliant drivers. Just about every database system
available today provides ODBC drivers for a variety of platforms.
In order to execute this sample, you must have already installed 32-bit ODBC
support on your Windows 98, Me, 2000, XP, Windows Server 2003 or Windows NT
system, along with the Microsoft Access Driver.

Verifying Whether ODBC is installed


In order to verify whether your system has the proper support installed, look in your
Administrative Tools for the icon of ODBC data source administrator.
An example is given below.

Figure A.36. Control Panel with Data Sources (ODBC) Icon Highlighted

Double-click on this icon.


Select the Drivers (or ODBC Drivers) tab and verify that the Microsoft Access Driver
(*.mdb) is installed. The driver is shown highlighted in the following figure.
172 Appendix A. Sample Programs

Figure A.37. The Control Panel’s ODBC Data Source Administrator

NOTE: Depending on the version of Microsoft Access, the ODBC drivers may not be
installed by default - you must manually select these options when you install
Microsoft Access (either from the Microsoft Access installation CD or the Office
Professional CD).
You will also require a sample Microsoft Access run-time enabled database called
"STOCK.MDB". This file can be found in the SAMPLE11 folder.
Once you have verified installation of 32 Bit ODBC, Microsoft Access and the
associated drivers, you are ready to proceed with executing the sample.

Defining an ODBC Source for Your Application


The first development task that needs to be accomplished is to define an ODBC
Source for the application.
An ODBC Source can be thought of as simply specifying to the ODBC manager a
symbolic name that you wish to use in your application to describe an ODBC
connection to a specific ODBC database driver and database.
The following figures may or may not match what you see on your particular
operating system, but you should be able to perform the following tasks by using the
instructions provided.
Bring up the Administrative Tools and double click on the Data Sources (ODBC) icon.
The main ODBC Data Source Administrator window is displayed.
Appendix A. Sample Programs 173

Figure A.38. The main ODBC Data Source Administrator window

You will add a new User DSN. Select the User DSN tab, and click on the Add button.
ODBC Data Source Administrator displays the “Create New Data Source” dialog box.

Figure A.39. The Create New Data Source dialog box


174 Appendix A. Sample Programs

Select the Microsoft Access Driver (*.mdb) and click on the Finish button.
The ODBC Microsoft Access Setup dialog box is displayed.

Figure A.40. The ODBC Microsoft Access Setup dialog box

Enter Stock in the Data Source Name field and Sample 11 Database in the
Description field as shown above.
You also need to select a file (database) to bind this driver to. Click on the Select
button in the ODBC Microsoft Access Setup dialog box. The Select Database dialog
box is displayed. Navigate through the directories and find the "Stock.mdb" database
file:

Figure A.41. The Select Database dialog box

Click on the OK button. This will take you back to the ODBC Microsoft Access Setup
dialog box.
Appendix A. Sample Programs 175

Click on the OK button. This will take you back to the main ODBC Source
Administrator window.
Note that "Stock" has been added to the list in User DSN. Although you should not
set any additional options for this exercise, you may wish to highlight “Stock” and
click on the Configure button.
This will give you an idea of some of the additional options that are available when
developing these types of applications. Once you are back to the main ODBC Source
Administrator window, click on the OK button. This will exit the ODBC configuration
facility. You have now defined the new ODBC Source (Stock) you are going to use
with COBOL.

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 "SAMPLE11.PRJ" is opened.

Figure A.42. The Sample 11 project in the COBOL Project Manager

2. 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 SAMPLE11.EXE is created.

Setting ODBC information


The ODBC information file is necessary for the execution of this sample program.
When connecting it with the database by using the ODBC driver, necessary
information is included as for the ODBC information file.
The ODBC information file is made by using ODBC information Setup Tool
(SQLODBCS.EXE). Refer to the “Database (SQL)” chapter in the “NetCOBOL User’s
Guide for Windows” for more detailed information.
The following figures show the place where information necessary to connect it with
an attached sample database by this sample program was set.
176 Appendix A. Sample Programs

Figure A.43. ODBC information setup for the Sample 11

NOTES: Information shown up is included in SAMPLE11.INF. When data bases


other than Microsoft Access are used, setting other items might become
necessary.

Setting Run-time Environment Information


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 (SAMPLE11.EXE).
3. Select the Common tab and enter data as shown below:
- For the environment variable @ODBC_Inf , specify SAMPLE11.INF.
Appendix A. Sample Programs 177

Figure A.44. The Run-time Environment Setup Tool with COBO85.CBR for Sample11.

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.

Debugging the Program


To run the program under the control of the Debugger, select Debug from the
Project menu. The Debugger starts and displays the Start Debugging dialog box.
Press the ENTER key.
When the source is read into the COBOL Debugger, it will look like the following
figure.
178 Appendix A. Sample Programs

Figure A.45. Sample 11 in the COBOL Debugger

Executing the Program


To execute this program, you can double-click on SAMPLE11.EXE in the Project Tree
or select Execute from the Project menu, with the project or .EXE file selected.

Execution Result
The data extracted from the table is displayed, as shown in the following figure.
Appendix A. Sample Programs 179

Figure A.46. The results displayed by Sample 11


180 Appendix A. Sample Programs

Sample 12: Remote Database Access (Multiple row


processing)
A sample program (sample 12) provided with this product is explained below.
Sample 12 shows an example where two or more lines in a database are operated
with one SQL statement as an example of advanced usage of the database (SQL)
function.
A database is present in a server and is accessed from a client.
A database is accessed via the ODBC driver. For details of database access using the
ODBC driver, refer to Chapter 19 in the “NetCOBOL User’s Guide for Windows”.
To use this program, the following products are necessary.
Client:
• ODBC driver manager
• ODBC driver
• Products necessary for ODBC driver
Server:
• Database
NOTE:
The function to use with this sample depends on the your database. With
Microsoft® Access, we could not execute this sample correctly.
• Products necessary for database access using ODBC

Overview
Sample 12 uses the STOCK table of the sample database. Refer to “Sample
Database” in the “NetCOBOL User’s Guide for Windows” for details of the sample
database. Sample 12 accesses and disconnects it after the following operation:
• Display of all data items in the database
• Fetch of the GNO value on a row with GOODS value "TELEVISION"
• QOH update on a row with the fetched GNO
• Deletion of lines with GOODS values "RADIO", "SHAVER", and "DRIER"
• Redisplay of all data items in the database
Part of the output result is output to a file by using compiler option SSOUT.

Programs and files in Sample 12


• SAMPLE12.COB (COBOL source program)
• SAMPLE12.PRJ (Project file)
• SAMPLE12.CBI (COBOL compiler option file)
• COBOL85.CBR (COBOL run-time initialization file)
Appendix A. Sample Programs 181

• SAMPLE12.INF (ODBC information file)

COBOL functions used in sample 12


• Remote database access
• Project management function

COBOL statements used


The CALL, DISPLAY, IF, and PERFORM statements are used.
Embedded SQL statements (host variable with multiple rows specified, host variable
with a table specified, embedded exception declaration, CONNECT statement, cursor
declaration, OPEN statement, FETCH statement, SELECT statement, DELETE
statement, UPDATE statement, CLOSE statement, COMMIT statement, ROLLBACK
statement, and DISCONNECT statement) are used.

Prerequisite to Executing the Program


In order to execute this sample, the DBMS product which can be connected via
ODBC is installed in server side and make the table named STOCK for the database
connected by default.
Make the STOCK table in the format as following.
GNO GOODS QOH WHNO ←Column name
Binary Fixed-length character Binary Binary integer ←Column
integer 10 bytes integer 4 digits attribute
4 digits 9 digits

Store the data items shown following in the STOCK table.


GNO GOODS QOH WHNO
110 TELEVISION 85 2
111 TELEVISION 90 2
123 REFRIGERATOR 60 1
124 REFRIGERATOR 75 1
137 RADIO 150 2
138 RADIO 200 2
140 CASSETTE DECK 120 2
141 CASSETTE DECK 80 2
200 AIR CONDITIONER 4 1
201 AIR CONDITIONER 15 1
212 TELEVISION 0 2
215 VIDEO 5 2
226 REFRIGERATOR 8 1
227 REFRIGERATOR 15 1
240 CASSETTE DECK 25 2
243 CASSETTE DECK 14 2
182 Appendix A. Sample Programs

GNO GOODS QOH WHNO


351 CASSETTE TAPE 2500 2
380 SHAVER 870 3
390 DRIER 540 3

Building and rebuilding the program


The project manager's build function is used to compile and link this program.
Folder C:\NetCOBOL is assumed below as the location where NetCOBOL is installed.
Change the folder name C:\NetCOBOL to the name of the folder where NetCOBOL is
installed.
1. Start the Project Manager, and project file SAMPLE12.PRJ is open.

Figure A.47. The Sample 12 project in the COBOL Project Manager

2. Select Build from the Project menu.


→ After build termination, check that SAMPLE12.EXE is created.

Setting ODBC information


The ODBC information file is necessary for the execution of this sample program.
When connecting it with the database by using the ODBC driver, necessary
information is included as for the ODBC information file.
The ODBC information file is made by using ODBC information Setup Tool
(SQLODBCS.EXE). Refer to the “Database (SQL)” chapter in the “NetCOBOL User’s
Guide for Windows” for more detailed information.

Setting Run-time Environment Information


1. Select “Run-time Environment Setup Tool” from the Tools menu.
→ The Run-time Environment Setup Tool is displayed.
Appendix A. Sample Programs 183

2. Select "Open" on the File menu and create an object initialization file
(COBOL85.CBR) in the folder that contains the executable program
(SAMPLE12.EXE).
3. Select a common tab and set data as shown below:
- Specify an ODBC information file name in environment variable information
@ODBC_Inf (ODBC information file specification).
- Specify a file to save the DISPLAY statement output result in environment
variable RESULT.

Figure A.48. The Run-time Environment Setup Tool with COBO85.CBR for Sample12.

4. Click the Apply button.


→ The set data is saved in the object initialization file.
5. Select "Exit" on the File menu to terminate the Run-time environment setup tool.

Debugging the Program


To run the program under the control of the Debugger, select Debug from the
Project menu. The Debugger starts and displays the Start Debugging dialog box.
Press the ENTER key.
184 Appendix A. Sample Programs

Executing the program


Select "Run" on the Project Manager "Project" menu.
→ The following is displayed in the COBOL console window.
SUCCESSFUL CONNECTION WITH DATABASE.
RECEIVE THE PRODUCT NUMBER WHOSE PRODUCT NAME IS ‘TELEVISION’
SET STOCKS OF THE FOLLOWING PRODUCTS DECREASING 10
TELEVISION -> + 0110
TELEVISION -> + 0111
TELEVISION -> + 0212
DELETE THE ROW WHICH HAS PRODUCT NAME IS ‘RADIO’, ‘SHAVER’ OR ‘DRIER’.
PROGRAM END
To the file assigned to environment variable RESULT, the contents of the STOCK
table before and after the operation are output in the format shown below.
Contents before processing
No.01:
Product number = +0110
Product name = TELEVISION
Stock quantity = +000000085
Warehouse number = +0002
:
No.19:
Product number = +0390
Product name = DRIVER
Stock quantity = +000000540
Warehouse number = +0003
There are 19 data in total.
Contents after processing
No.01:
Product number = +0110
Product name = TELEVISION
Stock quantity = +000000075
Warehouse number = +0002
:
No.15:
Product number = +0351
Product name = CASSETTE TAPE
Stock quantity = +000002500
Warehouse number = +0002
There are 15 data items in total.
Appendix A. Sample Programs 185

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


• SAMPLE13.EXE (Visual Basic executable file)
• SAMPLE13.VBP (Visual Basic project file)
• SAMPLE13.FRM (Visual Basic form module)
• SAMPLE13.COB (COBOL source program)
• SAMPLE13.PRJ (COBOL project file)
• SAMPLE13.CBI (COBOL compiler option file)
• SAMPLE13.LNI (Linkage option file)
• COBOL85.CBR (COBOL run-time initialization file)

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.
• JMPCINT2
• JMPCINT3

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.


186 Appendix A. Sample Programs

Figure A.51. The Sample 13 project in the COBOL Project Manager

2. The project file is selected, and "Linker options" is selected from "Project"-
"Options" menu.
→ The "Linker options" dialog is displayed.
Appendix A. Sample Programs 187

Figure A.52. Linker Options Dialog in the Sample 13 project

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:
188 Appendix A. Sample Programs

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.

Figure A.53. Setting the “invoke debugger” environment variable.

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.
Appendix A. Sample Programs 189

8. When you exit from the COBOL application control returns to the Visual Basic
code.
The section “H. Debugging with Visual Basic” in Chapter 2’s Quick Tour steps through
this example in some detail.
For more information on this topic, see Chapter 4, “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.

Figure A.54. The application window for Sample 13

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.
190 Appendix A. Sample Programs

Sample 14: Visual Basic calling COBOL –Simple ATM


Example
Sample 14 demonstrates Visual Basic calling COBOL by using a simple automatic
teller machine (ATM) bank account handling example.
Operating this program requires Visual Basic version 5.0 or later.

Overview
The sample program performs the following account functions:
• Opening a new account
• Depositing funds
• Withdrawing funds
The account data, comprising account number, PIN number, name and balance, is
saved in an indexed file.
The structure of the indexed file is:

Account number 9(5) *> (This is the primary record key.)


Password 9(4)
Name X(12)
Deposit 9(9)

When functions are requested from the “ATM terminal” (user screens), the record
data for the account in the indexed file is updated.

Files Included in Sample 14


• SAMPLE14.EXE (Visual Basic executable file)
• SAMPLE14.VBP (Visual Basic project file)
• SAMPLE14.BAS (Visual Basic standard module)
• SAMPLE14.FRM (Visual Basic form module)
The main window for the “ATM terminal” processing.
• SINKI.FRM (Visual Basic form module)
Dialog for opening a new account.
• SINKCHK.FRM (Visual Basic form module)
Displays the assigned account number for a new account.
• SELE.FRM (Visual Basic form module)
Account-handling dialog – shows account number, name and balance, and offers
the withdrawal and deposit functions.
• NYUKIN.FRM (Visual Basic form module)
Dialog for performing a deposit.
• SYUKIN.FRM (Visual Basic form module)
Dialog for performing a withdrawal.
Appendix A. Sample Programs 191

• ERROR_H.FRM (Visual Basic form module)


Error message box.
• K_KEN.COB (COBOL source program)
Retrieves accounts by account number.
• K_SIN.COB (COBOL source program)
Opens a new account.
• K_NYU.COB (COBOL source program)
Adds money deposited to an account.
• K_SYU.COB (COBOL source program)
Subtracts money withdrawn from an account.

Processing Overview
The Visual Basic application starts, and subroutine JMPCINT2 (which initializes the
COBOL run-time system) is called when the main form is loaded.
The Visual Basic forms manage the interface with the user – accepting input data,
transaction requests and displaying output data and messages. COBOL programs are
called to manage the account data in the indexed file.
When the Visual Basic application is closed, it calls subroutine JMPCINT3 (which
terminates the COBOL run-time).
Figure A.55 shows the structure of the application:

Visual Basic COBOL


System

sample14.exe
|
COBOL initialization processing --- JMPCINT2 Initialization procedure subroutine
|

Processing opening of a new account --- Account number retrieval processing

Account retrieval processing --- Account retrieval processing

Withdrawal processing --- Processing for debiting money

Deposit processing --- Processing for depositing money

|
|
COBOL termination processing --- JMPCINT3 Termination subroutine

Figure A.55. Sample 14 structure diagram


192 Appendix A. Sample Programs

COBOL Statements used in Sample 14


The MOVE, IF, PERFORM, COMPUTE, OPEN, READ, WRITE, REWRITE, CLOSE and
EXIT statements are used.

COBOL Run-time System Subroutines


The following routines are used to initialize and terminate the COBOL run-time
system.
• JMPCINT2
• JMPCINT3

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 "SAMPLE14.PRJ" is opened.

Figure A.56. The Sample 14 project in the COBOL Project Manager

2. The project file is selected, and "Linker options" is selected from "Project"-
"Options" menu.
Appendix A. Sample Programs 193

→ The "Linker options" dialog is displayed.

Figure A.57. Linker Options Dialog in the Sample 14 project

3. "/EXPORT:K_KEN /EXPORT:K_SIN /EXPORT:K_NYU /EXPORT:K_SYU" 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 K_KEN .DLL is created.

5. When you rebuild executable file of Visual Basic, SAMPLE14.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 SAMPLE14.EXE" is selected from the file menu.
194 Appendix A. Sample Programs

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 (SAMPLE14.EXE).
3. Select the Common tab and enter data as shown below:
- For the environment variable @CBR_ATTACH_TOOL, specify TEST.

Figure A.58. Setting the “invoke debugger” environment variable.

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 SAMPLE14.VBP project.
Appendix A. Sample Programs 195

7. Start debugging the Visual Basic code when you execute the call to SAMPLE14
(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 2’s Quick Tour steps through
this example in some detail.
For more information on this topic, see Chapter 4, “Working with Visual Basic and
COBOL” and the “NetCOBOL Debugging Guide”.

Executing the Program


To execute the program:
1. The first application dialog is displayed:
14

Figure A.59. The Enter Account number and PIN number dialog box

2. To work with the application click on the “New Account?” button.


The New Account Information dialog is displayed:

Figure A.60. The New Account Information window

3. Open a new account. Type in a name (such as Alan), an amount (such as


100000) and a PIN number (such as 1234). Click the OK button.
A dialog displaying the assigned account number is displayed:
196 Appendix A. Sample Programs

Figure A.61. The Account Number Assignment dialog box

4. Note the account number and the PIN number you used – you will need to enter
them in subsequent transactions.
5. Click on the OK button to return to the main Sample 14 dialog box.
6. You can now enter the account and PIN numbers and use the Withdrawal and
Deposit functions.
Appendix A. Sample Programs 197

Sample 15: Basic Object-Oriented Programming


This program illustrates basic object-oriented programming functions including
encapsulation, object generation and method invocation.

Overview
In the sample program, three employee objects are generated. After an object has
been generated using the "NEW" method, the "Data-Set" method is invoked to set
the data.
Although all the employee objects have the same form, they have different data
(employee numbers, names, departments and sections, and address information).
Address information also belongs to an object, containing postal codes and
addresses.
Upon input of an employee number on the screen, the appropriate "Data-Display"
method in the employee object is invoked, with employee information in the object is
displayed on the screen.
The employee object invokes the "Data-Get" method of the associated address
object to acquire the address information.
The employee object consists of three pieces of data and an object reference to an
address object. The structure of the object is transparent to the main program user.
The user must, however, understand the two methods of "Data-Set" and "Data-
Display."
The encapsulation of data completely masks the information in the object.

Files Included in Sample 15


• MAIN.COB (COBOL source program)
• MEMBER.COB (COBOL source program)
• ADDRESS.COB (COBOL source program)
• SAMPLE15.PRJ (Project file)
• SAMPLE15.CBI (Compiler option file)
• SAMPLE15.TXT (Program guide)

COBOL Functions used in Sample 15


• Object-oriented programming function
− Class definition (Encapsulation)
− Object generation
− Method invocation
• Project management
198 Appendix A. Sample Programs

Object-Oriented Syntax used in Sample 15


• INVOKE and SET statements
• REPOSITORY paragraph
• Class, object and method definitions

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 "SAMPLE15.PRJ" is opened.

Figure A.62. The Sample 15 project in the COBOL Project Manager

2. 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 MAIN.EXE is created.

Debugging the Program


You can execute this program under the control of the COBOL Debugger by selecting
Debug from the Project menu.
Appendix A. Sample Programs 199

Figure A.63. Sample 15 source in the COBOL Debugger

Executing the Program


To execute the linked program, select Execute from the Project menu.
Sample 15 requires no special execution environment information to be set.
The interface is very basic – simply enter an employee number 1, 2 or 3 to display
details for that employee. After the details are displayed enter Y or N to terminate or
continue.
200 Appendix A. Sample Programs

Sample 16: Collection Class (Class Library)


Sample 16 demonstrates using a collection class for creating a class library.
This sample can be used as an example of class library creation, and can be used to
create a class library in an actual program.
This sample covers only the basic operation. An easy-to-use class library can be
rolled out by modifying and changing this sample.

Overview
A collection class is the generic name of a class that handles a set of objects—it is
used to collectively handle and store many objects.
This sample covers the following classes.
• Collect (Collection)
• Dict (Dictionary)
• List (List)
Appendix A. Sample Programs 201

Class Layers
The following diagram shows the relationships between the class layers in Sample
16.

+--------------+
| FJBASE |
+--------------+
| Inherited
+---------------------------------------------------+
|Collect |
+---------------------------------------------------+
|CollectionSize-Get |
|FirstElement-Get |
|NextElement-Get |
|PreviousElement-Get |
+---------------------------------------------------+
| Inherited
+------------------+---------------+
| |
+-------------------------+ +---------------------------+
|List | |Dict |
+-------------------------| |---------------------------+
|Element-Get | |Element-Get |
|Element-Insert | |Element-PutAt |
|Element-PutAt | |FirstKey-Get |
|Element-PutLast | |LastKey-Get |
|ElementNo-Get | |Remove-All |
|LastElement-Get | |Remove-At |
|Remove-All | +---------------------------+
|Remove-At |
+-------------------------+

Figure A.63. Relationship between class layers in Sample 16.

NOTE: In addition to the above classes, Sample 15 also includes the classes
BinaryTree-Class, DictionaryNode-Class and ListNode-Class. These classes, which
are used inside the List and Dict classes, are transparent to the collection class user,
and are not explained here.

Collect Class
This is the highest collection class. All collection classes inherit this class.
Collect is an abstract class, and does not create any objects.
202 Appendix A. Sample Programs

Since this class inherits the FJBASE class, all the methods defined in the FJBASE class
can be used.

Definitions
CLASS-ID. Collect INHERITS FJBASE.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
CLASS FJBASE.
OBJECT.
PROCEDURE DIVISION.
METHOD-ID. CollectionSize-Get.
METHOD-ID. FirstElement-Get.
METHOD-ID. NextElement-Get.
METHOD-ID. PreviousElement-Get.
END OBJECT.
END CLASS Collect.

CollectionSize-Get method
This method investigates the number of elements in a set.

Parameter
None
Return value
PIC 9(8) BINARY
Returns the number of elements in a set.

FirstElement-Get method
This method returns the first element in a set.

Parameter
None
Return value
USAGE OBJECT REFERENCE
Returns the first element in a set. If no element exists, NULL is returned.

NextElement-Get method
This method returns the element following the one currently pointed to.

Parameter
None
Appendix A. Sample Programs 203

Return value
USAGE OBJECT REFERENCE
Returns the element following the one currently pointed to. If a following
element does not exist, NULL is returned.

PreviousElement-Get method
This method returns the element immediately preceding the one currently pointed to.

Parameter
None
Return value
USAGE OBJECT REFERENCE
Returns the element immediately preceding the one currently pointed to. If a
previous element does not exist, NULL is returned.

Dict Class
This class has the following features:
• Each element has a key.
• The key value is unique.
• A key can be used for retrieval.
• The key is used for ordering.
Since this class inherits from the Collect class, all the methods defined in Collect can
be used as well.

Definitions
CLASS-ID. Dict INHERITS Collect.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
CLASS Collect.
OBJECT.
PROCEDURE DIVISION.
METHOD-ID. Element-Get.
METHOD-ID. Element-PutAt.
METHOD-ID. FirstKey-Get.
METHOD-ID. LastKey-Get.
METHOD-ID. Remove-All.
METHOD-ID. Remove-At.
END OBJECT.
END CLASS Dict.
204 Appendix A. Sample Programs

Element-Get method
This method returns elements for a specified key.

Parameter
Key: PIC X(10)
Specifies a key value for an element to be returned.
Return value
USAGE OBJECT REFERENCE
Returns an element for a specified key if it is found, and returns NULL if it is not
found.

Element-PutAt method
This method adds an element for a specified key. If an element with the same key
already exists, it is replaced by the new element.

Parameters
Key: PIC X(10)
Specifies the key value of the element to be added or replaced.
Element: USAGE OBJECT REFERENCE
Specifies the element to be added or replaced.
Return value
None

FirstKey-Get method
This method determines the key value for the first element.

Parameter
None
Return value
PIC X(10)
Returns the key value for the first element. If the number of elements is 0, or if
the key for the first element is a blank, a blank is returned.

LastKey-Get method
This method determines the key value for the last element.

Parameter
None
Return value
PIC X(10)
Returns the key value for the last element. If the number of elements is 0, or if
the key for the last element is a blank, a blank is returned.
Appendix A. Sample Programs 205

Remove-All method
This method deletes all elements contained in a set.

Parameter
None
Return value
None

Remove-At method
This method deletes an element for a specified key.

Parameter
Key: PIC X(10)
Specifies the key value for the element to be deleted.
Return value
None

List Class
This class has the following features:
• Elements are arranged in a certain order.
• Duplicate elements can be contained.
Since this class inherits from the Collect class, all the methods defined in the Collect
class can be used as well.
206 Appendix A. Sample Programs

Definitions
CLASS-ID. List INHERITS Collect.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
CLASS Collect.
OBJECT.
PROCEDURE DIVISION.
METHOD-ID. Element-Get.
METHOD-ID. Element-Insert.
METHOD-ID. Element-PutAt.
METHOD-ID. Element-PutLast.
METHOD-ID. ElementNo-Get.
METHOD-ID. LastElement-Get.
METHOD-ID. Remove-All.
METHOD-ID. Remove-At.
END OBJECT.
END CLASS List.

Element-Get method
This method returns the element at a specified location (index).

Parameter
Index: PIC 9(8) BINARY
Specifies the location of the element to be returned by an integer starting at 1.
Return value
USAGE OBJECT REFERENCE
Returns the specified element. If no element exists at the specified location,
NULL is returned.

Element-Insert method
This method adds an element at the specified location (index).

Parameters
Index: PIC 9(8) BINARY
Specifies the location at which the element is to be added by an integer
beginning with 1.
If a value greater than the number of elements plus 1 is specified, no element is
added.
Element: USAGE OBJECT REFERENCE
Appendix A. Sample Programs 207

Specifies the element to be added.


Return value
PIC 9(8) BINARY
Returns the location at which the element was added by an integer beginning
with 1. If no element is added, 0 is returned.

Element-PutAt method
This method replaces the element at the specified location (index).

Parameters
Index: PIC 9(8) BINARY
Specifies the location of the element to be replaced by an integer beginning with
1. If a value greater than the number of elements is specified, no element is
replaced.
Element: USAGE OBJECT REFERENCE
Specifies the element to be replaced.
Return value
Returns the location of the replaced element using an integer beginning with 1.
If no element has been replaced, 0 is returned.

Element-PutLast method
This method adds an element after the last element.

Parameter
Element: Specifies the element to be added.
Return value
None

ElementNo-Get method
This method checks the location (index) of a specified element.

Parameter
Element: Specifies the element whose location is checked.
Return value
PIC 9(8) BINARY
Returns the location of the element using an integer beginning with 1.
If the specified element is not found, 0 is returned.
If duplicate elements exist, the first found location is returned.

LastElement-Get method
This method returns the last element.
208 Appendix A. Sample Programs

Parameter
None
Return value
USAGE OBJECT REFERENCE
Returns the last element. If the number of elements is 0, NULL is returned.

Remove-All method
This method deletes all the elements contained in a set.

Parameter
None
Return value
None

Remove-At method
This method deletes the element at the specified location (index).

Parameter
Index: PIC 9(8) BINARY
Specifies the location of the element to be deleted using an integer starting at 1.
If a value greater than the number of elements is specified, no element is
deleted.
Return value
Returns the location of the deleted element using an integer beginning with 1.
If no element has been deleted, 0 is returned.

Programs and Files in Sample 16


• COLLECT.COB (COBOL source program)
• DICT.COB (COBOL source program)
• LIST.COB (COBOL source program)
• BIN_TREE.COB (COBOL source program)
• D_NODE.COB (COBOL source program)
• L_NODE.COB (COBOL source program)
• SAMPLE16.PRJ (Project file)
• SAMPLE16.CBI (Compiler option file)
• SAMPLE16.TXT (Program guide)

COBOL Functions Used in Sample 16


• Object-oriented programming functions
− Class definition (Encapsulation)
Appendix A. Sample Programs 209

− Inheritance
− Object creation
− Method calling
• Project management

Object-Oriented Syntax used in Sample 16


• INVOKE and SET statements
• Object properties
• Method calling
• REPOSITORY paragraphs
• Class , object and method definitions

Building the Class Library


The Build function supported by the COBOL Project Manager is used to create the
class library.
1. Start the Project Manager, and project file SAMPLE16.PRJ is opened.

Figure A.64. The Sample 16 project in the COBOL Project Manager

2. Select Build from the Project menu.


When building terminates, the following files are created.
• COLLECT.DLL
• COLLECT.LIB
• COLLECT.REP
• DICT.REP
210 Appendix A. Sample Programs

• LIST.REP
NOTE: Some other files are also created, but they are not required when the class
library is used.

Using the Class Library


When the sample class library to be used is installed in a program, the following files
are required.

For Compiling or Linking


• COLLECT.LIB (Import library)
• COLLECT.REP (Repository library)
• DICT.REP (Repository file)
• LIST.REP (Repository file)
Install the above files to be used into a project that uses the class library. See
Sample 18, ”Advanced Object-oriented Programming.”

For Executing
• COLLECT.DLL (DLL file)
Appendix A. Sample Programs 211

Sample 17: Object-Oriented Cobol (Aggregation, Singleton,


and Iteration)

Sample 17 demonstrates the general object-oriented design concepts of


aggregation, singleton, and iteration.
The program use the Dict and List classes created in "Sample 16 Collection Class
(Class Library)".
It also requires Microsoft Excel (abbreviated to “Excel”).

Overview
The purpose of Sample 17 is to illustrate implementation of the object-oriented
concepts of aggregation, singleton and iteration (explained below). It uses a
conference room booking system to demonstrate these features. The user interface
is very basic as the focus is to demonstrate the OO concepts.
The conference room booking system provides functions for:
• Reserving a conference room.
• Canceling a conference booking.
• Reviewing conference room bookings.
• Listing, adding and deleting conference rooms.
• Updating conference room details.

The OO concepts are demonstrated as described below.

Aggregation
Aggregation is a relation of "whole to part". The life of the downstream class
instance in the aggregate relation is controlled completely by the life of the
upstream class instance. In this sample, the following classes are in the aggregation
relation:
• Reservation-status-Date class (RSVSTATE-DATE-CLASS) and Reservation-status-
Conference-Room class (RSVSTATE-CFNROOM-CLASS).
• Reservation-status-Conference-Room class (RSVSTATE-CFNROOM-CLASS) and
Reservation-status-Time-Frame class (RSVSTATE-TIME-CLASS).
• Conference-Room-Information-Control calls (CFRINFO-CNT-CLASS) and
Conference-Room-Information class (CFRINFO-CLASS).

Singleton
Singleton refers to a mechanism that guarantees that only one object instance can
exist for a class.
This is implemented in the SINGLE-TON class in this sample. The SINGLE-TON class
is inherited by the following classes so that they can only create a single object
instance.
212 Appendix A. Sample Programs

• Reservation control class


- RSVCNT-CLASS
• Conference room information control class
- CFRINFO-CNT-CLASS.
• Error processing class
- ERROR-CLASS

Iteration
Iteration provides a means of accessing elements sequentially without having to be
aware of the internal structure of the aggregation object. In this sample, iteration is
implemented by the LIST-ITERATER class.
Two or more LIST-ITERATER class objects can be created for a single list object.
In this sample, iteration is used in the following functions:
• Display of reservation information
Method: RSV-SITUATION-LIST-DISP,
Class: RSVCNT-CLASS
• Deletion of reservation state objects (date, conference room and time frame)
Method: RSV-SITUATION-OBJ-DEL
Class: RSVCNT-CLASS
• Retrieval of conference room information and reservation information
Method: RSV-DATA-RETRIEVAL
Class: RSVINFO-CLASS
• Storing the information in an Excel file.
Entry: PRESERVATION-DATA
Program: EXCEL-EDIT
Appendix A. Sample Programs 213

Design of Sample 17
Figure A.63 shows the relationships between the classes used in Sample 17.

Main
Exception Object
Program

Error Class
Singleton Class

Resevation Dictionary Reservation status -


Control Class Class Dict Date Class

Method Method Class List Class


Control Class List

Conference Conference Reservation status -


room room Conference room
information List Class information Class
control Class List Class

List Iterate Reservation status -


EXCEL
Class Time form Class
Operation

List Class Reservation


List information Class

: Shows an inheritance relationship : Shows a one-to-one relationship

: Shows a one-to-many relationship

Figure A.63. Design diagram for Sample 17

Programs and Files in Sample 17


• MAIN.COB (COBOL source program)
• EXCELEDT.COB (COBOL source program)
• RSVCTRL.COB (COBOL source program)
• ROOMCTRL.COB (COBOL source program)
• DATESTA.COB (COBOL source program)
• ROOMSTA.COB (COBOL source program)
• TIMESTA.COB (COBOL source program)
214 Appendix A. Sample Programs

• RESERVE.COB (COBOL source program)


• SPECCTRL.COB (COBOL source program)
• SPEC.COB (COBOL source program)
• SINGLETN.COB (COBOL source program)
• LISTITER.COB (COBOL source program)
• ERRORPUT.COB (COBOL source program)
• RSVINFO.CBL (library file)
• ROOMINFO.CBL (library file)
• R_CONST.CBL (library file)
• SPECINFO.CBL (library file)
• ROOMLIST.XLS (Excel file)
• RSVLIST.XLS (Excel file)
• DICT.REP (Repository file)
• LIST.REP (Repository file)
• SAMPLE17.PRJ (project file)
• COLLECT.DLL (DLL file)
• COLLECT.LIB (import library)

COBOL Functions used in Sample 17


• Object oriented programming functions
• Definition of class (encapsulation)
• Inheritance
• Multiple inheritance
• Creation of objects
• Method calls
• Exception handling
• OLE connection
• Project manager function

Object-Oriented Syntax used in Sample 17


• INVOKE and SET statements
• Object properties
• In-line method calls
• REPOSITORY paragraphs
• Class , object and method definitions
• Type definitions
Appendix A. Sample Programs 215

Before Executing the Application


Check the definitions of the Excel file names in the file:
R_CONST.CBL
used by most of the programs. (To edit this file expand the project tree down to a
list of Copy Library Files for one of the programs, e.g. MAIN, and double click on the
R_CONST.CBL file name.)
The names are defined at the head of the file as the constants: CNFROOM-FILE-
NAME and RSV-FILE-NAME.
You may need to change the settings of these constants if you did not install to the
default folder structure.
Sample17 frequently writes to the Excel files so if you want to preserve the original
or other version of these files, please take a backup before running Sample17.
Attention
The code is written to discard out-of-date data. When objects are restored from the
Excel file, any objects with dates earlier than the current date are discarded.
Likewise, when objects are preserved, any objects that are out-of-date are not
written to the Excel file.

Building the Application


Project Manager's build function is used to make the executable program.
1. Start the Project Manager.
2. Open project file SAMPLE17.PRJ.
3. Select Build from the Project menu.

Executing the Application


To execute the program select Execute from the Project menu. (No run-time
environment variables need to be set.)
When the program starts, it reloads the information from Excel, then displays a list
of options:
216 Appendix A. Sample Programs

Figure A.64. Starting screen for Sample 17.

Guidelines for Operating the Application


The application has a simple, menu-driven interface, controlled by the MAIN.COB
program.
The main menu lets you select functions by entering a number from 1 to 3 or X to
exit. The functions are:
1. Reserve and refer
this provides the ability to reserve conference rooms, see bookings of
conference rooms (on a given date), and cancel bookings.

2. Retrieval
The retrieval function returns all conference room bookings for a given name.
3. Conference room control
The conference room control function lets you update, add and delete
conference room details.
Once you have entered the primary function you are prompted to enter data an item
at a time. Sometimes the interface introduces a set of information to be entered,
such as "Enter the conference room name, and time at which it should be booked"
and the prompts for the first piece of information (the conference room name). Just
enter the first piece of information and you will be prompted for the other pieces in
the set.
You'll be asked for the following information for the different functions:
Appendix A. Sample Programs 217

Reserve and refer


An 8-digit date (equal to or later than today's date)
Conference room name (4 characters)
Time (AM or PM)
If there is no booking for that room, date and time, you are prompted for:
Name (10 characters)
Extension (9 digits)
Department (10 characters)
and are returned a reservation number (required if you later want to cancel the
reservation).
If there is a booking for the room, the booking details are displayed and you have
the option to cancel the room. If you choose to cancel the booking you are prompted
for the 4 digit reservation number.

Retrieval
You are prompted to enter:
Subscriber name (10 digits) – the name entered when booking the room.
Information corresponding to the name is displayed.

NOTE: The program is configured so that it only displays five reservations. To


increase this change the following item in the R_CONST.CBL file.
RSV-MAX IS 5

Conference room control


You are prompted to enter:
Conference room name (4 characters)

If the conference room exists, details for that conference room are listed. If it
doesn't exist you are prompted to enter details to register the conference room. The
conference room details are:
Capacity (2 digits)
Extension (9 digits)
Type (N for general, T for video)
When changes are completed the appropriate updates are made to the Excel file to
reflect the changes.

End
The program saves reservation information objects in the Excel file and terminates.
218 Appendix A. Sample Programs

Sample 18: Advanced Object-Oriented Programming


Sample 18 uses all object-oriented specific functions including encapsulation,
inheritance and polymorphism.
This program, which deals with two or more employee objects, uses the Dict class
created in Sample 16, “Collection Class (Class Library).”

Overview
The application shows how an OO system can be set up to manage employee
information. It implements, in a basic manner, the functions of registering, deleting,
and modifying employee details, calculating salaries and printing a directory of
employee information.
The employees are classified into staff members and managers. Different data is
maintained and the pay calculation method differs between staff members and
managers. The elements of the application are:
• An employee class that defines the attributes common to all employees
(AllMember-class)
• A staff member class that defines the attributes particular to staff employees
(Member-Class)
• A manager class that defines the attributes particular to managers (Manager-
Class)
The staff member and manager classes inherit the employee class.
The addresses portion of the employee information is managed by an independent
class (Address-Class).
Address objects are referenced from employee objects (and their subclasses).
The employee object defines the employee data to be managed by the supervisor.
Sample 18 use the dictionary class (Dict) created in Sample 16, “Collection Class
(Class Library)”. It registers an employee object with the dictionary using the
employee number as the key. The dictionary class allows easy repeat processing of
two or more employee objects, and easy retrieval of an employee object.

Programs and Files in Sample 18


• MAIN.COB (COBOL source program)
• ALLMEM.COB (COBOL source program)
• MEMBER.COB (COBOL source program)
• MANAGER.COB (COBOL source program)
• ADDRESS.COB (COBOL source program)
• BONU_MAN.COB (COBOL source program)
• BONU_MEM.COB (COBOL source program)
• SALA_MAN.COB (COBOL source program)
Appendix A. Sample Programs 219

• SALA_MEM.COB (COBOL source program)


• DICT.REP (Repository file)
• SAMPLE18.PRJ (Project file)
• SAMPLE18.CBI (Compiler option file)
• COBOL85.CBR (Environment variable initialization file)
• COLLECT.DLL (DLL file)
• COLLECT.LIB (Import library)

COBOL Functions used in Sample 18


• Object-oriented programming functions
− Class definition (Encapsulation)
− Inheritance
− Object generation
− Method calling
− Polymorphism
• Screen operation function
• Project management

Object-Oriented Syntax used in Sample 18


• INVOKE and SET statements
• Object properties
• In-line method calls
• REPOSITORY paragraphs
• Class, object and method definitions

Building the Application


Project Manager's build function is used to make the executable program.
1. Start the Project Manager.
2. Open project file SAMPLE18.PRJ.
3. Select Build from the Project menu.

Debugging the Application


The project file is supplied with the debugging option set. You can execute the
program under the control of the COBOL Debugger by:
1. Selecting Debug from the Project menu.
2. Clicking on the OK button in the Start Debugging dialog box.
The COBOL Debugger loads the code as shown in Figure A.65.
220 Appendix A. Sample Programs

Figure A.65. Sample 18 in the COBOL Debugger

Executing the Application


To run the application, select Execute from the Project menu.
The main application window is displayed:
Appendix A. Sample Programs 221

Figure A.66. Main window for Sample 18

To operate the program, enter the number of the desired process into “Select
number” and press ENTER.
Note that this sample does not implement object persistence so data is lost when the
application terminates. See Sample 19 for the same application with persistence
added.

Registering Employee Information


Enter the employee information including:
• Employee number (four-digit numeric character string);
• Name (Character string of eight or fewer characters);
• Address consisting of a (Japanese style) postal code (7 digits) and a character
string of 20 or fewer characters
• Date the employee joined the company (in the format of YY.MM.DD – for this
program there is no date processing so the format is not significant)
• Status code (1 for a manager, and 2 for a staff member); and
• Basic pay (numeric character string of eight or fewer digits)
For a manager, type in the special allowances (numeric character string of six or
fewer digits). For a general staff member, type in the overtime (numeric character
string of five or fewer digits).
Press the F3 key to register the data.
To terminate processing of employee registration information, press the F2 key.
222 Appendix A. Sample Programs

Deleting Employee Information


To delete employee information, type in the employee number (four-digit numeric
character string), and press the F3 key.
The employee data is displayed and can be corrected.

Calculating Employee Pay


When you select the “Salary calculation” function, the pay of all employees is
calculated.
Entering an employee number and pressing the F3 key displays the pay information
for that employee.
To terminate processing of the salary calculation function, press the F2 key.

Address list
An employee address list can be printed. Select Manager (1) or General employee
(2), and then press the F3 key.

End
Terminates processing.
Appendix A. Sample Programs 223

Sample 19: Object Persistence (Indexed File)


Sample 19 demonstrates object persistence based on the program created in Sample
18, ”Advanced Object-Oriented Programming.” In Sample 18, all objects are created
in memory and thus upon termination of the program, all objects are lost.
In an actual system, however, data must be preserved when the program
terminates. For an object-oriented system, in which the data is held in objects, the
objects must continue to exist whether or not the application is executing. These
objects are known as persistent objects.
In general, a persistent object can be implemented by relating the object to
databases or files. In this example, persistent objects are rolled out by relating each
object to an index file record.
For further details on object perpetuation, refer to the section “Making Objects
Persistent” in the “NetCOBOL User’s Guide for Windows”.

Overview
The function of this sample is identical to that of Sample 18 except that the
employee and address objects are stored in an index file, and persistence functions
are added.
Persistence is implemented by adding the following methods to the employee and
address objects.
• Store-Method (Object method)
Stores an object in the file.
• RetAt-Method (Factory method)
Loads an object from the file using the employee number as the key.
• Remove-Method (Factory method)
Deletes an object from the file using the employee number as the key.
Actually, the object saving and retrieving is implemented by using the employee
master class and the address master class. From the object user (the MAIN program
in this example), however, the process is viewed as if the employee object assumed
all the tasks.

Programs and Files in Sample 19


• MAIN.COB (COBOL source program)
• ALLMEM.COB (COBOL source program)
• MEMBER.COB (COBOL source program)
• MANAGER.COB (COBOL source program)
• ADDRESS.COB (COBOL source program)
• SET.COB (COBOL source program)
• STORE.COB (COBOL source program)
224 Appendix A. Sample Programs

• ALLMEM_M.COB (COBOL source program)


• ALLMEMMF (Data file)
• BONU_MAN.COB (COBOL source program)
• BONU_MEM.COB (COBOL source program)
• MEM_SET.COB (COBOL source program)
• MEM_STOR.COB (COBOL source program)
• MAN_SET.COB (COBOL source program)
• MAN_STOR.COB (COBOL source program)
• ADDR_M.COB (COBOL source program)
• ADDR_MF (Data file)
• SALA_MAN.COB (COBOL source program)
• SALA_MEM.COB (COBOL source program)
• SAMPLE19.PRJ (Project file)
• SAMPLE19.CBI (Compiler option file)
• COBOL85.CBR (Environment variable initialization file)

COBOL Functions used in Sample 19


• Object-oriented programming functions
• Indexed file functions
• Project management

Object-Oriented Syntax used in Sample 19


• INVOKE and SET statements
• Object properties
• In-line method calls
• REPOSITORY paragraphs
• Class, object, factory and method definitions

Building the Application


Project Manager's build function is used to make the executable program.
1. Start the Project Manager.
2. Open project file SAMPLE19.PRJ.
3. Select Build from the Project menu.

Debugging the Application


The project file is supplied with the debugging option set. You can execute the
program under the control of the COBOL Debugger by:
Appendix A. Sample Programs 225

1. Selecting Debug from the Project menu.


2. Clicking on the OK button in the Start Debugging dialog box.
The COBOL Debugger loads the code as shown in the following figure.

Figure A.67. Sample 19 in the COBOL Debugger

Executing the Application


To run the application, select Execute from the Project menu.
The main application window is displayed.
For details on running the program, see Sample 18, ”Advanced Object-oriented
Programming”. The main difference is that data you enter is preserved from one
execution of the application to the next to the next.
226 Appendix A. Sample Programs

Sample 20: Object Persistence (Database)


Sample 20 demonstrates a method of making a permanent object and is based on
the program created in "Sample 18 Object-Persistence". In sample 18, all objects
were created in memory. Therefore, the objects disappear at program termination.
However, some data has to remain in the system after the program ends. An object
that retains data after the program ends is called a "Permanent Object".
In the previous program, such data was stored in a file or a database. To create a
permanent object one must associate the permanent object with data in file or a
database. In this example, the permanent object has been achieved by associating
each object with a line in a data base table.
Refer to the "NetCOBOL User's Guide for Windows" for details on Object Persistence.
The database exists on the server, and is accessed from the client side.
The database is accessed via the ODBC driver. For information on setting up and
using ODBC databases, please refer to the Chapter 19, "Database (SQL) " of the
"NetCOBOL User's Guide for Windows".
The following products are necessary to execute this program.
• Client
− ODBC driver manager
− ODBC driver
− Products needed for the ODBC driver
• On the server
− Database
− Products needed for accessing the database via ODBC

Overview
The management of employee information (addition, deletion, and modification), the
salary calculations, and address are printed as was done in sample 18.
In addition to the functions above, in this example the employee object and the
address object stored in the database, and a permanent object function is added.
These functions have been created in the employee and the address objects by
adding the following methods.
• Store-Method (object method)
: The object is stored in a database.
• RetAt-Method (object method)
: The object is read from the database using the employee number as the key.
• RemoveAt-Method (object method)
: The object is removed from the database using the employee number as the
key.
• Update-Method (object method)
Appendix A. Sample Programs 227

: The object stored in the database is updated.

Available Programs
• MAIN.COB (COBOL source program)
• ALLMEM.COB (COBOL source program)
• MEMBER.COB (COBOL source program)
• MANAGER.COB (COBOL source program)
• ADDRESS.COB (COBOL source program)
• SET.COB (COBOL source program)
• ALLMEM_M.COB (COBOL source program)
• BONU_MAN.COB (COBOL source program)
• BONU_MEM.COB (COBOL source program)
• MEM_SET.COB (COBOL source program)
• MEM_STOR.COB (COBOL source program)
• MAN_SET.COB (COBOL source program)
• MAN_STOR.COB (COBOL source program)
• ADDR_M.COB (COBOL source program)
• SALA_MAN.COB (COBOL source program)
• SALA_MEM.COB (COBOL source program)
• SAMPLE20.PRJ (project file)
• SAMPLE20.CBI (compilation option file)
• SAMPLE20.MDB (Microsoft Access Database file)
• COBOL85.CBR (initialization file for execution)
• SAMPLE20.KBD (key definition file)
• SAMPLE20.INF (ODBC information file)

Applicable COBOL Functions


• Object oriented programming function
• Remote database access (ODBC) function
• Project manager function

Available Object-oriented Statements/Paragraphs/Definitions


INVOKE and SET statements
Object property
In-line invocation of a method
228 Appendix A. Sample Programs

Repository paragraph
Class definition, object definition, factory definition, and method definition

Prerequisite to Executing the Program


ODBC is a defined interface that attempts to provide a highly generic API into any
database system that provides compliant drivers. Just about every database system
available today provides ODBC drivers for a variety of platforms.
In order to proceed with this exercise, you must have already installed 32-bit ODBC
support on your Windows 98, Me, 2000, XP, Windows Server 2003 or Windows NT
system, along with the Microsoft Access Driver.

Verifying Whether ODBC is Installed


Please refer to "Sample11".

Defining an ODBC Source for Your Application


The first development task that needs to be accomplished is to define an ODBC
Source for the application.
An ODBC Source can be thought of as simply specifying to the ODBC manager a
symbolic name that you wish to use in your application to describe an ODBC
connection to a specific ODBC database driver and database.
The following figures may or may not match what you see on your particular
operating system, but you should be able to perform the following tasks by using the
instructions provided.
Bring up the Administrative Tools and double click on the Data Sources (ODBC) icon.
The main ODBC Data Source Administrator window is displayed.
Appendix A. Sample Programs 229

Figure A.68. The main ODBC Data Source Administrator window

You will add a new User DSN. Select the User DSN tab, and click on the Add button.
ODBC Data Source Administrator displays the “Create New Data Source” dialog box.

Figure A.69. The Create New Data Source dialog box


230 Appendix A. Sample Programs

Select the Microsoft Access Driver (*.mdb) and click on the Finish button.
The ODBC Microsoft Access Setup dialog box is displayed.

Figure A.70. The ODBC Microsoft Access Setup dialog box

Enter Sample20 in the Data Source Name field and Sample20 Database in the
Description field as shown above.
You also need to select a file (database) to bind this driver to. Click on the Select
button in the ODBC Microsoft Access Setup dialog box. The Select Database dialog
box is displayed. Navigate through the directories and find the "Sample20.mdb"
database file:

Figure A.71. The Select Database dialog box

Click on the OK button. This will take you back to the ODBC Microsoft Access Setup
dialog box.
Appendix A. Sample Programs 231

Click on the OK button. This will take you back to the main ODBC Source
Administrator window.
Note that "Sample20" has been added to the list in User DSN. Although you should
not set any additional options for this exercise, you may wish to highlight “Sample20”
and click on the Configure button.

This will give you an idea of some of the additional options that are available when
developing these types of applications. Once you are back to the main ODBC Source
Administrator window, click on the OK button. This will exit the ODBC configuration
facility. You have now defined the new ODBC Source (Sample20) you are going to
use with COBOL.

Building/Rebuilding the Program


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

1. The project manager is started.

Figure A.72. The Sample 20 project in the COBOL Project Manager

2. Project file "SAMPLE20.PRJ" is opened.

"Build" is selected from the "Project" menu.


Setup the Execution Environment Information
232 Appendix A. Sample Programs

The initialization file for execution is updated with the Run-time Environment Setup
Tool. The content is shown below.
1. The Run-time Environment Setup Tool is started.
2. The initialization file for execution is opened. (COBOL85.CBR)
3. A common tab is selected.
4. The ODBC information file name is specified for @ODBC_Inf. (SAMPLE20.INF)
5. The key definition file name is specified for @CBR_SCR_KEYDEFFILE.
(SAMPLE20.KBD)

Figure A.73. The Run-time Environment Setup Tool with COBOL85.CBR for Sample
20

6. Click "Apply" button, and the content is saved in the execution initialization file.
7. Click "Exit" button in file menu, and the Run-time Environment Setup Tool ends.
Appendix A. Sample Programs 233

Executing the Program


To execute the program, "Execute" is selected from the project menu.
Execution Procedure
Please refer to "Sample 18 Object Persistence" for execution procedures.
However, please note the following points.
• When the name and the address are input, National characters cannot be used.
Please use upper and lower case letters or numeric characters.
• An error report is generated when an error occurs when accessing the database
(such as connecting or getting a record).
234 Appendix A. Sample Programs

Sample 21: Multi-Thread Programming


Sample 21 demonstrates how COBOL can support multithreading programming.
In sample 21, a resource (file data) is shared between threads, and the synchronous
control between threads is shown by using the multi-thread programming functions
of NetCOBOL.
Please refer to Chapter 22, "Multithread Programs” of the “NetCOBOL User's Guide
for Windows” for details of the multi-thread programming functions of NetCOBOL.
Sample 21 is a Web application. Web applications typically can benefit from
multithreading applications.
Please refer to the "NetCOBOL Web Guide" and the "NetCOBOL ISAPI Subroutines
User's Guide" for details of the functions available for programming the Web.
To execute this sample, the following products are needed on the client side and the
server side.
Client side
• World Wide Web browser
− Microsoft(R) Internet Explorer 4.0 or more
− Netscape Navigator(TM) 4.0 or more
Server side
• One of the following products:
− Microsoft® Windows NT® Server Network operating system Version 4.0
− Microsoft® Windows NT® Server Network operating system, Enterprise
Edition Version 4.0
− Microsoft® Windows® 2000 Server operating system
− Microsoft® Windows® 2000 Advanced Server operating system

− Microsoft® Windows Server™2003, Standard Edition

− Microsoft® Windows Server™2003, Enterprise Edition

• Microsoft® Internet Information Server 4.0 or later

For Windows NT® 4.0, Microsoft® Windows NT® OptionPack 4.0 is necessary.

Overview
The sample program consists of the following three parts.
• Initialization processing
The resource (file data) between threads is acquired, and is initialized.
• Authorization processing
The authorization process is achieved by accessing the resource (file data) between
threads.
• Termination processing
Appendix A. Sample Programs 235

The resource (file data) between threads is closed.


This sample demonstrates how to share resource data with Web functions, and how
to synchronize the threads.

Available Programs
• Project files
− ISAPIAPL.PRJ
− MTHAPL.CBI
• Option files
− ISAPIAPL.CBI
− MTHAPL.CBI
• COBOL source files
− AUTH.COB
− ISAINIT.COB
− ISATERM.COB
− MTHEND.COB
− MTHSTR.COB
− MTHUSRINF.COB
− SHUTDOWN.COB
− STARTUP.COB
− STUPINIT.COB
• Library text
− User-Info.CBL
− User-Lock.CBL
• Module definition files
− AUTH.DEF
− SHUTDOWN.DEF
− STARTUP.DEF
• Data files
− USERINFO
• Run-time Initialization file
− COBOL85.CBR
• HTML files
− ADMIN.HTML
− AUTH.HTML
− AUTHFAIL.HTML
236 Appendix A. Sample Programs

− AUTHSUCESS.HTML
− NOTOPENED.HTML
− OPENED.HTML
− SHUTDOWN.HTML
− STARTUP.HTML
− SYSERROR.HTML
− SYSTEMERROR.HTML

Process Flow
1. Business start and end

Figure A.74. The process flow of Business start and end

2. Authorization service

Figure A.75. The process flow of Authorization service


Appendix A. Sample Programs 237

Applicable COBOL Functions


• Index file (reference)
• External data
• External file
• Data lock subroutine
• COBOL ISAPI subroutine

Applicable COBOL Statements


The CALL, CLOSE, EXIT, GO TO, IF, MOVE, OPEN, PERFORM, READ, and SET
statements are used.

Building 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.
2. The project file "MTHAPL.PRJ" is opened.

Figure A.76. The MTHAPL project in the COBOL Project Manager


238 Appendix A. Sample Programs

3. The project file is selected, and "Compiler options" is selected from "Project"-
"Options" menu.
→ The "Compiler options" dialog is displayed.

Figure A.77. Compiler Options for the MTHAPL project

4. Compiler option THREAD(MULTI), SHREXT and ALPHAL(WORD) are specified.


After confirming the information, click the OK button.
→You are now returned to the Project Manager window.
5. "Build" is selected from Project Manager's "Project" menu.
→(Prior to executing the application, please confirm that all the DLL’s in the
application have been built correctly.)
6. The project file "ISAPIAPL.PRJ" is opened
Appendix A. Sample Programs 239

Figure A.78. The ISAPIAPL project in the COBOL Project Manager

7. The compiler option dialog is displayed as in step 3 above, showing that the
compiler options THREAD(MULTI), SHREXT, and ALPHAL(WORD) are specified.
Modify the folder name of the library file specified in the compiler option LIB.
Click the OK button.
→You are now returned to the Project Manager window.
240 Appendix A. Sample Programs

Figure A.79. Compiler Options for the ISAPIAPL project

8. "Build" is selected from Project Manager's "Project" menu.

Executing the Program


It is assumed that the domain-name and virtual directory name are registered in IIS
(Internet Information Server) as "user" and "sample21" respectively.
This example presumes that Microsoft Internet Explorer is being used.
1. The authentication service begins.
The following information is set in URL.

Address │ http://user/sample21/admin.html

The administrator menu screen is displayed. Click on the “Set Starting Business”
hyperlink.
Appendix A. Sample Programs 241

Figure A.80. The administrator menu screen

When clicked, the authentication service begins. Please do so before starting the
authentication service.
2. The authentication service is started.
The following information is set in the URL and the "Execute" key is pushed.

Address │ http://user/sample21/Auth.html

The authentication service screen is displayed. After the screen is displayed, input a
User ID and password, then click the "OK" button.
Valid User ID’s are USER0001 to USER0030. The password is the same as the User
ID.
242 Appendix A. Sample Programs

Figure A.81. The member authentication screen

When the OK button is clicked, the authentication success screen is displayed.

Figure A.82. The authentication success screen


Appendix A. Sample Programs 243

If the User ID and/or password are invalid, an authentication failure screen is


displayed.

Figure A.83. The authentication failure screen

3. The authentication service is ended.


Input following information in the URL and press the "Execute" key.

Address │ http://user/sample21/admin.html

Because the administrator menu screen is displayed, "Set ending business" is


clicked.
244 Appendix A. Sample Programs

Figure A.84. The administrator menu screen


Appendix A. Sample Programs 245

Sample 22: Multi-Thread Programming(Advanced usage)


In sample 22, the program in sample 21 is enhanced to demonstrate an online store.
The sample uses multi-thread programming functions as well as functions for
programming the Web.
Please refer to "NetCOBOL Web Guide" and "NetCOBOL ISAPI Subroutines User's
Guide" for details of the Web functions.
Please refer to Chapter 22 "Multithread Programs" of the "NetCOBOL User's Guide for
Windows" for details of the multithreading programming functions.
This example also outputs an event log using the function “COB_REPORT_EVENT”.
This function is described in Appendix H of the “NetCOBOL User's Guide for
Windows”.
To use this sample, the following products are needed on the client side and the
server side.
Client side
− Microsoft(R) Internet Explorer 4.0 or more
− Netscape Navigator(TM) 4.0 or more
Server side
• One of the following products:
− Microsoft® Windows NT® Server Network operating system Version 4.0
− Microsoft® Windows NT® Server Network operating system, Enterprise
Edition Version 4.0
− Microsoft® Windows® 2000 Server operating system
− Microsoft® Windows® 2000 Advanced Server operating system

− Microsoft® Windows Server™2003, Standard Edition

− Microsoft® Windows Server™2003, Enterprise Edition

• Microsoft® Internet Information Server 4.0 or later

For Windows NT® 4.0, Microsoft® Windows NT® OptionPack 4.0 is necessary.

Overview
The sample program consists of the following five parts.
• Begin processing
The resource (file data) between threads is acquired and initialized.
• Authorization processing
The authorization processing is achieved by referring to the resource (file data)
between threads.
• Order confirmation processing
The order confirmation processing is performed by referring to the resource (file
data) between threads.
246 Appendix A. Sample Programs

• Order issue processing


The order issue processing is performed by referring to the resource (file data)
between threads. Shared file update processing is done.
• Termination
The resource (file data) between threads is closed.

Available Programs
• Project files
− ISAPIAPL.PRJ
− OLSAPL.PRJ
• Option files
− ISAPIAPL.CBI
− OLSAPL.CBI
• COBOL source files
− AUTH.COB
− CONFIRM.COB
− ENTRY.COB
− ISAINIT.COB
− ISATERM.COB
− OLSEND.COB
− OLSPRDGT.COB
− OLSSTCGT.COB
− OLSSTCODR.COB
− OLSSTR.COB
− OLSUSRINF.COB
− SHUTDOWN.COB
− STARTUP.COB
− STUPINIT.COB
• Library text
− Order-Info.CBL
− Product-Info.CBL
− Stock-Info.CBL
− User-Info.CBL
− User-Lock.CBL
− User-Log.CBL
• Module definition files
− AUTH.DEF
Appendix A. Sample Programs 247

− CONFIRM.DEF
− ENTRY.DEF
− SHUTDOWN.DEF
− STARTUP.DEF
• Data files
− PRODUCTINFO
− STOCKINFO
− USERINFO
• Run-time Initialization file
− COBOL85.CBR
• HTML files
− ADMIN.HTML
− AUTH.HTML
− AUTHFAIL.HTML
− CATALOG.HTML
− CONFIRMDETAILPARTS.HTML
− CONFIRMHEAD.HTML
− CONFIRMTAIL.HTML
− ILLIGALACCESS.HTML
− ILLIGALSYSTEM.HTML
− NOTOPENED.HTML
− OPENED.HTML
− ORDERDETAILPARTS.HTML
− ORDERRESULTHEAD.HTML
− ORDERRESULTTAIL.HTML
− SHOPPINGMENU.HTML
− SHORTAGESTOCK.HTML
− SHUTDOWN.HTML
− STARTUP.HTML
− STARTMENU.HTML
− SYSERROR.HTML
− SYSTEMERROR.HTML
− UNDERCONSTRUCTION.HTML
• GIF files
− CATALOGTITLE.GIF
− FJLOGO.GIF
248 Appendix A. Sample Programs

− TITLE.GIF
− LifeBookE.GIF
• JPEG files
− LifeBookB.JPG
− LifeBookC.JPG
− LifeBookL.JPG

Process Flow
1. Business start and end

Figure A.85. The process flow for Business start and end
Appendix A. Sample Programs 249

2. Online store

Figure A.86. The process flow for Online store

Applicable COBOL Functions


• Index file (creation, reference, update, and rewrite)
• External data
• External file
• Data lock subroutine
• External file event log (output of user definition information)
• COBOL ISAPI subroutine

Applicable COBOL Statements


The CALL, CLOSE, EXIT, GO TO, IF, MOVE, OPEN, PERFORM, READ REWRITE, SET,
START, and WRITE statements are used.
250 Appendix A. Sample Programs

Building the Program


The Project Manager's Build function is used to create the executable program.
In the following screens, the user has installed the COBOL system in C:\NetCOBOL.
Your install location may be different.
1. The Project Manager is started.
2. The project file "OLSAPL.PRJ" is opened.

Figure A.87. The OLSAPL project in the COBOL Project Manager

3. The project file is selected, and "Compiler options" is selected from the "Project"-
"Options" menu.
→ The "Compiler options" dialog is displayed.
Appendix A. Sample Programs 251

Figure A.88. Compiler Options for the OLSAPL project

4. Compiler option THREAD(MULTI), SHREXT and ALPHAL(WORD) are specified.


Click the OK button.
5. "Build" is selected from Project Manager's "Project" menu.
→Please confirm each DLL (dynamic link library) registered in the project is created.
6. Next, The project file "ISAPIAPL.PRJ" is opened
252 Appendix A. Sample Programs

Figure A.89. The ISAPIAPL project in the COBOL Project Manager

7. The compiler option dialog is displayed as in step 3 above. The compiler option
THREAD(MULTI), SHREXT, and ALPHAL(WORD) are specified. Modify the folder
name of the library file specified with the compiler option LIB. After
confirmation, the OK button is clicked.

Figure A.90. Compiler Options for the ISAPIAPL project


Appendix A. Sample Programs 253

8. "Build" is selected from Project Manager's "Project" menu.


→Please confirm each DLL (dynamic link library) registered in the project is built.

Executing the Program


It is assumed that the domain-name and virtual directory name are registered in IIS
(Internet Information Server) as "user" and "sample22" respectively.
In this example uses Microsoft® Internet Explorer as the WWW browser.
1. Online store is started.
The following information is set in theURL.
http://user/sample22/admin.html

Then click "Set starting business".

Figure A.91. The administrator menu screen

When clicked, the online store is started. Please do the "Set starting business"
before starting online shopping.
2. Online store is started.
The following information is set in URL and the "Execute" key is pushed.
http://user/sample22/startmenu.html

Because the screen of an online store is displayed, the catalog shopping is clicked.
254 Appendix A. Sample Programs

Figure A.92. The online store screen

When Catalog Shopping is clicked, the member authentication screen is displayed.


After the screen is displayed, the User ID and password are input. Click the "OK"
button.
Valid User ID’s are USER0001 to USER0030. The password is the same as the user
ID.
Appendix A. Sample Programs 255

Figure A.93. The member authentication screen

When the "OK" button is clicked, the catalog screen is displayed. The catalog screen
shows you the various PC’s and their quantities available for ordering. To place an
order, select one or more products and enter an order quantity, then press the
“Order” button.

Figure A.94. The catalog screen


256 Appendix A. Sample Programs

When the "Order" button is clicked, the order confirmation screen is displayed. The
content of the order is confirmed and the "Order issue" button is clicked.

Figure A.95. The order confirmation screen

When the "Order issue" button is clicked, the order result screen is displayed.
Appendix A. Sample Programs 257

Figure A.96. The order result screen

3. Online shopping is terminated.


The following information is set in URL and the "Execute" key is pressed.
http://user/sample22/admin.html

Because the administrator menu screen is displayed, the "Set ending business" is
clicked.
258 Appendix A. Sample Programs

Figure A.97. The administrator menu screen

Creating an Event Log


In this exercise, detailed information of errors detected by the program is output to
an event log by using the event log output subroutine. Note that the Event Viewer
discussed below is only available on Windows NT, Windows 2000, Windows Server
2003 and Windows XP.
1. The log of the application is selected by starting the Event Viewer from the
Administrative Tools.

Figure A.98. The Event Viewer window

2. Detailed information is displayed when the source selects the log of "NetCOBOL
Application", and double-clicks it.
Appendix A. Sample Programs 259

Figure A.99. The Event on Local Computer Property


260 Appendix A. Sample Programs

Sample 23: COM Program to Control Excel (Late Binding)


Sample 23 is a program that uses the COBOL COM client function to operate Excel.
The COBOL COM client function creates a COM object that uses functions which the
COM server supports in the same way as a COBOL method.
Excel is an independent application and also functions as a COM server. Therefore,
an application that operates Excel can be written with COBOL.
To run this program, the following products are necessary:
• Microsoft® Excel
"Excel" is used here as a generic name for these products.
For details on the COBOL COM function, refer to Chapter 24 in the "NetCOBOL User’s
Guide for Windows".

Overview
The sample program performs the operations listed below for Excel from a COBOL
application.
• Excel activation and termination
• Excel sheet open
• Work sheet selection and data insertion
• Graph creation
• Printing of a selected sheet
The COBOL COM client function supports the early binding and late binding methods
to recognize COM server methods and interfaces.
Late binding is used in this sample.
When late binding is used, usable COBOL descriptions are limited and execution
performance is slower than early binding, but late binding is freer from the effects of
COM server modifications than early binding. When using late binding this program
can operate Excel97 and Excel2000, which are different COM servers.
For details of early binding, see Sample 24, "COM Program to Control Excel (Early
Binding)."

Available Programs
• SAMPLE23.PRJ (Project file)
• SAMPLE23.COB (COBOL source file)
• GRAPHDATA.XLS (Excel file for test)

Available COBOL Functions


• COM client function
Appendix A. Sample Programs 261

Applicable COBOL Statements


The DISPLAY, IF, INVOKE, PERFORM, and SET statements are used.

Compiling, Linking, and Executing

Building/Rebuilding
The Project Manager's Build function is used to compile and link this program.
Folder C:\NetCOBOL is assumed below as the location where NetCOBOL is installed.
Change the folder name C:\NetCOBOL to the name of the folder where NetCOBOL is
installed.
1. Start the Project Manager.

Figure A.100. The Sample 23 project in the COBOL Project Manager

2. Open the project file SAMPLE23.PRJ.


3. Select "Build" on the "Project" menu.
→ After the build terminates, check that SAMPLE23.EXE is created.

Executing the Program


1. Select "Execute" on the "Project" menu.
→ Excel is started. The following message is also displayed on the COBOL
console and the system waits for input.
262 Appendix A. Sample Programs

Figure A.101. The Excel file name input screen for sample 23

2. Use the full path-name to specify an Excel file for the test.
→ The specified Excel file is opened and the sample program controls Excel.
The following message is displayed on the COBOL console and the system
waits for input again.

Figure A.102. The graph type input screen for sample 23

3. Input the type of graph to be created.


→ The specified graph is plotted. The following message is displayed on the
COBOL console and the system waits for input again.
Appendix A. Sample Programs 263

Figure A.103. The printer name input screen for sample 23

4. If the graph is to be printed, specify a printer name. If it is not to be printed,


press the ENTER key without inputting data.
→ Excel is terminated and the program is terminated. The graph is printed
before terminating Excel when a printer name is specified.
264 Appendix A. Sample Programs

Sample 24: COM Program to Control Excel (Early Binding)


Sample 24 is a program that uses the COBOL COM client early binding function to
operate Excel.
The COBOL COM client function creates a COM object to use functions which the
COM server supports in the same way as the COBOL method.
Excel is an independent application and also functions as a COM server. So, an
application that operates Excel can be written with COBOL.
To execute this program, the following products are necessary:
• Microsoft® Excel
"Excel" is used here as a generic name for these products.
For details on the COBOL COM function, refer to Chapter 24 in the "NetCOBOL
User’s Guide for Windows".

Overview
The sample program performs the operations listed below for Excel from a COBOL
application.
• Excel activation and termination
• Excel sheet open
• Work sheet selection and data insertion
• Graph creation
• Printing of selected sheet
The COBOL COM client function supports the early binding and late binding methods
to recognize COM server methods and interfaces.
Early binding is used in this sample.
When early binding is used, object property and in-line method invocation can be
written. Also, the performance is better than that of late binding. However, the
early binding method requires a type library during development and is liable to be
influenced by COM server modifications. This program uses the COM server name
EXCEL to use the Excel functions. An Excel type library must be specified for this
COM server name before compiling the program. Excel97 cannot be operated with
an executable file built by referencing an Excel2000 type library. Also, Excel2000
cannot be used with an executable file built by referencing an Excel97 type library.
For details of late binding, see Sample 23, "COM Program to Control Excel (Late
Binding)."

Available Programs
• SAMPLE24.PRJ (Project file)
• SAMPLE24.COB (COBOL source file)
Appendix A. Sample Programs 265

• GRAPHDATA.XLS (Excel file for test)

Applicable COBOL Functions


• COM client function
• Project management function

Applicable COBOL Statements


The DISPLAY, IF, INVOKE, PERFORM, and SET statements are used.

Compiling, Linking, and Executing

Building and Rebuilding


The Project Manager's Build function is used to compile and link this program.
Folder C:\NetCOBOL is assumed below as the location where NetCOBOL is installed.
Change the folder name C:\NetCOBOL to the name of the folder where NetCOBOL is
installed.
1. Start the Project Manager.

Figure A.103. The Sample 24 project in the COBOL Project Manager

2. Open the project file SAMPLE24.PRJ.


3. Select a project file and select "Compiler Options" from the "Project-Option"
menu.
→ The "Compiler Options" dialog is displayed.
4. Click the "COM Server Name" button in the "Compiler Options" dialog.
266 Appendix A. Sample Programs

Figure A.104. The COM Server Setting dialog

→ The "COM Server Setting" dialog is displayed.


5. Set data as shown below, assuming that type library Excel is installed in the
"C:\Program Files\Microsoft Office" with COM server name EXCEL.

EXCEL = C:\Program Files\Microsoft Office\Office\Excel8.olb

If type library Excel2000 is used (file name: Excel9.olb) or Excel is installed in


another folder, click the Change button to change the type library setting.
6. Select "Build" from the "Project" menu.
→ After build termination, check that SAMPLE24.EXE is created.
NOTE:
When you use Excel2002 or later, following modification is necessary.
• Program
Modify "sample24.cob" as following.

001040 INVOKE OBJ-CELL "SET-VALUE"


001050 USING PDATA(CELL-COL)
001060* If you use EXCEL2002 or later, replace the above statement with
001061* this statement:
001070* INVOKE OBJ-CELL "SET-VALUE"
001080* USING OMITTED PDATA(CELL-COL)

- Comment out line 1040 and 1050.


- Validate the comment line 1070 and 1080.

001040* INVOKE OBJ-CELL "SET-VALUE"


001050* USING PDATA(CELL-COL)
001060* If you use EXCEL2002 or later, replace the above statement with
001061* this statement:
001070 INVOKE OBJ-CELL "SET-VALUE"
001080 USING OMITTED PDATA(CELL-COL)

• Type library
The type library of Excel2002 or later is Excel.exe. Set COM server name as
shown below, assuming that Excel2002 is installed in the "C:\Program
Files\Microsoft Office".

EXCEL = C:\Program Files\Microsoft Office\Office\Excel.exe


Appendix A. Sample Programs 267

Executing the Program


1. Select "Execute" on the "Project" menu.
→ Excel is started. The following message is displayed on the COBOL console
and the system waits for input.

Figure A.105. The Excel file name input screen for sample

2. Use the full path-name to specify an Excel file for the test.
→ The specified Excel file is opened and the program controls Excel. The
following message is displayed on the COBOL console and the system waits
for input again.

Figure A.106. The graph type input screen for sample 24


268 Appendix A. Sample Programs

3. Input the type of a graph to be created.


→ The specified graph is plotted. The following message is displayed on the
COBOL console and the system waits for input again.

Figure A.107. The printer name input screen for sample 24

4. If the graph is to be printed, specify a printer name. If it is not to be printed,


press the ENTER key without inputting data.
→ Excel is terminated and the program is terminated. The graph is printed
before terminating Excel when a printer name is specified.
Appendix A. Sample Programs 269

Sample 25: Creating a COBOL COM Server Program


Sample 25 shows an example where the COBOL COM server function is used to
enable a COBOL program to be used as a COM server.
The COBOL class definition can be shifted to the COM server without change, by
using the COBOL COM server function. The selected class is published as the COM
server interface simply by setting the necessary information with the Project
Manager's COM sever creation function and executing rebuild.
For details on the COBOL COM function, refer to Chapter 24 in the "NetCOBOL User’s
Guide for Windows".
This program accesses a database via an ODBC driver, so the following products are
necessary to use this program:
• Database
• Products necessary for database access with ODBC
• ODBC driver
• ODBC driver manager
For details of database access using an ODBC driver, refer to Chapter 19 in the
"NetCOBOL User’s Guide for Windows".

Overview
The sample program consists of two class definitions.

Figure A.108. The overview of sample 25

The COBOL ONLINE_STORE class is the COM server class. The ONLINE_STORE
class provides the following functions for constructing an online store application:
• Authorization processing
• Stock check
• Order registration
• Order exact calculation
In addition to COBOL programs, programs generated with Visual C++®, programs
generated with Visual Basic®, and active server pages (ASP) Visual Basic® Scripting
Edition (VBScript) can be used as client programs to use these functions. See
Sample 26 and Sample 27 for examples of the COBOL client and ASP client.
270 Appendix A. Sample Programs

Available Programs
• DB_ACCESS.COB (COBOL source file)
• ONLINE_STORE.COB (COBOL source file)
• STORESV1.PRJ (project file)
• STORESV1.CBI (compiler option file)
• STORESV1_DLL.CSI (COM server information file)
• STORESV1.DEF (module definition file)

Applicable COBOL Functions


• COM server function
• Remote database access
• *COM-ARRAY class

Applicable COBOL Statements


The IF, INVOKE, INITIALIZE, SET, MOVE, and PERFORM statements are used.
The embedded SQL statements (COMMIT, CONNECT, INSERT, SELECT, UPDATE,
ROLLBACK, and DISCONNECT) are used.

Compiling, Linking, and Executing

Operation necessary before executing the program


• Construct an environment where a database can be accessed via the ODBC
driver.
• Set a default server to be connected and create the four tables shown below in a
database on the server.
- CUSTOMER table
Use the format shown below to create this table.
USERID PASSWORD ←Column name
Variable-length character Variable-length character ←Column attribute
32 bytes 32 bytes
↑ Primary key
Appendix A. Sample Programs 271

Store the data items shown below in the client table.


USERID PASSWORD
USER0001 USER0001
USER0002 USER0002
USER0003 USER0003
USER0004 USER0004
USER0005 USER0005
USER0006 USER0006
USER0007 USER0007
USER0008 USER0008
USER0009 USER0009
USER0010 USER0010

- STOCK table
Use the format shown below to create this table.
PRODUCTNUMBER QUANTITY ←Column name
Fixed-length character Fixed-length character ←Column attribute
10 bytes 10 digits
↑ Primary key
Store the data items shown below in the stock table.
PRODUCTNUMBER QUANTITY
FMV2TXH111 900000
FMV2TXH161 100000
FMV2TXH151 500000
FMV2TXF111 45000
FMV2TXF161 300000
FMV2TXF151 60000
FMV2DXH111 90000
FMV2DXH161 55000
FMV2DXH151 990000
FMV2DXF111 10000
FMV2DXF161 777700
FMV2DXF151 200000
FMV2DXD111 690000
FMV2DXD161 870000
FMV2DXD151 619000
FMV2DXA111 2900000
FMV2DXA161 8760000
FMV2DXA151 100000
FMV3NA3LC0 10000
FMV3NA3LC6 300
272 Appendix A. Sample Programs

- ORDERTABLE
Use the format shown below to create this table.
ORDERNUMBER USERID DATE ←Column name
Fixed-length Variable-length Fixed-length ←Column attribute
character character character
12 bytes 32 bytes 14 bytes
↑ Primary key
No data need be stored in the ORDERTABLE table.
- ORDERDETAIL table
Use the format shown below to create this table.
ORDERNUMBER PRODUCTNUMBER QUANTITY ←Column name
Fixed-length Fixed-length Decimal ←Column attribute
character character integer
12 bytes 10 bytes 10 digits

No data need be stored in the ORDERDETAIL table.


- Use the ODBC information file setting tool (SQLODBCS.EXE) to create an
ODBC information file (assumed to be C:\DBMSACS.INF).

Building and Rebuilding


The Project Manager's Build function is used to compile and link this program.
Folder C:\NetCOBOL is assumed below as the location where NetCOBOL is installed.
Change the folder name C:\NetCOBOL to the name of the folder where NetCOBOL is
installed.
1. Start the Project Manager.
2. Open the project file SAMPLE25.PRJ.
Appendix A. Sample Programs 273

Figure A.109. The STORESV1 project in the COBOL Project Manager

3. Check the set COM server information.


Select a target file (STORESV1.DLL) and select "View" from the "Project-Option-
COM server" menu.

Figure A.110. Select View from the Project-Option-COM server menu


274 Appendix A. Sample Programs

The "View" dialog is opened and the server information can be referenced.

Figure A.111. The View dialog

4. Select "Build" from the "Project" menu.


→ After build termination, check that STORESV1.DLL is created.

Setup the Server Program Execution Environment


1. Select "Run-time Environment Setup Tool" from the "Tools" menu of Project
Manager.
→ The run-time environment setup tool is displayed.
2. Select "Open" on the "File" menu and create an object initialization file
(COBOL85.CBR) in the folder that contains the dynamic link library
(STORESV1.DLL).
3. Select a common tab and set data as shown below:
- Specify an ODBC information file name in environment variable information
@ODBC_Inf (ODBC information file specification).
Appendix A. Sample Programs 275

Figure A.112. The Run-time Environment Setup Tool with COBOL85.CBR for Sample
25

4. Click the Apply button.


→ The data is saved in the object initialization file.
5. Select File, Exit to terminate the run-time environment setup tool.

Registering the COM Server


The created COBOL application must be registered in the Windows system to use it
as a COM server. There are two registration methods depending on how the COM
server is used.
• When the COM server and COM client are used on the same machine
→ Use REGSVR32.EXE to register the COBOL application in the system registry.
For details, refer to Section the "Registering or Deleting a COM Server" in the
"NetCOBOL User’s Guide for Windows".
• When the COM server is used from a COM client in a remote network-connected
machine.
→ Use Microsoft Transaction Server (MTS). Use MTS explorer to register the
COBOL application in the system registry and MTS. For details, refer to
276 Appendix A. Sample Programs

Section the "Registration in the MTS Environment" in the "NetCOBOL User’s


Guide".
Appendix A. Sample Programs 277

Sample 26: Using the COM Linkage-COBOL Server Program


(COBOL Client)
Sample 26 demonstrates a client program that uses the COBOL server program in
Sample 25 with the COBOL COM client function.
For details on the COBOL COM function, refer to Chapter 24 in the "NetCOBOL User’s
Guide for Windows".

Overview
The sample program uses the COBOL server program generated in Sample 25 to
create the online store application. The client program accepts data input on the
screen by using the screen operation function and requests the server program to
process the accepted data. The result of the server program processing is displayed
on the screen.

Figure A.113. The overview of Sample 26

Available Programs
• CLMAIN.COB (COBOL source program)
• ORDERSHEET-INFO.CBL (COBOL library file)
• PRODUCT-TABLE.CBL (COBOL library file)
• SCREENS.CBL (COBOL library file)
• SAMPLE26.PRJ (Project file)
• SAMPLE26.CBI (compiler option file)

Applicable COBOL Functions


• Screen function
• COM client function
• *COM-ARRAY class
278 Appendix A. Sample Programs

Applicable COBOL Statements


The ACCEPT (screen function), DISPLAY (screen function), EVALUATE, INVOKE, IF,
PERFORM, and SET statements are used.

Compiling, Linking, and Executing

Operations Necessary Before Executing the Program


When a COM sever in a remote network-connected machine is used, install the
server information in the machine where this program is to be executed as follows:
1. Generate a client information installation program in the machine where the
COM server is registered. For details on generation of the data, refer to the
section "Installation in the Client Machine" in the "NetCOBOL User’s Guide for
Windows".
2. Execute the client information installation program on the machine where this
program is to be executed. If this program is to be executed on the same
machine as the COM sever, this operation is unnecessary.

Building and Rebuilding


The Project Manager's build function is used to compile and link this program.
In the following screen snapshots, it is assumed that NetCOBOL was installed in
folder C:\NetCOBOL. Change the folder name C:\NetCOBOL to the name of the
folder where NetCOBOL is installed on your machine.
1. Start the Project Manager.
2. Open the project file SAMPLE26.PRJ.

Figure A.114. The Sample 26 project in the COBOL Project Manager


Appendix A. Sample Programs 279

3. Select the project file and select "Compiler Options" from the "Project-Option"
menu.
→ The "Compiler Options" dialog is displayed.
4. Click the COM Server button in the "Compiler Options" dialog.
→ The "COM Server" dialog is displayed.

Figure A.115. The COM Server Setting dialog

5. Specify STORESV1.DLL (type library) for COM server name STORESV1. Check
the entry, then click the OK button.
→ The "Compiler Options" dialog is redisplayed. Click the OK button to
redisplay the Project Manager window.
6. Select "Build" from the "Project" menu.
→ After build termination, check that SAMPLE26.EXE is created.

Configuring the Server Program Execution Environment


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 create an object initialization file
(COBOL85.CBR) in the folder that contains the executable program
(SAMPLE26.EXE).
3. Select the Common tab and enter data as shown below:
- Specify "(80,27)" in the environment variable information @ScrnSize (logical
screen size in screen operation).
280 Appendix A. Sample Programs

Figure A.116. The Run-time Environment Setup Tool with COBOL85.CBR for
Sample 26

- If the COBOL client program is executed on the same machine as the


server program, also set the server program execution environment
information by specifying the ODBC information file name in
environment variable information @ODBC_Inf (ODBC information file
specification).
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.

Executing
1. Select "Execute" on the "Project" menu.
→ The screen shown below is displayed. Input the user ID and password and
press the ENTER key (use the CURSOR or TAB key to move among the input
fields). Valid user IDs are USER0001 to USER0010 and the password is the
same as the user ID. Note that the entered password is not displayed.
Appendix A. Sample Programs 281

Figure A.117. The User ID and Password input screen

2. The menu screen is displayed. Input "1" and press the ENTER key.

Figure A.118. The menu screen

3. The catalog screen is displayed. Input the number of items to be ordered. Use
the CURSOR or TAB key to move among the input fields. After entering the
data, press the ENTER key.
282 Appendix A. Sample Programs

Figure A.119. The catalog screen

4. The order check screen is displayed. Input "Y" and press the ENTER key.

Figure A.120. The order check screen


Appendix A. Sample Programs 283

5. The order list screen is displayed. The (2) menu screen is redisplayed when the
ENTER key is pressed.

Figure A.121. The order list screen

6. To terminate the processing, input "2" on the menu screen and press the ENTER
key.
284 Appendix A. Sample Programs

Sample 27: Using the COM Linkage-COBOL Server Program


(ASP Client)
Sample 27 shows an example where a COM server created using the COBOL COM
server function is used by calling it from active sever pages (ASP) Visual Basic®
Scripting Edition (VBScript).
For details on ASP and VBScript, refer to commercially available handbooks.
• To use this program, the following products are necessary:
- Microsoft® Windows NT® Server Network operating system Version 4.0
- Microsoft® Windows NT® Server Network operating system, Enterprise
Edition Version 4.0
- Microsoft® Windows® 2000 Server operating system
- Microsoft® Windows® 2000 Advanced Server operating system
- Microsoft® Windows 2003™ Server Standard Edition
- Microsoft® Windows 2003™ Server Enterprise Edition
• Microsoft® Internet Information Server 4.0 or later
For Windows NT® 4.0, Microsoft® Windows NT® OptionPack 4.0 is necessary.

Overview
ASP is one of the methods of constructing dynamic Web applications by embedding
script language in the HTML document.
A COM server object can be created in ASP VBScript by using a server object in
which the CreateObject method is installed. The method provided by the COM
server can be called from the created object.
Use this function to create online store Web applications using the COBOL server
program in Sample 25.

Available Programs
• MENU.ASP (ASP page file)
• AUTH.ASP (ASP page file)
• CATALOG.ASP (ASP page file)
• CONFIRM.ASP (ASP page file)
• ORDER.ASP (ASP page file)
• STYLE.CSS (style sheet file)
• CATALOGTITLE.GIF (image file)
• FJLOGO.GIF (image file)
Appendix A. Sample Programs 285

Operations Necessary Before Execution


The COM server program created in Sample 25 is used in this sample. Build the COM
server program, register it as the COM server, and configure the execution
environment information.
Next, register Sample 27 with the Internet Service Manager as follows:
1. Activate the Internet Service Manager, select "specified Web site", and select
"Virtual Directory" from the "New Creation" field on the "Context" menu.

Figure A.122. The Internet Information Services window

2. Input a virtual directory name, and then specify the physical path of Sample 27
with the ASP page file.
286 Appendix A. Sample Programs

Figure A.123. The Virtual Directory Creation Wizard

Execution
Domain name "user" and virtual directory name "sample27" are assumed.
Microsoft Internet Explorer is used as the WWW browser.
1. Input the URL as shown below.
The menu screen is displayed. Select "Catalog Shopping" and click the OK
button.
Appendix A. Sample Programs 287

Figure A.124. The menu screen

2. The member authorization screen is displayed. Input a user ID and password


and click the OK button. Valid user ID’s are USER0001 to USER0010. The
passwords are identical to the user ID.
Note that the entered password is not displayed.

Figure A.125. The member authorization screen


288 Appendix A. Sample Programs

3. The catalog screen is displayed. Input the number of items to be ordered and
click the Order button.

Figure A.126. The catalog screen

4. The order check screen is displayed. Click the Order Issue button.
Appendix A. Sample Programs 289

Figure A.127. The order check screen

5. The order reception screen is displayed. The (2) menu screen is redisplayed
when the Menu button is clicked.
290 Appendix A. Sample Programs

Figure A.128. The order reception screen


Appendix A. Sample Programs 291

Sample 28: Transaction Management with COM Linkage-


MTS
Sample 28 demonstrates COBOL COM server program transaction management using
the Microsoft® Transaction Server (MTS).
See Chapter 24 in the "NetCOBOL User’s Guide for Windows" for details on COBOL
application transaction management using MTS.
To use this program, the following products are necessary:
• One of the following products:
- Microsoft® Windows NT® Server Network operating system Version 4.0
- Microsoft® Windows NT® Server Network operating system, Enterprise
Edition Version 4.0
- Microsoft® Windows® 2000 Server operating system
- Microsoft® Windows® 2000 Advanced Server operating system
- Microsoft® Windows 2003™ Server Standard Edition
- Microsoft® Windows 2003™ Server Enterprise Edition
• Microsoft® Transaction Server 2.0 or later
For Windows NT® 4.0, Microsoft® Windows NT® OptionPack 4.0 is necessary.
This program accesses a database via the ODBC driver, so the following products are
necessary to run this program:
• Database
• Products necessary for database access with ODBC
• ODBC driver
• ODBC driver manager
For database access using an ODBC driver, refer to Chapter 19 in the "NetCOBOL
User’s Guide for Windows".

Overview
As in Sample 25, this sample program provides the following functions for
constructing an online store application:
• Authorization processing
• Stock check
• Order registration
• Order calculation
However, this program manages transactions directly from a COBOL program by
using the MTS functions.
In Sample 25, the embedded SQL statements COMMIT/ROLLBACK are used and
transaction management depends on the database. This is a simple approach for
292 Appendix A. Sample Programs

someone who is familiar with embedded SQL statements but increases the database
processing load.

Figure A.129. The overview of Sample 25

This program manages transactions by itself with MTS functions. This reduces the
database processing load and enables more detailed transaction management.

Figure A.130. The overview of Sample 28

There are two methods of using the MTS function for transaction management from
a COBOL application:
• Control of transactions where the object is operating from the COM server object
• Control of transactions where the COM server is operating from the COM client
The first method is shown below.

Available Programs
• DB_ACCESS.COB (COBOL source file)
• ONLINE_STORE.COB (COBOL source file)
• STORESV2.PRJ (project file)
• STORESV2.CBI (compiler option file)
• STORESV2_DLL.CSI (COM server information file)
• STORESV2.DEF (module definition file)
Appendix A. Sample Programs 293

Applicable COBOL Functions


• COM server function
• Remote database access
• *COM-ARRAY class
• Object context object

Applicable COBOL Statements


The IF, INVOKE, INITIALIZE, SET, MOVE, and PERFORM statements are used.
The embedded SQL statements (CONNECT, INSERT, SELECT, UPDATE, ROLLBACK,
and DISCONNECT) are used.

Compiling, linking, and Executing

Operations Necessary Before Execution


• Construct an environment where a database can be accessed via the ODBC
driver.
• Set a default server to be connected to and create the five tables listed below in
a database on the server. For the formats of the tables and data to be stored,
see Sample 25, "Creating a COBOL COM Server Program “.
- CUSTOMER table
- STOCK table
- ORDERTABLE table
- ORDERDETAIL table
• Use the ODBC information file tool (SQLODBCS.EXE) to create an ODBC
information file (assumed to be C:\DBMSACS.INF).

Building and Rebuilding


The Project Manager's build function is used to compile and link this program.
In the screen snapshots below, it is assumed that NetCOBOL was installed in folder
C:\NetCOBOL. Change the folder name C:\NetCOBOL to the name of the folder
where NetCOBOL is installed on your machine.
1. Start the Project Manager.
2. Open the project file SAMPLE28.PRJ.
294 Appendix A. Sample Programs

Figure A.131. The STORESV2 project in the COBOL Project Manager

3. Check the COM server information.


Select a target file (STORESV2.DLL) and select "View" from the "Project-Option-
COM Server" menu.

Figure A.132. Select View from the Project-Option-COM Server menu

The "View" dialog is opened and the server information can be referenced.
Appendix A. Sample Programs 295

Figure A.133. The View dialog

4. Select "Build" from the "Project" menu.


→ After build termination, check that SAMPLE28.DLL is created.

Registering the COBOL Application in MTS


The COBOL application must be registered in the Windows system to use it as a COM
server. Also specify a transaction control method in the registration.
Use the MTS explorer to register the COBOL application in the system registry and
MTS. For details, refer to the section "Registration in the MTS Environment" in the
"NetCOBOL User’s Guide for Windows".

Setup the Server Execution Environment


1. Select "Run-time Environment Setup Tool" from the "Tools" menu of Project
Manager.
→ The run-time environment setup tool is displayed.
296 Appendix A. Sample Programs

Figure A.134. The Run-time Environment Setup Tool with COBOL85.CBR for
Sample 28

2. Select "Open" on the "File" menu and create an object initialization file
(COBOL85.CBR) in the folder that contains the dynamic link library
(SAMPLE28.DLL).
3. Select the Common tab and enter data as shown below:
- Specify an ODBC information file name in the environment variable
@ODBC_Inf (ODBC information file specification).
4. Click the Apply button.
→ The data is saved in the object initialization file.
5. Select "Exit" on the "File" menu to exit the tool.

Modifying the Client Program


Samples 26 and 27 can be used as clients of this program by modifying them as
shown below.
• Using the COBOL client in Sample 26.
- Modify the cluster specifier of the CLMAIN.COB repository as shown below.
Appendix A. Sample Programs 297

000200 CLASS ONLINE_STORE AS "*COM:STORESV2:ONLINE_STORE"

- Open the project file SAMPLE26.PRJ, delete COM server name STORESV1,
and enter STORESV2.

Figure A.135. The Add COM Server dialog

- Rebuild SAMPLE26.EXE.
- Create an installation program for client information on the Sample 28
program and install it in the client.
• Using the ASP client in Sample 27.
Modify the CreateObject argument for COM object creation as shown below.
Catalog.asp

Set OLSService = Server.CreateObject("STORESV2.ONLINE_STORE")

Confirm.asp

Set OLSService = Server.CreateObject("STORESV2.ONLINE_STORE")


298 Appendix A. Sample Programs

Order.asp

Set obj = Server.CreateObject("STORESV2.ONLINE_STORE")


Appendix A. Sample Programs 299

Sample 29: Inter-application Communication Function


Sample 29 demonstrates how to use the simplified inter-application communication
function to read or write messages to or from logical destinations. For details on how
to use the simplified inter-application communication function for message transfer,
refer to the topic “Using Simplified Inter-application Communication” in Chapter 18 of
the “NetCOBOL User’s Guide for Windows.”

Overview
The sample program transfers messages between SAMPLE29 and COMMU.
SAMPLE29 establishes a connection to server "SERVER1", writes messages to logical
destination "MYLD1", and reads messages from logical destination "MYLD2". If no
message comes from logical destination "MYLD2", SAMPLE29 waits for the message
arrival for up to 60 seconds. After reading messages from the logical destination
"MYLD2", SAMPLE29 reads messages from logical destination "MYLD1" in order of
priority.
COMMU establishes a connection to server "SERVER1", reads messages from logical
destination "MYLD1", then writes messages to logical destinations "MYLD1" and
“MYLD2”.

Figure A.136. Reading and writing messages between logical destinations

Available Programs
• SAMPLE29.COB (COBOL source program)
• COMMU.COB (COBOL source program)
• SAMPLE29.PRJ (Project file)
• PRM_REC.CBL (library text)
• SAMPLE29.INI (logical destination definition file)

Applicable COBOL Functions


• Using Simplified Inter-application Communication
300 Appendix A. Sample Programs

Applicable COBOL Statements


The CALL, DISPLAY, IF, and MOVE statements are used.

Prerequisites to Execution
To make the Sample 29 programs ready for execution, execute the following utility:
• COBCISRV
After you run this, you can build and execute Sample 29.
You may refer to Chapter 18, “Communication Functions” in the “NetCOBOL User’s
Guide for Windows” for more general information on the following topics:
• Activating the server for simplified inter-application communication.
• Creating logical destinations "LD1" and "LD2" on the server for simplified inter-
application communication.
• Changing the information on the opponent machine name of the logical
destination definition file (SAMPLE29.INI) by using the logical destination
definition file creation utility (COBCIU32.EXE). Specify the host name of the
server as the opponent machine name and click on the Set button.

Building the Program


The project file for Sample 29 is SAMPLE29.PRJ in the SAMPLE29 directory. Select
“Open Project” from the “File” menu to load it into the COBOL Project Manager
window.
Fully opened, this simple project includes two separate executable files that share
the same copy libraries, as shown in the following figure.

Figure A.137. The Sample 29 project in the COBOL Project Manager

Build the project by selecting Build from the Project menu.


Appendix A. Sample Programs 301

The MAIN and TEST compiler options have been specified.

Setup the Server Program Execution Environment


1. Select "Run-time Environment Setup Tool" from the "Tools" menu of Project
Manager.
→ The run-time environment setup tool is displayed.

Figure A.138. The Run-time Environment Setup Tool with COBOL85.CBR for
Sample 29

2. Select "Open" on the "File" menu and create an object initialization file
(COBOL85.CBR) in the folder that contains the executable file (SAMPLE29.EXE).
3. Select the Common tab and enter data as shown below:
- The path name of the logical destination definition file of the @CBR_CIINF
environment variable has been pre-assigned to SAMPLE29.INI. This is the
default setting for both the SAMPLE29 and COMMU executable files.
4. Click the Apply button.
→ The data is saved in the run-time initialization file.
302 Appendix A. Sample Programs

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

Debugging the Program


To run these programs under the control of the Debugger, select Debug from the
Tools menu. The Start Debugging dialog box appears. Press the ENTER key.
When the source is read into the COBOL Debugger, it will look like the following
figure.

Figure A.139. Source for COMMU.COB in Sample 29

Executing the Program


To execute this sample, you must start both executable files—SAMPLE29.EXE and
COMMU.EXE. You can do this by double-clicking the mouse on the executable files in
the COBOL Project Manager window.
Appendix A. Sample Programs 303

Execution Result

Figure A.140. The Sample 29 console window

Check the messages displayed on the console to ensure the following:


In the SAMPLE29 console window:
1. Messages from COMMU have been read from logical destination "MYLD2".
2. Messages have been read from logical destination "MYLD1" in order of priority.

Figure A.141. The COMMU console window

In the COMMU console window:


304 Appendix A. Sample Programs

1. Messages from SAMPLE29 have been read from logical destination "MYLD1".
2. Messages have been written to logical destination "MYLD1" and logical
destination "MYLD2".

Sample 31: Windows System Function Call


Sample 31 demonstrates how to invoke a Windows system function – for this
example, a call to create a message box.

Overview
Sample 31 calls the Windows system function "MessageBoxA" to display a message
in a message box with YES,NO and Cancel buttons. (Note that an "A" needs to be
appended to the function call when the function call contains a character string
parameter and you are working in ASCII, as opposed to Unicode data, when the
suffix is "W".)
The call uses the STDCALL calling convention.
The message box returns a value indicating which button was pressed. This value is
returned in the data item specified in the RETURNING phrase.
It is possible to refer to this return value in a batch file. In a batch file, you can
access this return value by the name ERRORLEVEL. SAMPLE31.BAT demonstrates
this as follows.

Figure A.142. The batch file for ERRORLEVEL

Files Included in Sample 31


• MSGBOX.COB (COBOL source program)
• SAMPLE31.PRJ (Project file)
• SAMPLE31.CBI (Compiler option file)
• SAMPLE31.BAT (Batch file for start)
Appendix A. Sample Programs 305

COBOL Statements Used


• Method of calling C program from COBOL program
• STDCALL call convention
• Parameter transfer BY VALUE
• RETURNING phrase of CALL statement
• Special register PROGRAM-STATUS (RETURN-CODE)
• Project Manager

NOTE:
1. Most Windows system functions (and C routines in general) require that strings
be terminated with a null byte (X"00" or LOW-VALUE). The sample shows how
you can place these bytes using reference modification.
2. The Windows system function names are case sensitive. So be sure to get the
case correct as in "MessageBoxA". You need to specify the compiler option
"NOALPHAL" or "ALPHAL(WORD)" to ensure the COBOL system uses mixed case
for the function name.

Building the Program


Project manager's build function is used to make the executable program.
1. Start the Project Manager.
2. Open project file SAMPLE31.PRJ.

Figure A.143. The Sample 31 project in the COBOL Project Manager

Library file "USER32.LIB" is required to link the program. It is used to ensure the
linker can resolve the external reference "MessageBoxA". If you installed the
product to something other than the default folder structure, you should change
the folder name to match the folder structure you are using.
306 Appendix A. Sample Programs

3. Select "Build" from the project menu.

Executing the Program


1. Open a Command Prompt, change directories to the SAMPLE31 folder, and
execute SAMPLE31.BAT.

Figure A.144. Execute SAMPLE31.BAT

2. The following message boxes are displayed. Click one of the buttons.

Figure A.145. The message box for Sample 31


Appendix A. Sample Programs 307

3. The COBOL program detects which button was pressed and indicates such by
displaying a message.

Figure A.146. Click the Yes button

4. When the [Cancel] button is clicked, the program is executed again.


308 Appendix A. Sample Programs

Sample 32: Starting Another Program


Sample 32 demonstrates a program that starts another program, waits for the
started program to terminate, and receives a completion code from the started
program via inter-program communications.

Overview
When SAMPLE32 is executed, you are prompted for a program to run. If you enter
nothing, the program MSGBOX.EXE from the SAMPLE31 folder is executed. If you
specify a program name, you must enter the fully qualified path to the location of the
application (or batch file) to execute.
The Windows system function “CreateProcessA” is called specifying this for the
argument, and specified program or batch file is started.
If the specified program is successfully started, SAMPLE32 then waits until the
specified program terminates and then receives the completion code from the started
program.

Files Included in Sample 32


• SAMPLE32.COB(COBOL source program)
• SAMPLE32.PRJ (Project file)

COBOL Statements Used


• Method of calling a C program from COBOL program
• STDCALL call convention
• Parameter transfer in BY VALUE
• RETURNING phrase of CALL statement
• STORED-CHAR-LENGTH function
• Project Manager

Compiling, Linking, and Executing

Before Executing the Application


• SAMPLE32 uses MSGBOX.EXE from SAMPLE 31. Therefore, please build the
MSGBOX application prior to executing SAMPLE32.
• In the following screens, SAMPLE06 is also executed, therefore please build
SAMPLE06 prior to executing SAMPLE32.

Building and Rebuilding


The Project Manager's build function is used to compile and link this program.
Appendix A. Sample Programs 309

In the screen snapshots below, NetCOBOL was installed in folder C:\NetCOBOL.


Change the folder name C:\NetCOBOL to the name of the folder where NetCOBOL is
installed on your machine.
1. Start the Project Manager.
2. Open the project file SAMPLE32.PRJ.

Figure A.147. The Sample 32 project in the COBOL Project Manager

3. Select "Build" from the "Project" menu.


→ After build termination, check that SAMPLE32.EXE is created.

Executing the Program


1. Select "Execute" on the "Project" menu.
→ The following display appears and waits for your input.
310 Appendix A. Sample Programs

Figure A.148. The path name input screen for sample 32

2. Input the path and filename of an executable program or batch file. The
environment variable PATH is not referenced here, therefore it is necessary to
specify a relative path from the SAMPLE32 folder, or a fully qualified path name.
If nothing is entered, then MSGBOX.EXE of SAMPLE31 is executed. Press the
ENTER key.
3. The completion code of MSGBOX.EXE of SAMPLE31 is displayed (indicating what
button was pushed). In the following screen, the “No” button was pressed.

Figure A.149. The result of Sample 32 (input no character)

4. If SAMPLE32 is reexecuted and an executable program or batch file name is


specified, you are then prompted to enter command line arguments (if any) for
the EXE or BAT file, as shown below.
Appendix A. Sample Programs 311

Figure A.150. The console after input the path name

6. SAMPLE06.EXE requires two command line arguments. Please note that the
command line arguments are specified following the program name.

Figure A.151. Input the command line arguments

7. A message indicating that SAMPLE06 has been started is displayed. (The system
console is opened and the execution result of SAMPLE06 is output.) The
completion code of SAMPLE06.EXE is displayed and execution ends.
312 Appendix A. Sample Programs

Figure A.152. The result of Sample 32 (input the path name)


Appendix B. Tips
1. To build a module or application in Project Manager you must have the
executable module or project selected in the project tree view. If any other item
is selected in the tree view, the build function is disabled.
2. To set compiler and linker options in Project Manager you must have the
project selected in the project tree view.
3. Before invoking the Data function in the debugger, if possible, place the
cursor on the data item name you want to inspect. This saves you having to
enter the data name in the Data dialog.
4. The Data file I/O function in the debugger is targeted at Fujitsu-specific
data file features, only likely to be found in applications being ported from other
Fujitsu environments (e.g. mainframe and UNIX machines).
5. To create a class information database you first use the menu bar Tools,
Class Database, Options dialog to set up the folders containing the class
repositories. You then select the Tools, Class Database, Creation function to
create the database.
6. To create a project information database you make sure that "Creation
Project Database" is checked on the Project, Option sub-menu, then build the
project.
7. For more information on NetCOBOL check the following sources:
Softcopy documentation - All the NetCOBOL manuals are available in
softcopy form (Adobe Acrobat pdf). You can access the documentation from the
Start, Programs, NetCOBOL for Windows menu. If you do not have the
documentation installed, reinsert your NetCOBOL for Windows CD, if necessary
run Autorun.exe, and select "Softcopy Documentation" from the Master Setup
menu.
Help - Most components come with on-line help that explains interface details
and command formats.
8. Understanding PowerGEM Development Manager.
There are a couple of architectural features that have influenced the menu
design but may not be obvious when first using the tool:
a) Development Manager has the potential for being a development
environment. We recommend you use Development Manager for maintaining
the configuration management system and the controlling application releases.
You can configure other tools into Development Manager to aid you in these
tasks. Project Manager is the appropriate tool for COBOL program development.
b) The configuration management system is separate from Development
Manager. All the functions on the CM (Configuration Management) menu are
serviced by the configuration management system, not Development Manager.
This means:
- if you want help on configuration management you need to invoke one of the
CM functions (such as Utility), then select Help from that dialog,
- if you want to configure CM behavior you need to use the CM, Setup
Environment function.
314 Appendix B. Tips

314
Appendix C. Support
We want your evaluation to be productive so don't hesitate to contact us if there are
questions you need answered or points you believe we have missed in introducing
you to our products.
Use one of the following:
• Check our support pages at "www.netcobol.com/support/". They contain lists of
tips, frequently asked questions, white papers and other helpful information.
• Use the Question or Incident reporting forms supplied in the support pages to
shoot your question or problem into our tracking and answering system. We aim
to respond within 24 hours.
• Email our support group at: support@netcobol.com
• For more general COBOL questions email: COBOL@netcobol.com
• Call our support line on 1-408-428-0500.
316 Appendix C. Support

316
Index
@MGPRM, 143 class library, 200
A brief history of COBOL, 63 CLOSE, 135, 138
ACCEPT, 128, 138 CM, 45
FROM argument-name, 150 CM Status, 51
FROM environment-name, 154 COBOL
aggregation, 211 compiler, 11
ANSI 74, 19 dialects, 11
ANSI 85, 19 files, 26
application OO, 53
building, 24 COBOL DLL
execution, 25 interaction with Visual Basic, 89
argument COBOL File Utility, 26
command line, 150 collection class, 200
arrays, 91 command line argument, 150
benchmarks, 11 compiler
browse data files, 26 option setting, 25, 27, 313
building COMPUTE, 150
applications, 24 configuration management, 12, 43
errors, 24 adding files, 50
BY VALUE, 305 check in, 53
CALL, 143 check out, 52
calling configuring, 49, 313
COBOL from VB, 185, 190 free format code, 49
Windows system functions, 304 help, 313
calling convention in Development Manager, 313
STDCALL, 304 in Project Manager, 51
CHARACTER TYPE, 161 maintaining, 44
Class Browser, 58 CONNECT, 171
invoking, 59 convert data files, 26
window, 60 data file i/o function, 313
class information database, 59 data files
creating, 59 converting, 26
tip, 313 editing, 26
318 Index

maintenance, 11, 26 DECLARE CURSOR, 171


database access, 170 development environment, 11
debugging Development Manager, 44, 313
animate, 30 dialects, 11
automating, 34 DISCONNECT, 171
break on condition, 32 DISPLAY, 128
break on data change, 31 UPON environment-name, 154
breaking execution, 31 DIVIDE, 150
change start point, 30 DLL
commands, 11, 36, 38 creating, 69
continue menu, 30 documentation, 313
data file i/o function, 313 editing
datatips, 33 data files, 26
debug data dialog, 34 OO COBOL, 56
execution control, 30 source, 23
features, 29 editor
history file, 35 saving, 24
information, 42 status bar, 24
invoking, 27 email addresses, 315
key features, 29 encapsulation, 126, 197, 208, 218
mixed language applications, 187, 194 Enterprise Edition, 26, 43
monitoring data, 33 environment variables, 154
other languages, 11 Event driven programming, 65
output log, 35 Excel, 215
playback, 35 exception handling, 214
preparing for, 27 executing, 25
recording, 34 EXIT PROGRAM statement, 92
redebug, 30 extend data files, 26
specific execution, 30 FCB, 161
start window, 41 FETCH, 171
tooltips, 30 Forms Control Buffers, 161
tour, 27 FOVLDIR, 168
tracing execution, 34 free format code, 23, 49, 144
watch data window, 34 frequently asked questions, 315
window, 28 Fujitsu COBOL
with Visual Basic, 38
Index 319

developing a mixed language application, LIB, 22


71
PowerGEM, 44
development environment, 17
source, 22
interacting with Visual Basic, 69
line sequential files, 135
parameter passing with Visual Basic, 90
linker
Fujitsu COBOL V3 Early Release, 65
options, 25, 313
using, 68
load data files, 26
GUI applications, 63
make file, 24
HICOBOL program, 75
message box, 304
I control record, 161
methods
incident reporting, 315
calling, 197
indexed files, 135
in-line calls, 214, 219
recovery, 26
Microsoft Access
reorganizing, 26
sample database (Test.mdb), 172
statistics, 26
Microsoft Excel, 211
inheritance, 209, 218
multiple inheritance, 214
multiple, 214
object
in-line method calls, 214, 219
properties, 209
integration
object creation, 209, 214
other languages, 10, 38
object files
tools, 10, 11, 26
standard, 11
Internet addresses, 315
object generation, 219
INVOKE, 53, 198, 209
object properties, 214, 219
iteration, 211, 212
object-oriented programming, 197, 200,
JMPCINT2, 41, 185 211, 218, 223
JMPCINT3, 41, 185 objects generation, 126, 197
key features ODBC, 11, 170
debugging, 29 32 bit support, 171, 228
Fujitsu COBOL, 11 defining an ODBC source, 172, 228
Project Manager, 11 OLE connection, 214
V5, 12 OO COBOL
languages aggregation, 211
debugging, 11, 38 calling methods, 197
other, 10, 11 Class Browser, 58
legacy applications, 67 class definition, 55
library files class hierarchy, 60
320 Index

class information database, 59 guided tour, 17


class library, 200 OO COBOL tour, 18
class objects, 53 Project Manager tour, 17
classes, 53, 60 parameter
collection class, 200 command line, 150
encapsulation, 126, 197, 218 pencil icon, 52
exception handling, 214 performance, 17
factory object, 53 persistence, 223
inheritance, 56, 218 phone numbers, 315
in-line method calls, 214 polymorphism, 218
introduction, 53 PowerBSORT OCX, 65
INVOKE, 214 PowerCOBOL, 67
iteration, 211 GUI development, 67
methods, 53 PowerGEM, 43
multiple inheritance, 214 adding files, 50
object creation, 214 creating libraries, 44
object properties, 214 Development Manager, 44, 313
OLE connection, 214 free format code, 49
persistence, 223 libraries, 44
polymorphism, 218 library icon, 49
Project Browser, 57 mapping libraries, 48
project information database, 57 member, 48
Project Manager, 54, 214 overview, 44
project tree, 54 retrieve, 53
repositories, 54, 55 work spaces, 44, 48
REPOSITORY, 214 print data files, 26
SET, 214 printing, 158, 161
singleton, 211 PRINTING MODE, 161
source, 56 PRINTING POSITION, 161
tour, 54 problems, 315
type definitions, 214 PROGRAMMING-STAFF (P-STAFF), 65
OPEN, 135 project
Oracle, 11 opening, 20
overview Project Browser, 57
configuration management tour, 18 invoking, 58
debugging tour, 18 window, 58
Index 321

project information database, 57 reliability, 10, 11, 17


tip, 313 repositories, 54, 55
project management, 209 storage folder, 59
Project Manager, 17, 19 REPOSITORY, 198, 209, 219
build, 313 RETURNING, 305
building, 24 ROLLBACK, 171
changing icons, 51 sample05, 21, 27, 45, 51
check in, 53 sample11, 40
check out, 52 sample15, 54
Class Browser, 59 Screen Section, 138
class information database, 59, 313 sequence numbers, 50
compiler options, 313 SET, 198
configuration management, 43, 51 singleton, 211
customizing, 26 sort data files, 26
displaying CM information, 51 source
executing, 25 editing, 23
invoking, 20 free format, 23
linker options, 313 source control, 52
OO COBOL, 54 speed, 11, 17
path string, 53 SQL, 170
popup menu, 22 databases, 11
project information database, 57, 313 embedded, 11
retrieve, 53 embedded statements, 125, 171
specify execution options, 53 SQL Server, 11
using, 19 STDCALL, 304
window, 22 STRING, 154
project tree, 11 subprogram, 143
contracted, 21 support, 315
expanded, 22 TEST compiler option, 27
expanding, 22 tips, 313
OO COBOL, 54 type definitions, 214
questions, 315 unload data files, 26
READ, 135 Visual Basic, 63, 67, 185, 190
readability, 23, 29 COBOL DLL interaction, 89
recovery of indexed files, 26 creating the programming logic, 84
relational databases, 11 debugging with, 11, 38
322 Index

defining the COBOL components, 86 Windows function calls, 304


developing a GUI, 80 Windows message queue, 66
parameter passing with Fujitsu COBOL, 90 work spaces, 44, 48
Visual Basic COBOL Application, 69 WRITE, 135
web site, 315

You might also like