| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Calling C from RPG

Page history last edited by PBworks 16 years, 11 months ago

Using the ILE C or C++ compiler on the AS/400 it is possible to create a service program from C code. The functions within the C code can then be called from and RPG program.

 

Sample section of C code:

 

    double _EXP16 ConvRate(double rate, short ifreq, short ofreq)

    {

    /* Returns nominal interest rate converted to a different

    * compounding frequency

    *

    * double rate Input nominal interest rate.

    * int ifreq Frequency at which rate is currently stated

    * (12 = monthly, 3 = quarterly, etc.)

    * int ofreq New compounding frequency.

    */

     

    if (fabs(rate)>=0.000005 &&

    ifreq > 0 && ifreq <= 12 &&

    ofreq > 0 && ofreq <= 12

    )

    {

    rate = 1 + rate / (double)ifreq / 100;

    rate = pow(rate, (double)ifreq / ofreq);

    rate = (rate - 1) * ofreq * 100;

    }

    return rate;

    }

     

 

This code can be in the IFS or in a source PF. To compile use something like:

    CRTCMOD MODULE(LANCASP/ENGINE2) SRCSTMF('/lancasp/engine2') OUTPUT(*PRINT) DBGVIEW(*SOURCE)

or

    CRTCPPMOD MODULE(LANCASP/ENGINE2) SRCSTMF('/lancasp/engine2.c') OUTPUT('/lancasp/engine2.log') DBGVIEW(*ALL)

     

    (this is for C++, QCXXN needs to be in the library list)

and then

    CRTSRVPGM SRVPGM(LANCASP/ENGINE2) MODULE(LANCASP/ENGINE2) EXPORT(*ALL)

 

The RPG code to call the example C function is then

 

D ConvRate PR 8F EXTPROC('ConvRate')

D EXRATE 8F VALUE

D IFREQ 5I 0 VALUE

D OFREQ 5I 0 VALUE

*

D NEWRATE S 8F

D pEXRATE S 8F INZ(12.5)

D pIFREQ S 5I 0 INZ(1)

D pOFREQ S 5I 0 INZ(12)

*

C EVAL NEWRATE=ConvRate(pEXRATE: pIFREQ: pOFREQ)

*

C RETURN

 

Note that the call needs to be coded as an expression in order to return the returned value from the function. Also note that the input parameters are passed by value.

Comments (0)

You don't have permission to comment on this page.