Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dll folder as loading library search path #145

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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