Calling a C Function with arguments from LANSA

Date: 29 November 2004
Product/Release: LANSA for iSeries
Abstract: Calling a C Function with arguments from LANSA
Submitted By: LANSA Technical Support
Last Review: November 2010


To call a C function (3GL) with arguments using LANSA.


The solution is to specify the parameters in the LANSA function. The important part is to ensure the called 3GL program has been correctly setup to receive them.

This is a simple example of calling a C program from a LANSA RDML function. The C Program that is called is named MYCPGM.

The arguments it expects are :

  1. a char(10) and
  2. packed(7,0) number as arguments.

The C program listed below swaps the first 2 characters of the char(10) argument and adds 3 to the second argument.

This demonstrates information being passed into the program and returned by it.

/* Standard includes for all platforms */

#include <string.h>
#include <decimal.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <time.h>
#include <math.h>
#include <float.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <ctype.h>
#include <iconv.h>
#include <errno.h>
#include <dirent.h>

/*=================== M A I N L I N E - iSeries ==================*/

void main(int argc, char * argv[])
char * pArgument1 = argv[1]; /* Argument 1 is an alpha 10 */
decimal(7,0) * pArgument2 = (decimal(7,0) *) argv[2]; /* Argument 2 is a packed 7,0 */
char chTemp;

/* Swap the first 2 characters of argument 1 */
chTemp = pArgument1[0];
pArgument1[0] = pArgument1[1];
pArgument1[1] = chTemp;

/* Increment argument 2 by 3 */
*(pArgument2) += 3;
/* Finished */

Depending on the location of the file the function can be compiled in the following ways:

If on IFS:

CRTCMOD MODULE(<some library>/MYCPGM) SRCSTMF('< location of the MYPGM.C file on the IFS>') OUTPUT(*PRINT) OPTIMIZE(40) INLINE(*ON *AUTO) TGTRLS(V4R5M0)

If using source file:

CRTCMOD MODULE(<some library>/MYCPGM) SRCFILE(<some library>/<source file>) OUTPUT(*PRINT) OPTIMIZE(40) INLINE(*ON *AUTO) TGTRLS(V4R5M0)

and then


The following RDML function can be used to test the above:

DEFINE #arg1 *char 10
DEFINE #arg2 *dec 7 decimals(0)
REQUEST (#Arg1 #arg2)
CALL PGM(MYCPGM) PARM(#arg1 #arg2) NUM_LEN(*Defined)