Page 1 of 1

sample COBOL calling C with *parm parameters

PostPosted: Wed Sep 12, 2012 7:32 pm
by zatlas1
Hello all
I am struggling with calling C from COBOL. Basically, I need a working sample COBOL calling C with *parm parameters. I was trying to call using BY REFERENCE, I tried to call USING BY REFERENCE to a pointer and it is clear that it does not work. Let me please show you my latest attempt (bear in mind that it is only a test attempt, not the real program):
Here is the COBOL
000001                identification division.
000002                Program-ID. cobapi.
000003                ENVIRONMENT DIVISION.
000004                data division.
000005                working-storage section.
000006               * int TESTAPI(const char *subject,
000007               *     int *ovector, int stringcount,
000008               *     const char ***listptr)
000009
000010                01  subject.                                                               
000011                    05 FILLER PIC X(5) VALUE 'ABCDE'.                                     
000012                    05 FILLER PIC X    VALUE X'00'.                                       
000013                01  OVECTOR  PIC S9(8) VALUE 96.                                           
000014                01  STRINGCOUNT  PIC S9(8) VALUE 396.                                     
000015                01  LISTPTR      USAGE POINTER VALUE NULL.                                 
                                                                                                 
000016                01  RESULT       PIC S9(8) VALUE 0.                                       
000017                LINKAGE SECTION.
000018                PROCEDURE DIVISION.
000019                    CALL 'TESTAPI' USING by content                                       
000020                                 subject ,                                                 
000021                                 OVECTOR ,                                                 
000022                                 STRINGCOUNT ,                                             
000023                                 LISTPTR                                                   
000024                    RETURNING RESULT.                                                     
000025                    DISPLAY "RESULT " RESULT.                                             
000026                       GOBACK.

and here is the C:
#include "zatoolib.h"                                                                             
int TESTAPI(const char *subject, int *ovector, int stringcount,                                   
    const char ***listptr)                                                                       
{                                                                                                 
    printf ("SUBJECT %s\n", *subject);                                                           
    printf ("OVECTOR %d\n", *ovector);                                                           
    /*intf ("STRINGCOUNT %d\n", stringcount);*/                                                   
    /*printf ("LISTPTR %s\n", ***listptr); */                                                     
    return 100;                                                                                   
}                                                                                                 

and the results on the mainframe:
SUBJECT
OVECTOR 293264496

RESULT 0000000{

Obviously, not what I expected.
The COBOL compiler is IBM Enterprise COBOL for z/OS 3.3.1
The C is 15647A01 V2 R10 OS/390 C

Any suggestions what am I doing wrong

I do not use the #pragma linkage... this would be my next attempt tonight.
Thanks
ZA

Re: sample COBOL calling C with *parm parameters

PostPosted: Wed Sep 12, 2012 7:48 pm
by BillyBoyo
I googled with this - ibm calling c from Cobol - and got some likely-looking stuff.

Re: sample COBOL calling C with *parm parameters

PostPosted: Wed Sep 12, 2012 8:41 pm
by zatlas1
It did not work for me and I don't know why.
ZA

Re: sample COBOL calling C with *parm parameters

PostPosted: Wed Sep 12, 2012 8:57 pm
by dick scherrer
Hello,

What happens if you call your c module from another c module? Run it standalone?

Re: sample COBOL calling C with *parm parameters

PostPosted: Wed Sep 12, 2012 9:03 pm
by enrico-sorichetti
I don't know why.

no wonder...

You simply did not read about the correspondence between C and COBOL datatypes
for example here :
http://publib.boulder.ibm.com/infocente ... clcccd.htm
and here :
http://publib.boulder.ibm.com/infocente ... clccce.htm

Re: sample COBOL calling C with *parm parameters

PostPosted: Thu Sep 13, 2012 2:35 am
by enrico-sorichetti
follow on ...
I strongly recommend You to meditate on

http://publibz.boulder.ibm.com/cgi-bin/ ... 0602121454

chapter 4 for COBOL
chapter 8 for PL/I

Re: sample COBOL calling C with *parm parameters

PostPosted: Thu Sep 13, 2012 5:09 am
by Robert Sample
Any suggestions what am I doing wrong
1. int in C is NOT PIC S9(8) in COBOL -- you're missing something
2. The manual, as enrico indicates, will give you a lot of help
3. It's not clear whether some of the errors in your code are deliberate mistakes or not.

Re: sample COBOL calling C with *parm parameters

PostPosted: Thu Sep 13, 2012 7:57 am
by zatlas1
The mistake of not declaring the integers as S9(8) BINARY, which was a typo, threw me off. The examples do not cover how to pass a character string without the #pragma, but I will find the answer.
Thanks to all who answered...
This code is meant to teach me how to pass parameters between COBOL and C for the COBOL API that I am writing for the PCRE port to z/OS (which is up and running in pure C environment). Version V0.0b was already published for limited audience, mainly to test the installation procedures.
Any body who is interested can PM me and I will distribute it to you.
I hope to finish the COBOL API soon and come with V0.3b
ZA

Re: sample COBOL calling C with *parm parameters

PostPosted: Thu Sep 13, 2012 8:15 am
by zatlas1
OK,

#include "zatoolib.h"                                           
int TESTAPI(const char *subject, int *ovector, int stringcount, 
    const char ***listptr)                                       
{                                                               
    printf ("SUBJECT %s\n", subject);                           
    printf ("OVECTOR %d\n", *ovector);                           
    printf ("STRINGCOUNT %d\n", stringcount);                   
    /*printf ("LISTPTR %s\n", ***listptr); */                   
    return 100;                                                 
}                                                               

and

       identification division.
       Program-ID. cobapi.
       ENVIRONMENT DIVISION.
       data division.
       working-storage section.
      * int TESTAPI(const char *subject,
      *     int *ovector, int stringcount,
      *     const char ***listptr)

       01  subject.                                 
           05 FILLER PIC X(5) VALUE 'ABCDE'.       
           05 FILLER PIC X    VALUE X'00'.         
       01  OVECTOR  PIC S9(8) COMP VALUE 96.       
       01  STRINGCOUNT  PIC S9(8) COMP VALUE 396.   
       01  LISTPTR      USAGE POINTER VALUE NULL.   
                                                   
       01  RESULT       PIC S9(8) COMP VALUE 0.     
       01  LIST1        USAGE POINTER VALUE NULL.   
                                                   
       01  LIST2        USAGE POINTER VALUE NULL.   
                                                   
       LINKAGE SECTION.
       PROCEDURE DIVISION.
           set list1 to  ADDRESS OF   subject       
           set list1 to  ADDRESS OF   OVECTOR       
           CALL 'TESTAPI' USING                     
           by content   subject ,                   
           by reference OVECTOR ,                   
      *                 list1
      *                 list2
           by value    STRINGCOUNT ,               
                        LISTPTR                     
           RETURNING RESULT.                       
           DISPLAY "RESULT " RESULT.               
              GOBACK.

work fine. The only issue is how to address something like the
const char ***listptr


Thanks
ZA