Calling Non-COBOL Subprograms [ Micro Focus COBOL for UNIX COBOL User Guide ] MPE/iX 5.0 Documentation
Micro Focus COBOL for UNIX COBOL User Guide
Calling Non-COBOL Subprograms
You can access non-COBOL subprograms using the standard COBOL CALL
...USING statement. The address of each USING parameter is passed to the
argument in the non-COBOL subprogram which has the same ordinal position
in the formal parameter declarations. You must thus ensure all formal
parameter declarations are pointers.
If you use the CALL...USING BY VALUE form of the CALL...USING statement,
the USING parameter is not passed BY VALUE if it is larger than four
bytes long. If it is larger than this, it can be passed BY REFERENCE,
but no warning is output to inform you of this. If you specify a numeric
literal with a CALL...USING BY VALUE statement, it is passed BY VALUE as
though it were a four-byte COMP-5 item; that is, it appears in
machine-order as a data item and not as a pointer to that data item. If
you specify a data item with a CALL...USING BY VALUE statement it must be
defined as COMP-5.
If you call a C routine by number, the parameter must be a number in the
range 0 to 127.
The following example shows how C functions can be accessed from a COBOL
program:
$set rtncode-size(4)
working-storage section.
01 str.
03 str-text pic x(10).
03 filler pic x value x"00".
* Null terminate string for C function
01 counter pic 9(8) comp-5 value zero.
procedure division.
call-c section.
call "cfunc" using str, counter
if return-code not = zero
* RETURN-CODE set from return () in C
display "ERROR"
else
display "OK"
end-if
stop run.
------------------------------------------------
cfunc (st, c)
char *st;
int *c;
{
.
.
.
return(0);
}
All of the non-COBOL subprograms which you want to call from COBOL must
be statically linked to the dynamic loader run-time support module, or to
your executable COBOL application, using the cob command. The format of
the cob command you use determines whether you are creating a new RTS, or
a fully linked executable application program with the non-COBOL programs
linked to it.
cob -xe "" cprog.c -o rts
creates a new RTS called "rts" and enables all COBOL programs invoked
with rts to access the C functions linked to rts, as the entry point is
null, and can be supplied at run-time, while:
cob -x cobprog.cbl cprog.c -o cobprog
creates a fully executable COBOL application called "cobprog" and enables
only the specified COBOL program (cobprog.cbl) and any of its called
subprograms, to access the C functions linked to it. See the chapter
COBOL System Interface (cob) in your COBOL System Reference for full
details on the use of the cob command.
When you use the CALL statement from within a COBOL program to access a
non-COBOL module as described above, you must ensure that the COBOL run
environment is not accidentally damaged. This means, you must ensure
that:
* The called module preserves the local COBOL run environment (that
is, the registers) according to "C" calling conventions. Refer to
the documentation supplied with your UNIX system for allocation of
registers over calls, and a definition of which registers should
be preserved, and which can be used as work registers.
* The global COBOL run environment (that is, data areas allocated by
the COBOL system: open file, buffers, environment variables, and
so on) should be destroyed or altered only under the direct
control of the COBOL system.
The CANCEL statement has no effect when it references a non-COBOL
program.
The Micro Focus COBOL system supports functions to enable C programs
to call COBOL programs. Full details on these functions are given later
in this chapter.
Run-time Errors 114 and 115
The Micro Focus COBOL system traps unexpected signals and returns
run-time errors:
114 Attempt to access item beyond bounds of memory
115 unexpected signal
Where C and COBOL modules are linked together, these error messages are
usually due to an error in the C code which causes a hardware interrupt
for such conditions as segmentation violation. If you do receive these
run-time errors, carefully check the C source routines using the system
debugger, paying particular attention to the format and byte ordering of
parameters passed between the COBOL and C modules.
MPE/iX 5.0 Documentation