Passing PHP variables to service program by Value…

[adrotate group=”3,4,7″]
We had a slight problem when enhancing the PHP code we posted recently for displaying messages due to the way the value was interpreted by the service program.

Here is a quick overview of the Code which gave us the problem. (We have only posted the relevant parts)

This is the PHP code we used to call the service function

function get_msgq_list(&$conn,$br) {
$desc = array(
      array("Name" => 'NumEnt', "io" => I5_INOUT, "type" => I5_TYPE_INT),
      array("Name" => "Sev", "io" => I5_IN, "type" => I5_TYPE_INT),
      array("DSName" =>"msg", "CountRef" => 'NumEnt', "DSParm" => array(
            array("Name" => "msgid", "io" => I5_OUT, "type" => I5_TYPE_CHAR, "length" => "7"),
            array("Name" => "msgkey", "io" => I5_OUT, "type" => I5_TYPE_CHAR, "length" => "8"),
            array("Name" => "dta", "io" => I5_OUT, "type" => I5_TYPE_CHAR, "length" => "132"))));

$prog = i5_program_prepare("SVRSTS(Get_MsgQ_List)", $desc );
if ($prog === FALSE) {
      $errorTab = i5_error ();
      echo "Program prepare failed 
n"; var_dump ( $errorTab ); die (); } $parameter = array("NumEnt" => $maxmsg, "Sev" => '30'); $parmOut = array("NumEnt" => "nbr", "msg" => "messages"); $ret = i5_program_call($prog, $parameter, $parmOut); .....

We have declared a new input only variable called “Sev” and we are passing it onto the service program function. Here is the relevant code from the service program.

Int Get_MsgQ_List(int *num, int Severity, Msg_Dets_t data[]) {
..
….
//just to send msg about the passed variable
sprintf(msg_dta,"Severity to list = %d",Severity);
snd_msg("GEN0001",msg_dta,strlen(msg_dta));
..
….
Sel_Info.Severity_Criteria = Severity;

I get the following message into the message queue regardless of the parameter setting.

Severity to list = 10

However if I change the service program call structure to..

Int Get_MsgQ_List(int *num, int *Severity, Msg_Dets_t data[]) {
..
….
//just to send msg about the passed variable
sprintf(msg_dta,"Severity to list = %d",*Severity);
snd_msg("GEN0001",msg_dta,strlen(msg_dta));
..
….
Sel_Info.Severity_Criteria = *Severity;

The Sev parameter is being received as a pointer to an integer, if I call the service program using this code I get the following message in the message queue which is exactly what I wanted. But I had to pass be reference not something I had intended.

Severity to list = 30

We made a quick call to Aura Equipments to ask why when passing by value we always saw 10 as the variable passed? They came back to us and gave us the correct manner to pass a variable by Value which is..

function get_msgq_list(&$conn,$br) {
$desc = array(
      array("Name" => 'NumEnt', "io" => I5_INOUT, "type" => I5_TYPE_INT),
      array("Name" => "Sev", "io" => I5_IN|I5_BYVAL, "type" => I5_TYPE_INT),
      array("DSName" =>"msg", "CountRef" => 'NumEnt', "DSParm" => array(
            array("Name" => "msgid", "io" => I5_OUT, "type" => I5_TYPE_CHAR, "length" => "7"),
            array("Name" => "msgkey", "io" => I5_OUT, "type" => I5_TYPE_CHAR, "length" => "8"),
            array("Name" => "dta", "io" => I5_OUT, "type" => I5_TYPE_CHAR, "length" => "132"))));

We had to add the ‘|I5_BYVAL’ to the parameter definitions, this is not shown in the documentation or anywhere online I could find. So if you are developing program calls and need to pass in a variable by value this is the way to do it. Thanks again to EasyCom for their great support..

Hope this saves others from pulling too much hair out when trying to debug like we were!

Chris…

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.