diff options
Diffstat (limited to 'implementation/plugin')
-rw-r--r-- | implementation/plugin/include/plugin_manager.hpp | 8 | ||||
-rw-r--r-- | implementation/plugin/include/plugin_manager_impl.hpp | 12 | ||||
-rw-r--r-- | implementation/plugin/src/plugin_manager.cpp | 2 | ||||
-rw-r--r-- | implementation/plugin/src/plugin_manager_impl.cpp | 70 |
4 files changed, 56 insertions, 36 deletions
diff --git a/implementation/plugin/include/plugin_manager.hpp b/implementation/plugin/include/plugin_manager.hpp index 61662b8..d398e24 100644 --- a/implementation/plugin/include/plugin_manager.hpp +++ b/implementation/plugin/include/plugin_manager.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) +// Copyright (C) 2014-2021 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. @@ -19,8 +19,10 @@ public: VSOMEIP_EXPORT virtual ~plugin_manager() {}; VSOMEIP_EXPORT static std::shared_ptr<plugin_manager> get(); VSOMEIP_EXPORT virtual void load_plugins() = 0; - VSOMEIP_EXPORT virtual std::shared_ptr<plugin> get_plugin(plugin_type_e _type, std::string _name) = 0; - + VSOMEIP_EXPORT virtual std::shared_ptr<plugin> get_plugin(plugin_type_e _type, const std::string &_name) = 0; + VSOMEIP_EXPORT virtual void * load_library(const std::string &_path) = 0; + VSOMEIP_EXPORT virtual void * load_symbol(void * _handle, const std::string &_symbol) = 0; + VSOMEIP_EXPORT virtual void unload_library(void * _handle) = 0; }; } // namespace vsomeip_v3 diff --git a/implementation/plugin/include/plugin_manager_impl.hpp b/implementation/plugin/include/plugin_manager_impl.hpp index eb7d09f..e88772e 100644 --- a/implementation/plugin/include/plugin_manager_impl.hpp +++ b/implementation/plugin/include/plugin_manager_impl.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2016-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) +// Copyright (C) 2016-2021 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. @@ -29,7 +29,8 @@ public: VSOMEIP_EXPORT void load_plugins(); - VSOMEIP_EXPORT std::shared_ptr<plugin> get_plugin(plugin_type_e _type, std::string _name); + VSOMEIP_EXPORT std::shared_ptr<plugin> get_plugin(plugin_type_e _type, + const std::string &_name); VSOMEIP_EXPORT std::shared_ptr<plugin> load_plugin( const std::string& _library, plugin_type_e _type, @@ -37,12 +38,13 @@ public: VSOMEIP_EXPORT bool unload_plugin(plugin_type_e _type); + VSOMEIP_EXPORT void * load_library(const std::string &_path); + VSOMEIP_EXPORT void * load_symbol(void * _handle, const std::string &_symbol); + VSOMEIP_EXPORT void unload_library(void * _handle); + private: void add_plugin(const std::shared_ptr<plugin> &_plugin, const std::string& _name); - void * load_library(const std::string &_path); - void * load_symbol(void * _handle, const std::string &_symbol); - bool plugins_loaded_; std::mutex loader_mutex_; diff --git a/implementation/plugin/src/plugin_manager.cpp b/implementation/plugin/src/plugin_manager.cpp index f8ddaa1..d64cbaa 100644 --- a/implementation/plugin/src/plugin_manager.cpp +++ b/implementation/plugin/src/plugin_manager.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2016-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) +// Copyright (C) 2016-2021 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. diff --git a/implementation/plugin/src/plugin_manager_impl.cpp b/implementation/plugin/src/plugin_manager_impl.cpp index 159eea9..bfaf272 100644 --- a/implementation/plugin/src/plugin_manager_impl.cpp +++ b/implementation/plugin/src/plugin_manager_impl.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2016-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) +// Copyright (C) 2016-2021 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. @@ -6,6 +6,7 @@ #include <sstream> #include <vector> #include <stdlib.h> +#include <iomanip> #include <iostream> #ifdef _WIN32 @@ -110,7 +111,8 @@ void plugin_manager_impl::load_plugins() { } } -std::shared_ptr<plugin> plugin_manager_impl::get_plugin(plugin_type_e _type, std::string _name) { +std::shared_ptr<plugin> plugin_manager_impl::get_plugin(plugin_type_e _type, + const std::string &_name) { std::lock_guard<std::recursive_mutex> its_lock_start_stop(plugins_mutex_); auto its_type = plugins_.find(_type); if (its_type != plugins_.end()) { @@ -180,38 +182,52 @@ void * plugin_manager_impl::load_library(const std::string &_path) { #endif } -void * plugin_manager_impl::load_symbol(void * _handle, - const std::string &_symbol) { - void * its_symbol = 0; +void * plugin_manager_impl::load_symbol(void * _handle, const std::string &_symbol_name) { + void* symbol = nullptr; + if (_handle) { #ifdef _WIN32 - HINSTANCE hDLL = (HINSTANCE)_handle; - if (hDLL != NULL) { + symbol = GetProcAddress(reinterpret_cast<HMODULE>(_handle), _symbol_name.c_str()); +#else + symbol = dlsym(_handle, _symbol_name.c_str()); +#endif - typedef UINT(CALLBACK* LPFNDLLFUNC1)(DWORD, UINT); + if (!symbol) { + char* error_message = nullptr; - LPFNDLLFUNC1 lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL, _symbol.c_str()); - if (!lpfnDllFunc1) { - FreeLibrary(hDLL); - std::cerr << "Loading symbol \"" << _symbol << "\" failed (" << GetLastError() << ")" << std::endl; - } - else { - its_symbol = lpfnDllFunc1; - } - } +#ifdef _WIN32 + DWORD error_code = GetLastError(); + FormatMessageA( + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, + error_code, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + reinterpret_cast<LPSTR>(&error_message), + 0, + nullptr + ); #else - if (0 != _handle) { - dlerror(); // Clear previous error - its_symbol = dlsym(_handle, _symbol.c_str()); - const char *dlsym_error = dlerror(); - if (dlsym_error) { - VSOMEIP_INFO << "Cannot load symbol : " << _symbol << " : " << dlsym_error; - dlclose(_handle); + error_message = dlerror(); +#endif + + VSOMEIP_ERROR << "Cannot load symbol " << std::quoted(_symbol_name) << " because: " << error_message; + +#ifdef _WIN32 + // Required to release memory allocated by FormatMessageA() + LocalFree(error_message); +#endif } - } else { - VSOMEIP_ERROR << "Loading failed: (" << dlerror() << ")"; } + return symbol; +} + +void plugin_manager_impl::unload_library(void * _handle) { + if (_handle) { +#ifdef _WIN32 + FreeLibrary(reinterpret_cast<HMODULE>(_handle)); +#else + dlclose(_handle); #endif - return (its_symbol); + } } } // namespace vsomeip_v3 |