From 675e1a184e574044a8d508b130b509bcd30d45bb Mon Sep 17 00:00:00 2001 From: Wenweil1 <81895406+Wenweil1@users.noreply.github.com> Date: Thu, 5 Sep 2024 16:41:34 +0800 Subject: [PATCH] add dll folder as loading library search path --- src/fmi4cpp/library_helper.hpp | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/fmi4cpp/library_helper.hpp b/src/fmi4cpp/library_helper.hpp index 5a34dbc..dd6ee5e 100644 --- a/src/fmi4cpp/library_helper.hpp +++ b/src/fmi4cpp/library_helper.hpp @@ -3,7 +3,7 @@ #define FMI4CPP_LIBRARYHELPER_HPP #include - +#include #include namespace @@ -11,8 +11,28 @@ namespace DLL_HANDLE load_library(const std::string& libName) { -#ifdef WIN32 - return LoadLibrary(libName.c_str()); + std::string dllDirectory; + +#ifdef _WIN32 + fmi4cpp::fs::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; + #else return dlopen(libName.c_str(), RTLD_NOW | RTLD_LOCAL); #endif