COBOL calling C pointer problem?



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Re: COBOL calling C pointer problem?

Postby Robert Sample » Mon Jul 12, 2010 10:58 pm

From section 4.3.1.4 of the ILC LE manual:
3. COBOL turns on the high order bit of the address of the last parameter when it is passed by reference. This can cause problems in the C program if it is using the address (since it will be treated as a negative number). If a C program does need to use the address of the last parameter, one of the following techniques can be used to bypass this problem:

* If the COBOL program is an Enterprise COBOL program, instead of passing the parameter by reference, pass the address of the item by value. For example, use a call statement that looks like this:

CALL "C" using by value address of C-PARM1
by value address of C-PARM2

* If the COBOL program is not Enterprise COBOL, code needs to be added to mask out the high-order bit in the C routine. The sample code below shows how to do this:

#include <stdio.h>
#include <string.h>
void A1CC01BA(char* myString)
{
myString = (char*)((int)myString & 0x7fffffff);
printf("My String: %s \n", myString);
return;
}
Did you include this code in your C program since you have BY REFERENCE in your CALL statement?

Communicating between programs can be difficult in a single language (considering addressing modes, static versus dynamic calls, and so forth). Adding multiple languages to the mix vastly complicates the communication, especially with two languages so fundamentally different as COBOL and C. It can be done, but the mere fact that there is a manual devoted to nothing but interlanguage communication should indicate how tough the process is.

If I were in your shoes, I'd start by passing a simple structure (or even just a character variable) back and forth to make sure I could get that right. I would then add in the RETURNING phrase with an integer (COBOL PIC S9(09) COMP) value. After verifying that this works, I'd put in one piece at a time until I got the desired data structures going back and forth -- putting multiple things in the interface at once makes it difficult to know what isn't working, precisely. And make sure you use LIST,NOOFFSET on your compile options so you know exactly what COBOL is generating for the CALL -- COBOL doesn't always do what you think it will.
Robert Sample
Global moderator
 
Posts: 3719
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: COBOL calling C pointer problem?

Postby blueywilson » Tue Jul 13, 2010 1:47 pm

Thanks Robert,

We have already done as you suggest since you posted your last reply ie written a simple C program which has a number of simple functions such that we can pass known data by the different calling methods and check the results. Which I'm sure you'll be pleased to know has led us to the solution.

One of our variables was incorrectly defined in COBOL for the call to cfunction2. C-Pointer was defined as a PIC XX and the C equivalent being a unsigned int, it should have been defined in COBOL as a PIC 9(9) USAGE IS BINARY and when we defined it as such every thing has sprung into life.

Interestingly this source both the COBOL and C was ported from an iSeries platform where this mis-definition has not caused any problems?

Many thanks.
blueywilson
 
Posts: 11
Joined: Fri Jul 09, 2010 2:02 pm
Has thanked: 0 time
Been thanked: 0 time

Re: COBOL calling C pointer problem?

Postby blueywilson » Tue Jul 13, 2010 1:51 pm

Apologies....in my haste to share the good news the above contains a mistake. It was not the parameter C-pointer but C-STATUS which was mis-defined.

Cheers.
blueywilson
 
Posts: 11
Joined: Fri Jul 09, 2010 2:02 pm
Has thanked: 0 time
Been thanked: 0 time

Re: COBOL calling C pointer problem?

Postby Robert Sample » Tue Jul 13, 2010 3:55 pm

Glad to hear you got it working. One of the difficulties with porting code from platform to platform is the unstated assumptions built into the code. A pointer on the old platform very well may have been two bytes but z/OS uses 4 bytes for its pointers -- which would explain why it worked on the old platform but not the new one.
Robert Sample
Global moderator
 
Posts: 3719
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Previous

Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post