#ifndef UTILITY_LIBRARY_H #define UTILITY_LIBRARY_H #include #include #include #include #ifdef _WIN32 #include #else #include #define __stdcall #endif #include "utility/file/FilePath.h" namespace utility { template std::function loadFunctionFromLibrary( const FilePath& libraryPath, const std::string& functionName, std::string& errorString) { #ifdef _WIN32 const std::string libraryPathString = libraryPath.getBackslashedString(); HINSTANCE handle = LoadLibrary(libraryPathString.c_str()); if (handle == NULL) { errorString = "Could not load library \"" + libraryPathString + "\""; return std::function(); } FARPROC functionId = GetProcAddress(handle, functionName.c_str()); if (!functionId) { errorString = "Could not locate the function \"" + functionName + "\" in library\"" + libraryPathString + "\""; return std::function(); } #else void* handle = dlopen(libraryPath.str().c_str(), RTLD_LAZY); if (handle == nullptr) { errorString = "Could not load library \"" + libraryPath.str() + "\""; return std::function(); } void* functionId = (void*) dlsym(handle, functionName.c_str()); const char *dlsym_error = dlerror(); if (dlsym_error || !functionId) { std::stringstream ss; ss << "Cannot load symbol '" << functionName << "' from library '" << libraryPath.str() << "': " << dlsym_error; errorString = ss.str(); dlclose(handle); return std::function(); } #endif return std::function(reinterpret_cast(functionId)); } } #endif // UTILITY_LIBRARY_H