00001 // DynamicLibrary.cpp 00002 00003 #include "DynamicLibrary.h" 00004 00005 // ctor that loads shared lib 00006 DynamicLibrary::DynamicLibrary(const char *libName) : libHandle(NULL) 00007 { 00008 Load(libName); 00009 } 00010 00011 DynamicLibrary::~DynamicLibrary() 00012 { 00013 Unload(); 00014 } 00015 00016 // load and unload member methods 00017 bool DynamicLibrary::Load(const char *libName) 00018 { 00019 if (libName != NULL && std::strlen(libName) > 0) 00020 if (libHandle == NULL) 00021 libHandle = ::dlopen( libName, RTLD_LAZY ); 00022 00023 return (libHandle != NULL); 00024 } 00025 00026 bool DynamicLibrary::Unload() 00027 { 00028 if (libHandle != NULL) 00029 ::dlclose( libHandle ); 00030 00031 libHandle = NULL; 00032 cache.clear(); 00033 return true; 00034 } 00035 00036 // cached and uncached access to functions in dynamic lib 00037 DLPROC DynamicLibrary::GetProcAddr(const char *procName) const 00038 { 00039 DLPROC proc = NULL; 00040 if (libHandle != NULL) 00041 proc = (DLPROC) ::dlsym(libHandle, procName); 00042 return proc; 00043 } 00044 00045 DLPROC DynamicLibrary::GetProcAddrCached(const char *procName, size_t procId) 00046 { 00047 DLPROC proc = NULL; 00048 if (libHandle != NULL) { 00049 if ((procId >= cache.size()) || (cache.size() < 1)) 00050 cache.resize(procId + 1, cache_info()); 00051 cache_info &ci = cache[ procId ]; 00052 if (ci.procAddr == NULL && !ci.testFlag) { 00053 ci.testFlag = true; 00054 ci.procAddr = GetProcAddr(procName); 00055 } 00056 proc = ci.procAddr; 00057 } 00058 return proc; 00059 }