IBM Support

Passing Parameters from ILE RPG IV to an ILE C Program

Troubleshooting


Problem

This document contains an example of passing parameters from ILE RPG IV 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 IV, 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 IV program CALLILEC2 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(CALLILEC2) PARM('John' 'Smith')

Press the Enter key.
image-20241115125727-1

ILE RPG IV Program CALLILEC2
**FREE
Dcl-S var1            Char(11)        based(ptr1);
Dcl-S ptr1            Pointer;
Dcl-S var2            Char(11)        based(ptr2);
Dcl-S ptr2            Pointer;
Dcl-S var3            Char(21)        based(ptr3);
Dcl-S ptr3            Pointer;
// Prototype for CALLILEC1
Dcl-Pr Pgm_CALLILEC1 ExtPgm('CALLILEC1');
  parm1           Char(10);
  parm2           Char(10);
End-Pr;
// Procedure interface for CALLILEC1
Dcl-Pi Pgm_CALLILEC1;
  parm1           Char(10);
  parm2           Char(10);
End-Pi;
// Prototype for C_FM_RPG01
Dcl-Pr Pgm_C_FM_RPG01 ExtPgm('C_FM_RPG01');
  var1            Char(11);
  var2            Char(11);
  var3            Char(21);
End-Pr;
//
// Incoming parameter list.
//
//
// Allocate heap storage for the intermediate variables previously defined.
//
ptr1 = %alloc(%size(var1));
ptr2 = %alloc(%size(var2));
ptr3 = %alloc(%size(var3));
//
// Assign incoming parameter values to variables.
//
var1  = parm1;
var2  = parm2;
//
// Place a X'00' terminator (end of string in C) right before the beginning
// of trailing blanks in the parameter strings.
//
%str(ptr1:%len(var1)+1) = %trimr(var1);
%str(ptr2:%len(var2)+1) = %trimr(var2);
//
// Call C program with null-terminated strings as parameters.
//
Pgm_C_FM_RPG01(var1 : var2 : var3);
//
// Display result of concatenation.
//
Dsply var3;
//
// End of program.
//
*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;

}

[{"Type":"MASTER","Line of Business":{"code":"LOB68","label":"Power HW"},"Business Unit":{"code":"BU070","label":"IBM Infrastructure"},"Product":{"code":"SWG60","label":"IBM i"},"ARM Category":[{"code":"a8m0z0000000CHtAAM","label":"Programming ILE Languages"}],"ARM Case Number":"","Platform":[{"code":"PF012","label":"IBM i"}],"Version":"All Versions"}]

Historical Number

30042619

Document Information

Modified date:
15 November 2024

UID

nas8N1016576