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

Library.cpp

Go to the documentation of this file.
00001 #include "OSDependent/Library.h"
00002 #include "OSDependent/OS.h"
00003 
00004 #define DLL_SUFFIX ".dll"
00005 
00006 Library::Library() :
00007 _handle(NULL)
00008 {
00009 }
00010 
00011 Library::~Library()
00012 {
00013   unload();
00014 }
00015 
00016 /*
00017  * Load a library.
00018  *
00019  * Return -1 on failure, 0 on success.
00020  */
00021 int 
00022 Library::load(const char *libName, int mode)
00023 {
00024   // Windows supports a single mode, so we ignore the mode parameter.
00025   _handle = ::LoadLibrary(libName);
00026 
00027   if (_handle == NULL)
00028     return -1;
00029   return 0;
00030 }
00031 
00032 /*
00033  * Unload the previously loaded library.
00034  *
00035  * Return -1 on failure, 0 on success.
00036  */
00037 int 
00038 Library::unload()
00039 {
00040   if (_handle == NULL)
00041     return -1;
00042   if (::FreeLibrary(_handle) == 0) {
00043     _handle = NULL;
00044     return -1;
00045   }
00046 
00047         _handle=0;
00048 
00049   return 0;
00050 }
00051 
00052 /*
00053  * Get a pointer to a function from this library.
00054  *
00055  * Returns function address on success (caller must
00056  * cast to correct function signature), NULL on failure.
00057  */
00058 void *
00059 Library::getFuncPtr(const char *funcName) const
00060 {
00061   if (_handle == NULL)
00062     return NULL;
00063   return ::GetProcAddress(_handle, funcName);
00064 }
00065 
00066 /*
00067  * Add the standard dynamic library prefix and suffix for this platform.
00068  * Returns a new string containing the library name in nativeName.
00069  */
00070 void
00071 Library::getNativeLibName(const char *genericName, char *nativeName)
00072 {
00073   char temp[MAXNAMELEN];
00074   strcpy(temp, genericName);
00075   // strrchr returns a pointer to the last occurrence of a char in a string.
00076   char *filename = strrchr(temp, OS::DirectorySeparator);
00077   if (filename != NULL) {
00078     // It's a compound pathname
00079     *filename = '\0';
00080     filename++;
00081     sprintf(nativeName, "%s%c%s%s", temp, OS::DirectorySeparator,
00082             filename, DLL_SUFFIX);
00083     // Note that the method we use inside this if statement does not look into
00084     // the LD_LIBRARY_PATH, it only looks at the current directory.
00085     // To do that, we'd need somthing like the complicated ACE::ldfind() in ace/ACE.cpp.
00086   } else {
00087     // It's not compound pathname, simply add prefix and suffix.
00088     sprintf(nativeName, "%s%s", genericName, DLL_SUFFIX);
00089   }
00090 }

Generated on Mon Oct 7 09:33:29 2002 for Gaia by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001