z/OS Language Environment Programming Guide for 64-bit Virtual Addressing Mode
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


Explicit use of a DLL in a application using the dlopen() family of functions

z/OS Language Environment Programming Guide for 64-bit Virtual Addressing Mode
SA38-0689-00

  #define _UNIX03_SOURCE

  #include <dlfcn.h>
  #include <stdio.h>
  #include <string.h>

  #ifdef __cplusplus
    extern "C" {
  #endif

    typedef int (DLL_FN)(void);

  #ifdef __cplusplus
    }
  #endif

  #define FUNCTION        "FUNCTION"
  #define VARIABLE        "VARIABLE"

  static void Syntax(const char* progName) {
    fprintf(stderr, "Syntax: %s <DLL-name> <type> <identifier>\n"
                    "  where\n"
                    "  <DLL-name> is the DLL to open,\n"
                    "  <type> can be one of FUNCTION or VARIABLE,\n"
                    "  and <identifier> is the symbol to reference\n"
                    "  (either a function or variable, as determined by"
                    " <type>)\n", progName);
    return;
  }
  main(int argc, char* argv[]) {
    int value;
    void* symPtr;
    char* dll;
    char* type;
    char* id;
    void* dllHandle;
    if (argc != 4) {
      Syntax(argv[0]);
      return(4);
    }

    dll  = argv[1];
    type = argv[2];
    id   = argv[3];

    dllHandle = dlopen(dll, 0);
    if (dllHandle == NULL) {
      fprintf(stderr, "dlopen() of DLL %s failed: %s\n", dll, dlerror());
      return(8);
    }

    /*
     * get address of symbol (may be either function or variable)
     */
    symPtr = (int*)(dlsym(dllHandle, id));
    if (symPtr == NULL) {
      fprintf(stderr, "dlsym() error: symbol %s not exported from %s: %s\n"
                    , id, dll, dlerror());
      return(8);
    }

    if (strcmp(type, FUNCTION)) {
      if (strcmp(type, VARIABLE)) {
        fprintf(stderr,
          "Type specified was not " FUNCTION " or " VARIABLE "\n");
        Syntax(argv[0]);
        return(8);
      }
      /*
       * variable request, so display its value
       */
      value = *(int *)symPtr;
      printf("Variable %s has a value of %d\n", id, value);
    }
    else {
      /*
       * function request, so call it and display its return value
       */
      value = ((DLL_FN *)symPtr)();
      printf("Result of call to %s() is %d\n", id, value);
    }
    dlclose(dllHandle);

    return(0);
  }

For more information about the DLL functions, see z/OS XL C/C++ Runtime Library Reference.

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014