Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

vmddlopen.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr
00003  *cr            (C) Copyright 1995-2008 The Board of Trustees of the
00004  *cr                        University of Illinois
00005  *cr                         All Rights Reserved
00006  *cr
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  * RCS INFORMATION:
00011  *
00012  *      $RCSfile: vmddlopen.c,v $
00013  *      $Author: johns $        $Locker:  $             $State: Exp $
00014  *      $Revision: 1.15 $      $Date: 2008/03/27 19:36:52 $
00015  *
00016  ***************************************************************************
00017  * DESCRIPTION:
00018  *   Routines for loading dynamic link libraries and shared object files
00019  *   on various platforms, abstracting from machine dependent APIs.
00020  ***************************************************************************/
00021 
00022 #include <stdio.h> 
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include "vmddlopen.h"
00026 
00027 #if defined(ARCH_HPUX11)
00028 
00029 #include <dl.h>
00030 #include <errno.h>
00031 #include <string.h>
00032 
00033 void *vmddlopen( const char *path) {
00034     void *ret;
00035     ret = shl_load( path, BIND_IMMEDIATE | BIND_FIRST | BIND_VERBOSE, 0);
00036     return ret;
00037 }
00038 
00039 int vmddlclose( void *handle ) {
00040     return shl_unload( (shl_t) handle );
00041 }
00042 
00043 void *vmddlsym( void *handle, const char *sym ) {
00044     void *value=0;
00045 
00046     if ( shl_findsym( (shl_t*)&handle, sym, TYPE_UNDEFINED, &value ) != 0 ) 
00047         return 0;
00048     return value;
00049 }
00050 
00051 const char *vmddlerror( void  ) {
00052     return strerror( errno );
00053 }
00054 
00055 #elif 0 && (defined(ARCH_MACOSX) || defined(ARCH_MACOSXX86))
00056 /*
00057  * This is only needed for MacOS X version 10.3 or older
00058  */
00059 #include <mach-o/dyld.h>
00060 
00061 void *vmddlopen( const char *path) {
00062   NSObjectFileImage image;
00063   NSObjectFileImageReturnCode retval;
00064   NSModule module;
00065 
00066   retval = NSCreateObjectFileImageFromFile(path, &image);
00067   if (retval != NSObjectFileImageSuccess)
00068     return NULL;
00069 
00070   module = NSLinkModule(image, path,
00071             NSLINKMODULE_OPTION_BINDNOW | NSLINKMODULE_OPTION_PRIVATE
00072             | NSLINKMODULE_OPTION_RETURN_ON_ERROR);
00073   return module;  /* module will be NULL on error */
00074 }
00075 
00076 int vmddlclose( void *handle ) {
00077   NSModule module = (NSModule *)handle;
00078   NSUnLinkModule(module, NSUNLINKMODULE_OPTION_NONE);
00079   return 0;
00080 }
00081 
00082 void *vmddlsym( void *handle, const char *symname ) {
00083   char *realsymname;
00084   NSModule module;
00085   NSSymbol sym;
00086   /* Hack around the leading underscore in the symbol name */
00087   realsymname = (char *)malloc(strlen(symname)+2);
00088   strcpy(realsymname, "_");
00089   strcat(realsymname, symname);
00090   module = (NSModule)handle;
00091   sym = NSLookupSymbolInModule(module, realsymname);
00092   free(realsymname);
00093   if (sym) 
00094     return (void *)(NSAddressOfSymbol(sym));
00095   return NULL;
00096 }
00097 
00098 const char *vmddlerror( void  ) {
00099   NSLinkEditErrors c;
00100   int errorNumber;
00101   const char *fileName;
00102   const char *errorString = NULL;
00103   NSLinkEditError(&c, &errorNumber, &fileName, &errorString);
00104   return errorString;
00105 }
00106 
00107 #elif defined(_MSC_VER)
00108 
00109 #include <windows.h>
00110 
00111 void *vmddlopen(const char *fname) {
00112   return (void *)LoadLibrary(fname);
00113 }
00114 
00115 const char *vmddlerror(void) {
00116   static CHAR szBuf[80]; 
00117   DWORD dw = GetLastError(); 
00118  
00119   sprintf(szBuf, "vmddlopen failed: GetLastError returned %u\n", dw); 
00120   return szBuf;
00121 }
00122 
00123 void *vmddlsym(void *h, const char *sym) {
00124   return (void *)GetProcAddress((HINSTANCE)h, sym);
00125 }
00126 
00127 int vmddlclose(void *h) {
00128   /* FreeLibrary returns nonzero on success */
00129   return !FreeLibrary((HINSTANCE)h);
00130 }
00131 
00132 #else
00133 
00134 /* All remaining platforms (not Windows, HP-UX, or MacOS X <= 10.3) */
00135 #include <dlfcn.h>
00136 
00137 void *vmddlopen(const char *fname) {
00138   return dlopen(fname, RTLD_NOW);
00139 }
00140 const char *vmddlerror(void) {
00141   return dlerror();
00142 }
00143 void *vmddlsym(void *h, const char *sym) {
00144   return dlsym(h, sym);
00145 }
00146 int vmddlclose(void *h) {
00147   return dlclose(h);
00148 }
00149 
00150 #endif 
00151 

Generated on Fri Sep 5 01:25:19 2008 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002