可从 EGL 程序调用 C 函数。在遵循下列指示信息之前,必须按将 C 函数与 EGL 配合使用中标识的那样编译并链接 C 代码。
例如,以下函数调用语句调用 C 函数 sendmsg( )
sendmsg(chartype, 4, msg_status, return_code);
它将两个自变量(分别是 chartype 和 4)传递至该函数并期望有两个自变量被传递回来(分别是 msg_status 和 return_code)。这是通过按如下所示在本机库中定义函数来明确的:
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
传递的自变量是使用“in”参数指定的,并且要返回的自变量是使用“out”参数指定的。
C 函数接收一个整数自变量,它指定自变量堆栈上推送多少个值(在此情况下为两个自变量)。这是要在 C 函数中弹出堆栈的值的数目。在将控制权交还给 EGL 程序之前,该函数还需要返回 msg_status 和 return_code 自变量的值。出栈外部函数在从 EGL 接收值中作了描述;返回外部函数在将值返回至 EGL 中作了描述。
C 函数不应假定它已经传递了正确数目的堆栈化值。C 函数应测试其整数自变量以了解有多少个 EGL 自变量已堆栈化。
以下示例显示刚好需要一个自变量的 C 函数:
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 */ }
该函数返回指定日期后的下一个工作日的日期。因为函数必须刚好接收一个自变量,所以函数会检查传递的自变量数目。如果函数接收不同数目的自变量,它会终止该程序(并带有标识消息)。