33 #ifndef GSLAM_SHAREDLIBRARY_H 34 #define GSLAM_SHAREDLIBRARY_H 39 #if (defined(_WIN32) || defined(_WIN64)) && !defined(__CYGWIN__)// Windows 46 #undef GetShortPathName 47 #undef GetLongPathName 48 #undef GetEnvironmentStrings 49 #undef SetEnvironmentStrings 50 #undef FreeEnvironmentStrings 58 #undef CreateSemaphore 61 #undef GetModuleFileName 64 #undef GetEnvironmentVariable 65 #undef SetEnvironmentVariable 66 #undef ExpandEnvironmentStrings 67 #undef OutputDebugString 72 #undef GetSystemDirector 74 #undef GetTempFileName 75 #undef SetCurrentDirectory 76 #undef GetCurrentDirectory 77 #undef CreateDirectory 78 #undef RemoveDirectory 85 #undef GetComputerName 86 #undef SetComputerName 98 #if defined(__CYGWIN__) && !defined(RTLD_LOCAL) 115 SHLIB_GLOBAL_IMPL = 1,
119 typedef std::mutex MutexRW;
120 typedef std::unique_lock<std::mutex> WriteMutex;
142 bool load(
const std::string& path,
int flags=0)
145 WriteMutex lock(_mutex);
150 #if (defined(_WIN32) || defined(_WIN64)) && !defined(__CYGWIN__)// Windows 152 flags |= LOAD_WITH_ALTERED_SEARCH_PATH;
153 _handle = LoadLibraryExA(path.c_str(), 0, flags);
154 if (!_handle)
return false;
156 int realFlags = RTLD_LAZY;
157 if (flags & SHLIB_LOCAL_IMPL)
158 realFlags |= RTLD_LOCAL;
160 realFlags |= RTLD_GLOBAL;
161 _handle = dlopen(path.c_str(), realFlags);
164 const char* err = dlerror();
165 std::cerr<<
"Can't open file "<<path<<
" since "<<err<<std::endl;
181 WriteMutex lock(_mutex);
185 #if (defined(_WIN32) || defined(_WIN64)) && !defined(__CYGWIN__)// Windows 186 FreeLibrary((HMODULE) _handle);
211 WriteMutex lock(_mutex);
216 #if (defined(_WIN32) || defined(_WIN64)) && !defined(__CYGWIN__)// Windows 217 return (
void*) GetProcAddress((HMODULE) _handle, name.c_str());
219 result = dlsym(_handle, name.c_str());
240 #if defined(__APPLE__) 242 #elif defined(hpux) || defined(_hpux) 244 #elif defined(WIN32) || defined(WIN64) 262 typedef std::shared_ptr<SharedLibrary> SharedLibraryPtr;
bool isLoaded() const
Unloads a shared library.
Definition: SharedLibrary.h:196
const std::string & getPath() const
Returns the address of the symbol with the given name. For functions, this is the entry point of the ...
Definition: SharedLibrary.h:230
virtual ~SharedLibrary()
Creates a SharedLibrary object and loads a library from the given path.
Definition: SharedLibrary.h:131
SharedLibrary(const std::string &path)
Creates a SharedLibrary object.
Definition: SharedLibrary.h:124
void * getSymbol(const std::string &name)
Returns true iff the loaded library contains a symbol with the given name.
Definition: SharedLibrary.h:209
The SharedLibrary class dynamically loads shared libraries at run-time.
Definition: SharedLibrary.h:109
static std::string suffix()
Returns the path of the library, as specified in a call to load() or the constructor.
Definition: SharedLibrary.h:238
bool hasSymbol(const std::string &name)
Returns true iff a library has been loaded.
Definition: SharedLibrary.h:202
void unload()
Loads a shared library from the given path. Throws a LibraryAlreadyLoadedException if a library has a...
Definition: SharedLibrary.h:179
bool load(const std::string &path, int flags=0)
Destroys the SharedLibrary. The actual library remains loaded.
Definition: SharedLibrary.h:142