Skip to content

Commit

Permalink
add dll folder as loading library search path (#145)
Browse files Browse the repository at this point in the history
Co-authored-by: Wenweil1 <[email protected]>
  • Loading branch information
WeilongWen and WeilongWen authored Sep 6, 2024
1 parent 638ff47 commit 420d0be
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/fmi4cpp/library_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,36 @@
#define FMI4CPP_LIBRARYHELPER_HPP

#include <fmi4cpp/dll_handle.hpp>

#include <fmi4cpp/fs_portability.hpp>
#include <sstream>

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
Expand Down

0 comments on commit 420d0be

Please sign in to comment.