//! @file CrossDynamicLib.cpp //! @created 06.07.2008 //! @author Chris #include #ifdef WIN32 #include #else #include #endif #include #include ////////////////////////////////////////////////////////////////////////// class DynamicLibException : public std::exception { private: std::string msg; public: DynamicLibException(const char* what): std::exception(), msg(what) {} virtual ~DynamicLibException() throw() {} virtual const char* what() const throw() { return msg.c_str(); } }; ////////////////////////////////////////////////////////////////////////// cross::DynamicLibrary::DynamicLibrary(const char* libname): libhandle(NULL) { #ifdef WIN32 libhandle = LoadLibrary(libname); #else libhandle = dlopen(libname, RTLD_LOCAL); #endif if(!libhandle) throw( DynamicLibException("Library could not be loaded.") ); } //--------------------------------------------------------------------- cross::DynamicLibrary::~DynamicLibrary() { if(libhandle) { #ifdef WIN32 FreeLibrary((HINSTANCE)libhandle); #else if(dlclose(libhandle) != 0) throw( DynamicLibException("Library could not be closed.") ); #endif } } //--------------------------------------------------------------------- void* cross::DynamicLibrary::GetFunction(const char* funcname) { void* func = NULL; if(!libhandle) throw( DynamicLibException("Access to unloaded library.") ); #ifdef WIN32 func = GetProcAddress((HINSTANCE)libhandle, funcname); #else func = dlsym(libhandle, funcname); #endif return func; } ////////////////////////////////////////////////////////////////////////// //