Troubleshooting
Problem
This document contains an example of passing parameters from ILE RPG to an ILE C Program.
Resolving The Problem
According to C language conventions, function main() is a program's entry point. Parameters passed into main() must conform to the standard declaration:
main(int argc, char *argv[])
argc is a 4-byte binary integer, and argv is an array of strings (pointers to type char).
This parameter-passing convention is not directly compatible with ILE RPG, and some parameter manipulation is required when calling a C main program from RPG.
The following sample code illustrates a simple case of such parameter passing. ILE RPG program RPG_TO_C01 sends two string parameters to ILE C program C_FM_RPG01, which concatenates them and returns the result. To run this example, compile the two code samples with the CRTBNDRPG and CRTBNDC commands, respectively. Then, call the RPG program from the command line with two string parameters. For example, on the OS/400 command line type the following:
CALL PGM(RPG_TO_C01) PARM('John' 'Smith')
Press the Enter key.
ILE RPG Program RPG_TO_C01
*
* Define lengths of incoming parameters.
*
D parm1 S 10A
D parm2 S 10A
*
* Define length of variables to send/receive parameters with a C program.
* Note that each variable must be one byte longer than the corresponding
* parameter coming into the RPG program, to allow space for a X'00'
* terminator which C requires.
* The variables are based on the pointers declared after them.
*
D var1 S 11A based(ptr1)
D ptr1 S *
D var2 S 11A based(ptr2)
D ptr2 S *
D var3 S 21A based(ptr3)
D ptr3 S *
*
* Incoming parameter list.
*
C *entry plist
C parm parm1
C parm parm2
*
* Allocate heap storage for the intermediate variables previously defined.
*
C eval ptr1 = %alloc(%size(var1))
C eval ptr2 = %alloc(%size(var2))
C eval ptr3 = %alloc(%size(var3))
*
* Assign incoming parameter values to variables.
*
C eval var1 = parm1
C eval var2 = parm2
*
* Place a X'00' terminator (end of string in C) right before the beginning
* of trailing blanks in the parameter strings.
*
C eval %str(ptr1:%len(var1)+1) = %trimr(var1)
C eval %str(ptr2:%len(var2)+1) = %trimr(var2)
*
* Call C program with null-terminated strings as parameters.
*
C call 'C_FM_RPG01'
C parm var1
C parm var2
C parm var3
*
* Display result of concatenation.
*
C var3 dsply
*
* End of program.
*
C eval *inlr = *on
ILE C program C_FM_RPG01
/* Include definitions for I/O routines. */
#include <stdio.h>
/* Main program. */
int main(int argc, char *argv[])
{
/* Display parameters sent to the program. */
printf("Incoming parameter 1 = %s\n", argv[1]);
printf("Incoming parameter 2 = %s\n", argv[2]);
/* Concatenate the two incoming parameters into the third parameter. */
strcpy(argv[3],argv[1]);
strcat(argv[3],argv[2]);
/* End of program. Returning with concatenation of parameters 1 and 2 into parameter 3. */
return;
}
Historical Number
30042619
Document Information
Modified date:
18 December 2019
UID
nas8N1016576