Microsoft KB Archive/49964

Example of Passing a C Struct to COBOL 3.00

PSS ID Number: Q49964 Article last modified on 05-30-1990

3.00 3.00a | 3.00 3.00a MS-DOS | OS/2

Summary: The code example below shows how to pass a C structure (struct) to a COBOL procedure. This information applies to Microsoft COBOL version 3.00 and 3.00a, to Microsoft C version 5.10, and to Microsoft QuickC version 2.01.

More Information: COBOL routines are cdecl; therefore, the name of the file must be prefixed with an underscore. The PROGRAM-ID name overrides the source code name, and because you get an “illegal character” syntax error if you use an underscore to begin the PROGRAM-ID name, do not use a PROGRAM-ID. Compile the COBOL source code as follows: COBOL _cobolsource; Compile the C source code as follows: cl -c -AL csource.c Link the two .OBJ files created as follows: LINK csource.obj+_cobolsource.obj;

COBOL Source
$SET VSC2 $SET RTNCODE-SIZE(2) $SET LITLINK IDENTIFICATION DIVISION. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01  Integer              pic 9(4) comp-5. 01  Long                 pic 9(8) comp-5. LINKAGE SECTION. 01 CobRec. 03   COBInt         pic s9(4) comp-5 value zero. 03   COBLong        pic s9(8) comp-5 value zero. 03   COBString      pic x(21) value spaces. PROCEDURE DIVISION using CobRec. 000-MAIN. MOVE COBInt to Integer. MOVE COBLong to Long. DISPLAY &quot;Integer from C: &quot; Integer. DISPLAY &quot;Long integer from C: &quot; Long. DISPLAY &quot;String from C: &quot; COBString. EXIT PROGRAM.

C Source Code

 * 1) include  #include  #include  struct CobRec /* defines data type CobRec / { unsigned int varInt; / integer variable / unsigned long varLong; / long int / char szString[21]; / string variable / }; / COBOL routines are cdecl; this means the name must be prefixed with ’_’. Alternatively, you can manually reverse the parameters / extern far cdecl COBPROC(struct CobRec cPtr); main { struct CobRec cPtr; / declare pointer to struct / / get memory to hold struct / cPtr = (struct CobRec ) _fmalloc(sizeof(struct CobRec)); /* NOTE: COBOL will be the main program unless BP is non-zero. BP is zero until some local variables are allocated and used. In this example we do use some local variables; therefore, this is taken care of already. / printf(“Positive Integers and String”); cPtr->varInt = 32767; / refer to member of struct and/ cPtr->varLong = 60000; / assign values. / strcpy(cPtr->szString,“This is a test string”); COBPROC ( cPtr); / Call to COBOL procedure */ printf(“”); printf(“Negative Integers and String”); cPtr->varInt = -32765; cPtr->varLong = -987654; strcpy(cPtr->szString,“Here’s another string”); COBPROC ( cPtr); }

Copyright Microsoft Corporation 1990.