Skip to main content

Demo - C to Cobol, retrieving in the C code the COBOL STDOUT ( dlopen,dlsym,...)

  • February 15, 2013
  • 0 replies
  • 0 views

Problem:

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 C code here uses C primitives dlopen & dlsym & dlclose....

It's better to use MF "C Functions for Calling COBOL"

which makes the C code to be simpler and more portable.

see KB 25667 which uses MF cobcall & cobcancel & cobfunc

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

Resolution:

The demonstration :

The C code CCobStdout.c

...>Sets en environnent variable

          (setenv primitive)

...>Gets the content of this environment variable

(getenv primitive)

...>Loads a COBOL shared library

(dlopen primitive)

...>Finds the location of a Cobol function

(dlsym primitive)

...>Calls the COBOL function

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

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

...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 Docl.sh which compiles and links C and Cobol code

cob -x  CCobStdout.c

cob -z CobolFunction.cbl

To launch de demonstration, Just launch CCobStdout.

And you should get this STDOUT

*--> Set Environment Variable: CobolStdout

*--> Get Environment Variable: CobolStdout

        CobolStdout :  CobolStdout.log

*--> Load shared library CobolFunction.so

*--> Locate symbol: CobolFunction

*--> Call Cobol Function: CobolFunction

*--> Close and unload of module CobolFunction.so

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

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

*--> BEGIN CobolFunc

*--> 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:

CCobStdout.tar

Old KB# 2355

0 replies

Be the first to reply!