/* * sharedlibrary.cpp * * Created on: Apr 19, 2014 * Author: gregor */ #if defined WIN32 #include #endif #if defined LINUX || defined ANDROID #include #endif #include #include namespace gltb { SharedLibrary::SharedLibrary(std::string libraryName) : libraryName(libraryName) { #if defined WIN32 // TODO reimplement this using LoadLibraryEx library = LoadLibraryA(libraryName.c_str()); if(library == NULL) { std::ostringstream message; message << "LoadLibrary failed with error code " << GetLastError(); throw Exception(message.str(), "gltb::SharedLibrary::SharedLibrary()"); } #else library = dlopen(libraryName.c_str(), RTLD_NOW); if(library == 0) { throw Exception("dlopen failed: " + std::string(dlerror()), "gltb::SharedLibrary::SharedLibrary()"); } #endif } SharedLibrary::~SharedLibrary() { #if defined WIN32 FreeLibrary(library); #else dlclose(library); #endif } void *SharedLibrary::getSymbol(std::string name) { #if defined WIN32 void *result = GetProcAddress(library, name.c_str()); if(result == NULL) { std::ostringstream message; message << "GetProcAddress failed with error code " << GetLastError(); throw Exception(message.str(), "gltb::SharedLibrary::SharedLibrary()"); } return result; #else dlerror(); // clear any previous error void *result = dlsym(library, name.c_str()); // dlsym returning NULL may be ok char *error = dlerror(); // check dlerror again for actual errors if(error != 0) { throw Exception("dlsym failed: " + std::string(error), "gltb::SharedLibrary::getSymbol()"); } return result; #endif } }