HOME  NEWS  FORUM  DOWNLOAD  LINK
OpenCOBOL - an open-source COBOL compiler
Main Menu
Download
Documentation
Development
Who's Online
23 user(s) are online (15 user(s) are browsing Forum)

Members: 0
Guests: 23

more...
Powered by
SourceForge

Xoops

Creative Commons

OpenCOBOL Forum Index
   OpenCOBOL
     calling cobol para from C
Register To Post

Threaded | Newest First Previous Topic | Next Topic | Bottom
Poster Thread
sajjar
Posted on: 2011/6/1 6:12
Quite a regular
Joined: 2010/6/15
From: Chennai, India
Posts: 44
calling cobol para from C
Hi, I want to call a COBOL paragraph from a C program; I'm making use of PROCEDURE-POINTER and ENTRY stmt to get the para Entry pointer and passing this to a C program where I'm doing function call based on this pointer.

I'm getting "Attempt to reference unallocated memory" error.

Could anyone please help me. I'm using Ubuntu 10.04 OS.

HANDCOND.CBL
      *Compile: cobc -x -o HANDCOND HANDCOND.CBL
       IDENTIFICATION DIVISION.
       PROGRAM-ID. HANDCOND.
       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 HAND-COND-ADDR    USAGE PROCEDURE-POINTER.

       PROCEDURE DIVISION.
       
       000-REGISTER-EXIT-PROC.
          SET HAND-COND-ADDR TO ENTRY '999-EXIT'.
          CALL 'CPROG' USING HAND-COND-ADDR.
 
          IF RETURN-CODE NOT = 0
            DISPLAY 'Error: Could not  call CPROG sub program'
          END-IF.

          STOP RUN.

       999-EXIT-PROC.
          ENTRY '999-EXIT'
          DISPLAY 'This is from 999-EXIT-PROC'

          GOBACK.


CPROG.c
/* 
Compile: gcc -Wall -shared -fpic -Wl,--export-dynamic -o CPROG.so CPROG.c 
*/
#include <stdio.h>
#include <stdlib.h>

/* function pointer */
  typedef void (*pt2Function)(void);

int CPROG (pt2Function funcptr)
{
  printf ("@ CPROG enter\n");
  
  // call the function pointer which is pointing to a cobol para
  (*funcptr)();

  printf ("@ CPROG leaving\n");

  return 0;
}
ctimmer
Posted on: 2011/6/1 14:59
Not too shy to talk
Joined: 2006/10/20
From: Big Lake, Alaska
Posts: 22
Re: calling cobol para from C
CALL 'CPROG' USING HAND-COND-ADDR.

I would try changing this to call by-value.

Curt
btiffin
Posted on: 2011/6/2 2:21
Webmaster
Joined: 2008/6/7
From: CANADA
Posts: 1196
Re: calling cobol para from C
sajjar; Ditto-ing Curt.
See http://www.opencobol.org/modules/newbb/viewtopic.php?topic_id=1320&forum=1#forumpost6681 for an explanation of why.

Cheers,
Brian
sajjar
Posted on: 2011/6/2 3:24
Quite a regular
Joined: 2010/6/15
From: Chennai, India
Posts: 44
Re: calling cobol para from C
That magic word BY VALUE worked magically Thanks for the help.

However, the expected behavior I could not get it. I was expecting after the CALL, the cobol program should stop since the 999-EXIT-PROC para is having GOBACK stmt; but control came back to cobol program and continued till STOP RUN stmt.

Is there anyway I could get my expected behavior ?.

Thanks,
Sajjar.
btiffin
Posted on: 2011/6/2 13:54
Webmaster
Joined: 2008/6/7
From: CANADA
Posts: 1196
Re: calling cobol para from C
Couple of things for that then Sajjar.

Take a look at Gary's programming guide. CALL has an ON EXCEPTION clause. That is what will fire if the CALL itself has trouble. The RETURN-CODE is actually the result of the sub program, when the CALL itself has already 'succeeded'.

Next; GOBACK is, by definition, smart and 'does the right thing' and won't terminate a program run unless it occurs as the last of the call stack when it should return control to the operating system. If you want a subprogram to terminate the run unit as a whole, use STOP RUN. (And for fun, look at EXIT PROGRAM too). Gary's guide does a pretty good job of explaining all of the ins and outs of program flow rules.

From C you can call exit(status) which, by definition, unwinds the return stack and returns control (with the int status as it's result code) to the OS regardless of how deeply nested. That exit(status) is how STOP RUN (after proper COBOL cleanup) actually works as well. You can see the details in the OpenCOBOL sources in libcob/common.c in the function cob_stop_run. This is versus cobtidy which properly cleans up COBOL but does NOT terminate the process.

Cheers,
Brian
sajjar
Posted on: 2011/6/2 15:45
Quite a regular
Joined: 2010/6/15
From: Chennai, India
Posts: 44
Re: calling cobol para from C
Thank you brian for the educative explanation. Now, I understood something on control flow; with this I shall design progs accordingly.

Regards,
Sajjar.
Threaded | Newest First Previous Topic | Next Topic | Top

Register To Post
 
Copyright (C) 2005 The OpenCOBOL Project. All rights reserved.
Powered by Xoops2 | PHP | MySQL | Apache
ocean-net