Show how to call a function in a shared library (without dynamically linking to it at compile-time). In particular, show how to call the shared library function if the library is available, otherwise use an internal equivalent function.

This is a special case of calling a foreign language function where the focus is close to the ABI level and not at the normal API level.



Related task

 OpenGL -- OpenGL is usually maintained as a shared library.


/*REXX program calls a function  (sysTextScreenSize)  in a shared library  (regUtil).   */
 
                        /*Note:  the  REGUTIL.DLL  (REGina UTILity Dynamic Link Library */
                        /*       should be in the  PATH  or the current directory.      */
 
rca= rxFuncAdd('sysLoadFuncs', "regUtil", 'sysLoadFuncs')  /*add a function library.    */
if rca\==0  then do                                        /*examine the    return code.*/
                 say 'return code' rca "from rxFuncAdd"    /*tell about bad    "     "  */
                 exit rca                                  /*exit this program with RC. */
                 end
 
rcl= sysLoadFuncs()                                        /*we can load the functions. */
if rcl\==0  then do                                        /*examine the    return code.*/
                 say 'return code' rcl "from sysLoadFuncs" /*tell about bad    "     "  */
                 exit rcl                                  /*exit this program with RC. */
                 end
                                                           /* [↓]   call a function.    */
$= sysTextScreenSize()                                     /*$  has 2 words:  rows cols */
parse var  $     rows  cols  .                             /*get two numeric words in $.*/
say '    rows='  rows                                      /*show number of screen rows.*/
say '    cols='  cols                                      /*  "     "    "     "  cols.*/
 
rcd= SysDropFuncs()                                        /*make functions inaccessible*/
if rcd\==0  then do                                        /*examine the    return code.*/
                 say 'return code' rcd "from sysDropFuncs" /*tell about bad    "     "  */
                 exit rcd                                  /*exit this program with RC. */
                 end
exit 0                                           /*stick a fork in it,  we're all done. */