Microsoft KB Archive/48735

Example of Passing a Record from COBOL to a Microsoft C struct

PSS ID Number: Q48735 Article last modified on 09-15-1989

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

Summary: This article describes how to pass a record from a COBOL program to a C program that accesses the record as a struct. This information applies to Microsoft COBOL Version 3.00 and 3.00a for MS-DOS and MS OS/2 and to Microsoft C Version 5.10 for MS-DOS and MS OS/2.

More Information: Some key requirements to correctly pass a COBOL record to C are the following: 1. Compile both the C and COBOL programs with the large memory model. 2. Because COBOL passes all parameters by reference, the C program requires a pointer to a struct as the parameter for the function. 3. All variables passed from COBOL to C require far addresses. This means that the C function should define the parameter as a far pointer to a struct. All of the above requirements result in the following declaration for the C function: function1(struct struct1 far *p1) This says that the C function expects to be passed a far pointer to a struct of type struct1.

Code Example
The following examples demonstrate passing a COBOL record to a C program that uses the record as a struct. The compile line for the C function is as follows: CL -c /AL /Awlf structc.c The compile line for the COBOL main program is as follows: COBOL STRUCT.COB; The link line for the two programs is as follows: link struct minitc structc,,,lcobol.lib+llibcer.lib /noe /nod; The following is the C function code for structc.c: /* C program structc.c / #include  struct struct1 { unsigned char var1[8]; unsigned char var2[12]; unsigned int var3[5]; }; function1(struct struct1 far p1) { int a; for (a=0; a<5; a++) printf(“%i”,p1->var3[a]); for (a=0; a<8; a++) printf(“%c”, p1->var1[a]); printf(“”); for (a=0; a<12; a++) printf(“%c”, p1->var2[a]); printf(“”); } The following is COBOL program STRUCT.COB: $set VSC2 RTNCODE-SIZE(4) LITLINK MODEL “LARGE” * COBOL program STRUCT.COB IDENTIFICATION DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 REC-1. 11 VAR1 PIC X(8) VALUE “HELLO”. 11 VAR2 PIC X(12) VALUE “W O R LD”. 11 VARC2 PIC 9(04) COMP-5 VALUE 2. 11 VARC3 PIC 9(04) COMP-5 VALUE 3. 11 VARC4 PIC 9(04) COMP-5 VALUE 4. 11 VARC5 PIC 9(04) COMP-5 VALUE 5. 11 VARC1 PIC 9(04) COMP-5 VALUE 1. PROCEDURE DIVISION. 1000-MAIN. CALL “C_FUNCTION1” USING BY REFERENCE REC-1. DISPLAY “CBL VARC–>” VARC1. DISPLAY “CBL VARC–>” VARC2. DISPLAY “CBL VARC–>” VARC3. DISPLAY “CBL VARC–>” VARC4. DISPLAY “CBL VARC–>” VARC5. DISPLAY “CBL VAR1–>” VAR1. DISPLAY “CBL VAR2–>” VAR2. STOP RUN.

Copyright Microsoft Corporation 1989.