Before you following the instructions below, you must compile and link your C code as identified in Calling C functions through EGL libraries.
For example, the following function invocation statement calls the C function sendmsg( )
sendmsg(chartype, 4, msg_status, return_code);
It passes two arguments (chartype and 4, respectively) to the function and expects two arguments to be passed back (msg_status and return_code, respectively). This is made clear by defining the function in a native library as follows:
Library I4GLFunctions type nativeLibrary {callingConvention = I4GL, dllName = "mydll"} Function sendmsg(chartype char(10) in, i int in, msg_status int out, return_code int out) end end
The arguments passed are specified using the "in" parameter and the arguments to be returned are specified using the "out" parameter.
The C function receives an integer argument that specifies how many values were pushed on the argument stack (in this case, two arguments). This is the number of values to be popped off the stack in the C function. The function also needs to return values for the msg_status and return_code arguments before passing control back to the EGL program.
The C function should not assume it has been passed the correct number of stacked values. The C function should test its integer argument to see how many EGL arguments were stacked for it.
This example shows a C function that requires exactly one argument:
int nxt_bus_day(int nargs); { int theDate; if (nargs != 1) { fprintf(stderr, "nxt_bus_day: wrong number of parms (%d)\n", nargs ); ibm_lib4gl_returnDate(0L); return(1); } ibm_lib4gl_popDate(&theDate); switch(rdayofweek(theDate)) { case 5: /* change friday -> monday */ ++theDate; case 6: /* saturday -> monday*/ ++theDate; default: /* (sun..thur) go to next day */ ++theDate; } ibm_lib4gl_returnDate(theDate); /* stack result */ return(1) /* return count of stacked */ }
The function returns the date of the next business day after a given date. Because the function must receive exactly one argument, the function checks for the number of arguments passed. If the function receives a different number of arguments, it terminates the program (with an identifying message).