sample COBOL calling C with *parm parameters



Help for C/C++ for MVS, OS/390 C/C++, z/OS C/C++ and C/C++ Productivity Tools for OS/390

sample COBOL calling C with *parm parameters

Postby zatlas1 » Wed Sep 12, 2012 7:32 pm

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
zatlas1
 
Posts: 61
Joined: Mon Mar 15, 2010 9:19 am
Has thanked: 0 time
Been thanked: 0 time

Re: sample COBOL calling C with *parm parameters

Postby BillyBoyo » Wed Sep 12, 2012 7:48 pm

I googled with this - ibm calling c from Cobol - and got some likely-looking stuff.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: sample COBOL calling C with *parm parameters

Postby zatlas1 » Wed Sep 12, 2012 8:41 pm

It did not work for me and I don't know why.
ZA
zatlas1
 
Posts: 61
Joined: Mon Mar 15, 2010 9:19 am
Has thanked: 0 time
Been thanked: 0 time

Re: sample COBOL calling C with *parm parameters

Postby dick scherrer » Wed Sep 12, 2012 8:57 pm

Hello,

What happens if you call your c module from another c module? Run it standalone?
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: sample COBOL calling C with *parm parameters

Postby enrico-sorichetti » Wed Sep 12, 2012 9:03 pm

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
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator
 
Posts: 3003
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 164 times

Re: sample COBOL calling C with *parm parameters

Postby enrico-sorichetti » Thu Sep 13, 2012 2:35 am

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
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator
 
Posts: 3003
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 164 times

Re: sample COBOL calling C with *parm parameters

Postby Robert Sample » Thu Sep 13, 2012 5:09 am

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.
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: sample COBOL calling C with *parm parameters

Postby zatlas1 » Thu Sep 13, 2012 7:57 am

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
zatlas1
 
Posts: 61
Joined: Mon Mar 15, 2010 9:19 am
Has thanked: 0 time
Been thanked: 0 time

Re: sample COBOL calling C with *parm parameters

Postby zatlas1 » Thu Sep 13, 2012 8:15 am

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
zatlas1
 
Posts: 61
Joined: Mon Mar 15, 2010 9:19 am
Has thanked: 0 time
Been thanked: 0 time


Return to C, C++

 


  • Related topics
    Replies
    Views
    Last post