Compaq COBOL AAQ2G1FTK User Manual

Page 450

Advertising
background image

Interprogram Communication
12.6 Calling Compaq COBOL Programs from Other Languages

Example 12–7 Calling a COBOL Program from C

#include <stdio.h>
#include "cobfunc.h"
extern int calledfromc();

main(int argc, char **argv)
{

char *arg1="arg1_string";
char *arg2="1234";
int arg3 = 16587;
int func_result;
char *arglist[10];

#ifdef __osf__

cob_init(argc, argv, NULL);

#endif

arglist[0] = arg1;
arglist[1] = arg2;
arglist[2] = (char *) &arg3;

func_result = cobfunc ("calledfromc", 3, arglist);

}

Example 12–8 C Include File cobfunc.h

void cobcancel ( /* CANCEL the named COBOL routine */

char *name
);

int cobcall (

/* Call a COBOL program from a C routine */

char *name, /* READ: name of the program */
int argc,

/* READ: how many arguments */

char **argv /* READ: array of pointers to the arguments */
);

int cobfunc (

/* Call a COBOL program from a C routine, then CANCEL it */

char *name, /* name of the program */
int argc,

/* how many arguments */

char **argv /* array of pointers to the arguments */
);

#ifdef __osf__

void cob_init (

/* init the RTL */

int argc,

/* argument count */

char **argv,

/* arguments */

char **envp

/* environment variable pointers */

);

#endif

Note that argv[0] is the first argument to pass and argv[n-1] is the nth. The
maximum number of arguments supported is 254.

For Tru64 UNIX programs, if the main routine is written in C, it must call
cob_init. (See Section 12.1.2, Calling Procedures.) The Compaq COBOL program
must expect its arguments by reference.

12–20 Interprogram Communication

Advertising