Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

BaseSharedLib.cpp

Go to the documentation of this file.
00001 #include "BaseSharedLib.h"
00002 #include <cstdio>
00003 #include <cerrno>
00004 #include <string>
00005 
00006 #if defined(__sun) 
00007 #include <stdio.h>
00008 #include <strings.h>
00009 #endif /* __sun */
00010 
00011 BaseSharedLib::BaseSharedLib(const char *pathName, int loadIt) :
00012   fLibPath(NULL),fLibHandle(0) 
00013 {
00014   setPathName(pathName);
00015   if (loadIt)
00016     load();
00017 }
00018 
00019 BaseSharedLib::~BaseSharedLib()
00020 {
00021   if (fLibHandle)
00022     unload();   
00023   delete [] fLibPath;
00024 }
00025 
00026 void BaseSharedLib::setPathName(const char *name)
00027 {
00028   delete [] fLibPath;
00029   fLibPath = new char [strlen(name)+1];
00030   strcpy(fLibPath,name);
00031 }
00032 
00033 const char *BaseSharedLib::getPathName() 
00034 {
00035   return fLibPath;
00036 }
00037 
00038 int BaseSharedLib::load()
00039 {
00040   if (fLibHandle)
00041     unload();
00042   
00043 #if defined(__hpux)
00044   if ((fLibHandle = shl_load(fLibPath,BIND_IMMEDIATE | BIND_VERBOSE ,0)) == 0)
00045   {
00046     perror(fLibPath);
00047     return 0;
00048   }
00049 #endif
00050 
00051 
00052 #ifdef _WIN32
00053   if ((fLibHandle = LoadLibrary(fLibPath)) == NULL)
00054   {
00055     printf ("Last error %d \n",GetLastError());
00056     perror("loading shared schema lib ");
00057     return 0;
00058   }
00059 
00060 #endif
00061 
00062 #ifdef STD_SH_LIB
00063 #if (defined __sun) && (__SUNPRO_CC > 0x500)
00064   // from the Sun WSpro (v 6)  user-guide (p 6-6): 
00065   // Note : When shared libraries are opened with dlopen(), RTLD_GLOBAL must be
00066   // used for exceptions to work.
00067   if ((fLibHandle = dlopen(fLibPath,RTLD_LAZY)) == 0)
00068 #else
00069   if ((fLibHandle = dlopen(fLibPath,RTLD_LAZY)) == 0)
00070 #endif
00071   {
00072     // print out the error message from the dynamic loader (if available) 
00073     char *errMesg = dlerror();
00074     if (errMesg != 0) {
00075       fprintf(stderr,"%s\n",errMesg);    
00076       perror(fLibPath);
00077     }
00078     return 0;
00079   }
00080 #endif
00081   
00082   return 1;
00083 }
00084  
00085 
00086 void BaseSharedLib::unload()
00087 {
00088   if (fLibHandle != 0)
00089   {
00090 #if defined(__hpux)
00091     shl_unload(fLibHandle);
00092 #endif
00093 
00094 #ifdef _WIN32
00095     if ( FreeLibrary( fLibHandle ) == NULL ) {
00096         perror("unloading shared schema lib ");
00097       }
00098 #endif /* _WIN32 */
00099 
00100 #ifdef STD_SH_LIB
00101     dlclose(fLibHandle);
00102 #endif      
00103   }
00104   
00105   fLibHandle = 0;
00106 }
00107 
00108 void *BaseSharedLib::lookUp(const char *funName)
00109 {
00110   void *retval = 0;
00111   if (fLibHandle != 0)
00112   {
00113 #if defined(__hpux)
00114     int rc = shl_findsym(&fLibHandle,funName,TYPE_PROCEDURE,&retval);
00115     if (rc != 0) {
00116       fprintf(stderr,"Can't lookup %s\n",funName);    
00117       perror(fLibPath);
00118     }
00119 #endif
00120 #ifdef _WIN32
00121     //
00122 #endif /* _WIN32 */
00123 
00124 #ifdef STD_SH_LIB
00125     retval = dlsym(fLibHandle,funName);
00126     char *errMesg = dlerror();
00127     if (errMesg != 0) {
00128       fprintf(stderr,"%s\n",errMesg);    
00129       perror(fLibPath);
00130     }
00131 #endif      
00132   }
00133   return retval;
00134 }

Generated on Tue May 20 14:50:25 2003 for HepUtilities by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002