Calling a COBOL programm via C - IBM AS400

This is a discussion on Calling a COBOL programm via C - IBM AS400 ; Hello, I'm programming on a v5r3 system and have to call a COBOL program from within C. The problem is that the parameter (LINKAGE SECTION) of the called programm is defined as "COPY DDS-ALL-FORMATS OF FOOBAR". So my question is ...

+ Reply to Thread
Results 1 to 15 of 15

Thread: Calling a COBOL programm via C

  1. Calling a COBOL programm via C

    Hello,

    I'm programming on a v5r3 system and have to call a COBOL program from
    within C. The problem is that the parameter (LINKAGE SECTION) of the
    called programm is defined as "COPY DDS-ALL-FORMATS OF FOOBAR". So my
    question is if this is possible at all and if so, how can this be done?

    Best regards, Chris Mager.


  2. Re: Calling a COBOL programm via C

    I'm a COBOL guy, not a C guy; I'm afraid I don't understand what your
    difficulty is though. Can't C use copybooks?


  3. Re: Calling a COBOL programm via C

    Hi,

    thanks for your reply!

    walker.l2 schrieb:
    > I'm a COBOL guy, not a C guy; I'm afraid I don't understand what your
    > difficulty is though. Can't C use copybooks?


    Well, to call other programms in C you're using the system() function.
    This one takes a single parameter where you put the command as is, for
    example:
    system( "CRTDTAARA ....... ");

    But if the called program takes "structured parameter" (like copy
    dds-all-formats) system() won't work so I actually have two problems:

    - How to get the data definition of db-table FOOBAR into C so that i
    get a variable with this structure, and (the main problem)
    - how to give this to the called cobol program as a parameter

    At the moment I'm searching in the IBM iSeries Information Center for
    v5r3, but haven't found some suitable information about that.


  4. Re: Calling a COBOL programm via C

    christian.mager@gmail.com wrote:
    > Hi,
    >
    > thanks for your reply!
    >
    > walker.l2 schrieb:
    >> I'm a COBOL guy, not a C guy; I'm afraid I don't understand what your
    >> difficulty is though. Can't C use copybooks?

    >
    > Well, to call other programms in C you're using the system() function.
    > This one takes a single parameter where you put the command as is, for
    > example:
    > system( "CRTDTAARA ....... ");
    >
    > But if the called program takes "structured parameter" (like copy
    > dds-all-formats) system() won't work so I actually have two problems:
    >
    > - How to get the data definition of db-table FOOBAR into C so that i
    > get a variable with this structure, and (the main problem)
    > - how to give this to the called cobol program as a parameter
    >
    > At the moment I'm searching in the IBM iSeries Information Center for
    > v5r3, but haven't found some suitable information about that.
    >


    See Chapt 18 of the ILE C/C++ Programmer's Guide:
    http://publib.boulder.ibm.com/infoce...s/sc092712.pdf
    For external file data definitions, it looks like #pragma mapinc() applies.

    See Chapt 25 for info about inter-program linkages. If calling a
    separate program (*PGM), it looks like #pragma linkage with the OS
    option may apply.

    --
    Karl Hanson

  5. Re: Calling a COBOL programm via C

    Karl Hanson schrieb:

    > See Chapt 18 of the ILE C/C++ Programmer's Guide:
    > http://publib.boulder.ibm.com/infoce...s/sc092712.pdf
    > For external file data definitions, it looks like #pragma mapinc() applies.
    >
    > See Chapt 25 for info about inter-program linkages. If calling a
    > separate program (*PGM), it looks like #pragma linkage with the OS
    > option may apply.


    hm, yes that seems to be the right information i was searching. I'll
    try it and in some days i'll give a statement if it worked or if i have
    further questions.

    Thanks a lot! Chris Mager.


  6. Re: Calling a COBOL programm via C

    christian.mager@gmail.com wrote:
    > Hi,
    >
    > thanks for your reply!
    >
    > walker.l2 schrieb:
    >> I'm a COBOL guy, not a C guy; I'm afraid I don't understand what your
    >> difficulty is though. Can't C use copybooks?

    >
    > Well, to call other programms in C you're using the system() function.
    > This one takes a single parameter where you put the command as is, for
    > example:
    > system( "CRTDTAARA ....... ");


    i am no cobol programmer but in C you can also use fork() and exec*().
    they can seem difficult, but write a tiny proc that says "Hello World
    From the Parent" and "Hello World From the Child" to start. build upon
    that,
    hth,
    peter

  7. Re: Calling a COBOL programm via C

    Here's a working example I have:

    #pragma linkage (QCMDEXC,OS,nowiden)
    void QCMDEXC(char *, decimal(15,5));

    Prototype is not necessary, but sure helps C folks understand this
    better. Once declared like this, you simply call the program like any
    other function:

    decimal(15,5) len;
    char cmd[100];
    strcpy(cmd,"DSPJOB OUTPUT(*PRINT)");
    len = strlen(cmd);
    QCMDEXC(cmd,len);

    As for pulling in file's data structure, you can do it with mapinc
    pragma, or even better, GENCSRC CL command can generate all file
    includes you need.

    system() function is another option, but it won't let you pass non-char
    parms easily.

    HTH

    Elvis

    Christian Mager wrote:
    > Karl Hanson schrieb:
    >
    > > See Chapt 18 of the ILE C/C++ Programmer's Guide:
    > > http://publib.boulder.ibm.com/infoce...s/sc092712.pdf
    > > For external file data definitions, it looks like #pragma mapinc() applies.
    > >
    > > See Chapt 25 for info about inter-program linkages. If calling a
    > > separate program (*PGM), it looks like #pragma linkage with the OS
    > > option may apply.

    >
    > hm, yes that seems to be the right information i was searching. I'll
    > try it and in some days i'll give a statement if it worked or if i have
    > further questions.
    >
    > Thanks a lot! Chris Mager.



  8. Re: Calling a COBOL programm via C

    Why won't pragma mapinc followed by an include work for you? Are you doing
    something unsual? You are in ILE I am sure.

    i.e. (from the manual)
    #pragma mapinc("tempname","EXAMPLE/TEST(FMT)","input","d",,"")
    #include "tempname"



    wrote in message
    news:1165491616.690686.44310@f1g2000cwa.googlegrou ps.com...
    > Hi,
    >
    > thanks for your reply!
    >
    > walker.l2 schrieb:
    >> I'm a COBOL guy, not a C guy; I'm afraid I don't understand what your
    >> difficulty is though. Can't C use copybooks?

    >
    > Well, to call other programms in C you're using the system() function.
    > This one takes a single parameter where you put the command as is, for
    > example:
    > system( "CRTDTAARA ....... ");
    >
    > But if the called program takes "structured parameter" (like copy
    > dds-all-formats) system() won't work so I actually have two problems:
    >
    > - How to get the data definition of db-table FOOBAR into C so that i
    > get a variable with this structure, and (the main problem)
    > - how to give this to the called cobol program as a parameter
    >
    > At the moment I'm searching in the IBM iSeries Information Center for
    > v5r3, but haven't found some suitable information about that.
    >




  9. Re: Calling a COBOL programm via C

    Hi,

    thanks for all your replies. Karls's tip usig #pragma linkage and
    mapinc works fine, I just have a problem now that the called program
    ends in a MCH1202 error. I have to find out why that happens, but
    currently I don't really have time for that.

    P. Raulerson schrieb:
    > Why won't pragma mapinc followed by an include work for you?


    Well, I didn't know about that. I haven't really programed C on an
    as400 (but on other systems/OSs of course) before so I still have to
    find out some things.

    Thx, Chris.


  10. Re: Calling a COBOL programm via C

    ok, I had some tries now. What I forgot to tell is that most of the
    fields of the table can have NULL-values. My Code looks approximately
    like this:

    #pragma mapinc("evlogfmt","LIB/FILE(FMT)","input nullflds","_P d")
    #include "evlogfmt"

    and

    CALLED_PGM( FIL_FILE_FMT_i_t * );
    #pragma linkage( CALLED_PGM, OS, nowiden )

    that gives me the two structs LIB_FILE_FMT_i_t and LIB_FILE_FMT_nmap_t.
    In the *_nmap_t struct I set the null-capable fields to '1' but now I
    have no clue what to do with this structure? I think that would solve
    my MCH1202-problem.

    Thx, Chris.is.


  11. Re: Calling a COBOL programm via C

    Christian Mager wrote:
    > ok, I had some tries now. What I forgot to tell is that most of the
    > fields of the table can have NULL-values. My Code looks approximately
    > like this:
    >
    > #pragma mapinc("evlogfmt","LIB/FILE(FMT)","input nullflds","_P d")
    > #include "evlogfmt"
    >
    > and
    >
    > CALLED_PGM( FIL_FILE_FMT_i_t * );
    > #pragma linkage( CALLED_PGM, OS, nowiden )
    >
    > that gives me the two structs LIB_FILE_FMT_i_t and LIB_FILE_FMT_nmap_t.
    > In the *_nmap_t struct I set the null-capable fields to '1' but now I
    > have no clue what to do with this structure? I think that would solve
    > my MCH1202-problem.
    >


    You should be able to use a debugger to see what data is being passed in
    from the C program. You didn't say whether the COBOL program is ILE or
    OPM, but if ILE you could either use the green screen (STRDBG) or the
    System (Graphical) Debugger (I prefer the latter - here's a link I found
    via Google
    http://www.metromidrange.org/iSeries...20Debugger.ppt

    Also after getting the MCH1202, you could see low level message details
    (F1+F9 on a green screen) to get the "To program" and statement within
    it that incurs the MCH1202. My guess would be the COBOl program is
    expecting one or more packed decimal and/or zoned decimal fields in the
    structure(s), and the C program is not passing valid packed/zoned data
    in the field(s).

    --
    Karl Hanson

  12. Re: Calling a COBOL programm via C

    Hi,

    > You should be able to use a debugger to see what data is being passed in
    > from the C program. You didn't say whether the COBOL program is ILE or
    > OPM, but if ILE you could either use the green screen (STRDBG) or the
    > System (Graphical) Debugger (I prefer the latter - here's a link I found
    > via Google
    > http://www.metromidrange.org/iSeries...20Debugger.ppt


    yes sorry, the C and COBOL programs both are ILE. I have debugged
    (strdbg) the called COBOL program of course, it aborts at an
    OCCURS-State saying "decimal data error". I think that should have
    something to do with the nullflds. The C/C++ Programmer's guide doesn't
    really say something about dealing with nullflds, or giving an example
    for that - or i just haven't found it yet, maybe.

    So do you think the code lines I've posted are correct?

    Bye, Chris.


  13. Re: Calling a COBOL programm via C

    Christian Mager wrote:
    > Hi,
    >
    >> You should be able to use a debugger to see what data is being passed in
    >> from the C program. You didn't say whether the COBOL program is ILE or
    >> OPM, but if ILE you could either use the green screen (STRDBG) or the
    >> System (Graphical) Debugger (I prefer the latter - here's a link I found
    >> via Google
    >> http://www.metromidrange.org/iSeries...20Debugger.ppt

    >
    > yes sorry, the C and COBOL programs both are ILE. I have debugged
    > (strdbg) the called COBOL program of course, it aborts at an
    > OCCURS-State saying "decimal data error". I think that should have
    > something to do with the nullflds. The C/C++ Programmer's guide doesn't
    > really say something about dealing with nullflds, or giving an example
    > for that - or i just haven't found it yet, maybe.
    >
    > So do you think the code lines I've posted are correct?
    >


    The code you posted seems reasonable from a C standpoint, but I am not
    familiar with COBOL linkages (eg LINKAGE SECTION). If the COBOL program
    needs to know which (if any) fields in the format are NULL, you are
    probably right that the info in the nullflds structure (or equivalent)
    somehow needs to be passed to the COBOL program. Would a compiler
    output listing of the COBOL program provide any hints? ie would the
    COPY DDS-ALL-FORMATS you mentioned be expanded to show all parameters
    expected by the COBOL program?

    --
    Karl Hanson

  14. Re: Calling a COBOL programm via C

    If your fields allow NULLs, you'll need to set the null map accordingly
    in order for _Rwrite etc to work. One byte char value of '0' means
    there is valid data and '1' means it's a NULL value.
    Here is a simple example:

    /* open a file */
    _RFILE * p = _Ropen(......);
    .... set your record's values ....
    /* tell DB2 FIELD3 contains a valid value (not NULL) */
    p->in_null_map[offsetof(LIB_FILE_FMT_nmap_t,FIELD3)] = '0';
    /* write the record */
    _Rwrite(p,....);
    /* close the file */
    _Rclose(p);

    HTH

    Elvis


    Karl Hanson wrote:
    > Christian Mager wrote:
    > > Hi,
    > >
    > >> You should be able to use a debugger to see what data is being passed in
    > >> from the C program. You didn't say whether the COBOL program is ILE or
    > >> OPM, but if ILE you could either use the green screen (STRDBG) or the
    > >> System (Graphical) Debugger (I prefer the latter - here's a link I found
    > >> via Google
    > >> http://www.metromidrange.org/iSeries...20Debugger.ppt

    > >
    > > yes sorry, the C and COBOL programs both are ILE. I have debugged
    > > (strdbg) the called COBOL program of course, it aborts at an
    > > OCCURS-State saying "decimal data error". I think that should have
    > > something to do with the nullflds. The C/C++ Programmer's guide doesn't
    > > really say something about dealing with nullflds, or giving an example
    > > for that - or i just haven't found it yet, maybe.
    > >
    > > So do you think the code lines I've posted are correct?
    > >

    >
    > The code you posted seems reasonable from a C standpoint, but I am not
    > familiar with COBOL linkages (eg LINKAGE SECTION). If the COBOL program
    > needs to know which (if any) fields in the format are NULL, you are
    > probably right that the info in the nullflds structure (or equivalent)
    > somehow needs to be passed to the COBOL program. Would a compiler
    > output listing of the COBOL program provide any hints? ie would the
    > COPY DDS-ALL-FORMATS you mentioned be expanded to show all parameters
    > expected by the COBOL program?
    >
    > --
    > Karl Hanson



  15. Re: Calling a COBOL programm via C

    Hello,

    first of all: thanks to everybody helping me solving my problem.

    Everything works now. As it turned out I didn't have to use nullmap at
    the #pragma mapinc directive, because the called program does checking
    for null values itself. If it wouldn't do this, you would have to give
    the nullmap as a second parameter to the called program - of course,
    the called program must expect this parameter and use it when writing
    the data to a file or whatever.

    Bye, Chris Mager.


+ Reply to Thread