I have the following c code which calls a cobol program:
#include <stdio.h>
#include "libcob.h"
//#pragma linkage (verkoop, COBOL)
extern void VERKOOP(char *productid, char *aantal, char*resultaat);
main(int argc, char *argv[])
{
int return_status;
COB_RTD = cob_get_rtd();
char *productid = "20 ";
char *aantal = "000020";
char resultaat[30];
cob_init(rtd, 0, NULL);
printf("hallo");//prints
VERKOOP(productid, aantal, resultaat);
printf("hallo");//doesn't print
printf("resultaat:%s", resultaat);// doesn't print
cob_stop_run (rtd, return_status);
}
I'm using the printf to see if resultaat has been assigned correctly. However, both of the lines after VERKOOP don't print for some reason.
This is the COBOL code of VERKOOP(he does fill in LS-RESULTAAT correctly here, I tried it with DISPLAY and this part works):
*************************************************************
* VERKOOP
*************************************************************
IDENTIFICATION DIVISION.
PROGRAM-ID. VERKOOP.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT PRODUCTEN ASSIGN TO "BESTANDEN/LIJSTPRODUCTEN"
ACCESS MODE IS RANDOM
ORGANIZATION IS INDEXED
RECORD KEY IS PRODUCTID
FILE STATUS IS WS-FILE-STATUS.
DATA DIVISION.
FILE SECTION.
FD PRODUCTEN BLOCK CONTAINS 10 RECORDS.
01 PRODUCT.
02 PRODUCTID PIC X(6).
02 LEVERANCIERID PIC X(6).
02 AANTAL PIC 9(6).
WORKING-STORAGE SECTION.
77 FOUT PIC X.
88 PRODUCT-NIET-GEVONDEN VALUE 1.
77 WS-PRODUCTID PIC X(6).
77 WS-AANTAL PIC 9(6).
77 WS-FILE-STATUS PIC XX.
77 WS-RESULTAAT PIC X(30).
LINKAGE SECTION.
01 LS-PRODUCTID PIC X(6).
01 LS-AANTAL PIC 9(6).
01 LS-RESULTAAT PIC X(30).
PROCEDURE DIVISION USING LS-PRODUCTID, LS-AANTAL, LS-RESULTAAT.
MAIN.
PERFORM INITIALISEER
PERFORM LEES-PRODUCT-IN
PERFORM LEES-BESTAND
PERFORM SLUIT-BESTAND
STOP RUN.
INITIALISEER.
OPEN I-O PRODUCTEN.
* DISPLAY WS-FILE-STATUS..
LEES-PRODUCT-IN.
MOVE LS-PRODUCTID TO WS-PRODUCTID
MOVE LS-AANTAL TO WS-AANTAL
MOVE 'OK' TO WS-RESULTAAT
* DISPLAY WS-RESULTAAT
MOVE WS-RESULTAAT TO LS-RESULTAAT.
* DISPLAY "GEEF PRODUCTID OP: "
* ACCEPT WS-PRODUCTID
* DISPLAY "GEEF AANTAL OP: "
* ACCEPT WS-AANTAL.
LEES-BESTAND.
* DISPLAY "LEES-BESTAND"
MOVE WS-PRODUCTID TO PRODUCTID
* DISPLAY PRODUCTID
* DISPLAY WS-FILE-STATUS
READ PRODUCTEN INVALID KEY SET PRODUCT-NIET-GEVONDEN TO TRUE
END-READ
IF PRODUCT-NIET-GEVONDEN PERFORM FOUTJE
ELSE
* MOVE WS-PRODUCTID TO PRODUCTID
SUBTRACT WS-AANTAL FROM AANTAL
PERFORM UPDATE-PRODUCT
END-IF.
UPDATE-PRODUCT.
REWRITE PRODUCT INVALID KEY PERFORM FOUTJE.
SLUIT-BESTAND.
* DISPLAY "SLUIT-BESTAND"
CLOSE PRODUCTEN.
FOUTJE.
DISPLAY "ER IS EEN FOUT OPGETREDEN"
DISPLAY WS-FILE-STATUS
STOP RUN.
UPDATE: I tried removing both "STOP RUN's", however now for some reason he substracts 40 instead of 20 and prints "ER IS EEN FOUT OPGETREDEN". So he is running the COBOL Program twice for some reason.
UPDATE: After replacing STOP RUN by GOBACK it works perfectly
Your STOP RUN is returning here, cob_stop_run (rtd, return_status), so your prior code after the invocation of the COBOL program does not run.
If EXIT PROGRAM is in a "main" program (at least in the case of the pragma-usage it seems that is what you have) then it is treated the same as STOP RUN.
GOBACK is returning control to where you expect it to.
If you use the COBOL-IT API, as has already been suggested, then perhaps the EXIT PROGRAM will behave differently. Perhaps not.
You are using an undocumented way to call a COBOL-IT program. Exactly how it behaves is not known, and cannot be known, to someone without COBOL-IT and the same operating system as you have, and the patience to do something in a non-obvious way.
If things are suggested which you then ignore, it is difficult to keep answering your questions.
Again, you have an Assignment. The Assignment expects you to use the API. You should use the API and get your programs working. If you have time later, you can look at the pragma-usage to effect the interoperability.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.