From 32373dc9570738ddb0f9cf9b00008234142b5f5d Mon Sep 17 00:00:00 2001 From: WeilongWen <81895406+WeilongWen@users.noreply.github.com> Date: Sun, 27 Oct 2024 20:33:36 +0800 Subject: [PATCH] Keeping the dll path cookie during class instantiation (#147) --- include/fmi4cpp/fmi2/fmi2_library.hpp | 4 ++++ src/fmi4cpp/fmi2/fmi2_library.cpp | 21 +++++++++++++++++++++ src/fmi4cpp/library_helper.hpp | 27 ++------------------------- 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/include/fmi4cpp/fmi2/fmi2_library.hpp b/include/fmi4cpp/fmi2/fmi2_library.hpp index b09593c..961d379 100644 --- a/include/fmi4cpp/fmi2/fmi2_library.hpp +++ b/include/fmi4cpp/fmi2/fmi2_library.hpp @@ -55,6 +55,10 @@ class fmi2_library fmi2FreeInstanceTYPE* fmi2FreeInstance_; +private: +#ifdef _WIN32 + DLL_DIRECTORY_COOKIE dllDirectoryCookie_ = nullptr; +#endif protected: fmi2Status lastStatus_; DLL_HANDLE handle_ = nullptr; diff --git a/src/fmi4cpp/fmi2/fmi2_library.cpp b/src/fmi4cpp/fmi2/fmi2_library.cpp index 62dc382..06f7f87 100644 --- a/src/fmi4cpp/fmi2/fmi2_library.cpp +++ b/src/fmi4cpp/fmi2/fmi2_library.cpp @@ -60,6 +60,20 @@ fmi2_library::fmi2_library(const std::string& modelIdentifier, const std::shared MLOG_DEBUG("Loading shared library '" + std::filesystem::path(libName).stem().string() + get_shared_library_extension() + "'"); +#ifdef _WIN32 + std::string dllDirectory; + fmi4cpp::fs::path path(libName); + + if (path.has_parent_path()) { + dllDirectory = path.parent_path().string(); + } + + if (!dllDirectory.empty()) { + std::wstring wDllDirectory(dllDirectory.begin(), dllDirectory.end()); + dllDirectoryCookie_ = AddDllDirectory(wDllDirectory.c_str()); + } +#endif + handle_ = load_library(libName); if (!handle_) { @@ -419,6 +433,13 @@ void fmi2_library::free_instance(fmi2Component c) fmi2_library::~fmi2_library() { +#ifdef _WIN32 + if (dllDirectoryCookie_) { + RemoveDllDirectory(dllDirectoryCookie_); + dllDirectoryCookie_ = nullptr; + } +#endif + if (handle_) { if (!free_library(handle_)) { MLOG_ERROR(getLastError()); diff --git a/src/fmi4cpp/library_helper.hpp b/src/fmi4cpp/library_helper.hpp index e695c7c..e69bbe2 100644 --- a/src/fmi4cpp/library_helper.hpp +++ b/src/fmi4cpp/library_helper.hpp @@ -1,10 +1,7 @@ - #ifndef FMI4CPP_LIBRARYHELPER_HPP #define FMI4CPP_LIBRARYHELPER_HPP #include - -#include #include namespace fmi4cpp @@ -12,28 +9,8 @@ namespace fmi4cpp inline DLL_HANDLE load_library(const std::string& libName) { - std::string dllDirectory; - -#ifdef _WIN32 - std::filesystem::path path(libName); - if (path.has_parent_path()) { - dllDirectory = path.parent_path().string(); - } - - DLL_DIRECTORY_COOKIE dllDirectoryCookie = nullptr; - if (!dllDirectory.empty()) { - std::wstring wDllDirectory(dllDirectory.begin(), dllDirectory.end()); - dllDirectoryCookie = AddDllDirectory(wDllDirectory.c_str()); - } - - DLL_HANDLE handle = LoadLibrary(libName.c_str()); - - if (dllDirectoryCookie) { - RemoveDllDirectory(dllDirectoryCookie); - } - - return handle; - +#ifdef WIN32 + return LoadLibrary(libName.c_str()); #else return dlopen(libName.c_str(), RTLD_NOW | RTLD_LOCAL); #endif