Skip to main content

Demo - C to Cobol, retrieving in the C code the COBOL STDOUT ( cobcall, cobfunc)

  • February 15, 2013
  • 0 replies
  • 0 views

Problem:

This KB has the same aim than the KB No. 25663.

(http://supportline.microfocus.com/mf_kb_display.asp?kbnumber=25663).

=====

The demonstration is attached.

Customer need:

C calling COBOL

AND redirect COBOL STDOUT

AND retrieve COBOL STDOUT in the C code which has called a COBOL shared library

=====

The main difference is that C code in KB 25663 uses C primitives dlopen & dlsym & dlcose when C code in this KB uses MF "C Functions for Calling COBOL" which makes the C code to be simpler and more portable.

Another thing here is that the Cobol Shared library is loaded 3 times and cancelled every time is has been loaded, just to show that the cobcancel function & cobfunc functions CANCEL the shared library

( see in LOG file the comment "BEGIN CobolFunc Vn" n being equal to 0 or 1 or 2)

( the Cobol Shared library was relinked every time before to be called again)

Resolution:

The demonstration :

The C code CCobStdoutMF.c

>Sets en environnent variable

(setenv primitive)

>Gets the content of this environment variable

(getenv primitive)

>Calls the COBOL function

(MF cobcall)

This Cobol function uses the OUTDD directive to redirect its STDOUT,

Using in the OUTDD the environment variable set in the C code

> Cancels the COBOL program

( MF cobcancel)

> Waits you type ENTER

(fgets)

Initial DISPLAY in CobolFunction.cbl:

BEGIN CobolFunc V0

Modified to

BEGIN CobolFunc V1

cob -z CobolFunction.cbl in another UNIX session

> Calls the COBOL function

( cobfunc: Calls a COBOL program, subprogram or entry point and then cancels it.)

> Waits you type ENTER

(fgets)

Initial DISPLAY in CobolFunction.cbl:

BEGIN CobolFunc V1

Modified to

BEGIN CobolFunc V2

cob -z CobolFunction.cbl in another UNIX session

> Calls the COBOL function

( cobfunc: Calls a COBOL program, subprogram or entry point and then cancels it.)

  

Back in the C code,The C code will retrieve the COBOL STDOUT content

>Opens  COBOL Stdout file

(fopen primitive)

>Obtains COBOL Stdout file size

(ftell primitive)

>Dynamic Allocates memory for later read

(malloc primitive)

>Read COBOL STDOUT

(fread primitive)

>Terminates ..

(fclose,free)

The .tar attached contains

A sample script DoclMF.sh which compiles and links C and Cobol code

cob -x  CCobStdoutMF.c

cob -z CobolFunction.cbl

To launch de demonstration, Just launch CCobStdoutMF.

And You would get this STDOUT

$ CCobStdoutMF

*--> Set Environment Variable: CobolStdout

*--> Get Environment Variable: CobolStdout

*--> cobinit: Initializes the COBOL environment...

*--> Call Cobol Function: CobolFunction

        1st load of COBOL Function

        2nd load of COBOL Function Type Enter

        3rd load of COBOL Function Type Enter

*--> cobtidy: Tidies up the global COBOL run-time environment

*--> Read content of Cobol Stdout, File: CobolStdout.log

*=============================

*--> BEGIN CobolFunc V0                    !!!!! V0

*--> After read CobolStdout          = CobolStdout.log

    << sample COBOL DISPLAY statement >>

    << sample COBOL DISPLAY statement >>

    << sample COBOL DISPLAY statement >>

    << sample COBOL DISPLAY statement >>

    << sample COBOL DISPLAY statement >>

*--> END   CobolFunc

*=============================

*=============================

*--> BEGIN CobolFunc V1                    !!!!! V1 just LINKED new Shared library has been loaded   

*--> After read CobolStdout          = CobolStdout.log

    << sample COBOL DISPLAY statement >>

    << sample COBOL DISPLAY statement >>

    << sample COBOL DISPLAY statement >>

    << sample COBOL DISPLAY statement >>

    << sample COBOL DISPLAY statement >>

*--> END   CobolFunc

*=============================

*=============================

*--> BEGIN CobolFunc V2                    !!!!! V2 just LINKED new Shared library has been loaded    

*--> After read CobolStdout          = CobolStdout.log

    << sample COBOL DISPLAY statement >>

    << sample COBOL DISPLAY statement >>

    << sample COBOL DISPLAY statement >>

    << sample COBOL DISPLAY statement >>

    << sample COBOL DISPLAY statement >>

*--> END   CobolFunc

*=============================

Attachments:

CCobStdoutMF.tar

Old KB# 2356

0 replies

Be the first to reply!