Test Loader for Linux HeroScript Extension Plugins

From HEWIKI
Jump to: navigation, search

He server.png Documentation on this page is intended for customers of HeroEngine managing their own server(s).

The following source code can be compiled to create a command line utility to test HeroScript Extension Interface Plugins on Linux platforms. When compiling this with gcc, include the -ldl on the command line.

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
 
int main(int argc, char *argv[]) {
  void *handle;
  void *(*getFunc)(int);
  int (*releaseFunc)(void *);
  char *error;
  int i;
  void *interfaceHandle = 0;
 
  char *libname = "libHSEIDemo.so";
  char *getSymbolName = "getInterface";
  char *releaseSymbolName = "releaseInterface";
 
  if (argc > 1)
    libname = argv[1];
  else
    fprintf(stdout, "Pass a library name on the command line to test a library with a different name then %s.\n", libname);
 
  fprintf(stdout, "Testing HSEI plugin dynamic load library %s.\n", libname);
 
  // Open the library
  fprintf(stdout, "Opening the library %s with dlopen(\"%s\")\n", libname, libname);
  if (!(handle = dlopen(libname, RTLD_LAZY))) {
    fprintf(stderr, "Failed to open library %s, error: %s\n", libname, dlerror());
    exit(1);
  }
 
  // Find the getInterface function in the library
  fprintf(stdout, "Finding function %s in library with dlsym(handle, \"%s\")\n", getSymbolName, getSymbolName);
  getFunc = dlsym(handle, getSymbolName);
  if ((error = dlerror()) != NULL) {
    fprintf(stderr, "Failed to locate the function %s in the %s, error: %s\n", libname, getSymbolName, error);
    exit(1);
  }
 
  // Find the releaseInterface function in the library
  fprintf(stdout, "Finding function %s in library with dlsym(handle, \"%s\")\n", releaseSymbolName, releaseSymbolName);
  releaseFunc = dlsym(handle, releaseSymbolName);
  if ((error = dlerror()) != NULL) {
    fprintf(stderr, "Failed to locate the function %s in the %s, error: %s\n", libname, releaseSymbolName, error);
    exit(1);
  }
 
  // Call the symbol to determine if the library can retrieve the interface
  fprintf(stdout, "Calling function %s\n", getSymbolName);
  interfaceHandle = getFunc(2);
  if ( 0 == interfaceHandle ) {
    fprintf(stderr, "Failed to get the HSEI interface with version 2\n");
    exit(1);
  }
 
  // We just wanted to get the interface, go ahead and release it before exiting
  fprintf(stdout, "Calling function %s\n", releaseSymbolName);
  if ( 1 != releaseFunc(interfaceHandle) ) {
    fprintf(stderr, "Failed to release the HSEI interface\n");
    exit(1);
  }
 
  fprintf(stdout, "All tests successful\n");
 
  dlclose(handle);
 
  return 0;
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox