Skip to content

Commit f9e937f

Browse files
committed
Fixed: loading DLL dependencies for Windows
For finding the module dependencies added the directory of loaded library
1 parent e53e45b commit f9e937f

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

projects/msvs/httpserver.v12.suo

512 Bytes
Binary file not shown.

src/Module.cpp

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11

22
#include "Module.h"
33

4-
#include <iostream>
5-
64
namespace HttpServer
75
{
86
Module::Module(): lib_handle(nullptr)
@@ -33,7 +31,35 @@ namespace HttpServer
3331
}
3432

3533
#ifdef WIN32
36-
lib_handle = ::LoadLibrary(libPath.c_str() );
34+
const size_t pos_slash = libPath.rfind('\\');
35+
const size_t pos_slash_back = libPath.rfind('/');
36+
37+
size_t pos = std::string::npos;
38+
39+
if (pos_slash != std::string::npos && pos_slash > pos_slash_back)
40+
{
41+
pos = pos_slash;
42+
}
43+
else if (pos_slash_back != std::string::npos)
44+
{
45+
pos = pos_slash_back;
46+
}
47+
48+
DLL_DIRECTORY_COOKIE cookie = nullptr;
49+
50+
if (std::string::npos != pos)
51+
{
52+
std::wstring directory(libPath.cbegin(), libPath.cbegin() + pos + 1);
53+
54+
cookie = ::AddDllDirectory(directory.data() );
55+
}
56+
57+
lib_handle = ::LoadLibraryEx(libPath.c_str(), 0, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
58+
59+
if (cookie)
60+
{
61+
::RemoveDllDirectory(cookie);
62+
}
3763
#elif POSIX
3864
lib_handle = ::dlopen(libPath.c_str(), RTLD_NOW | RTLD_LOCAL);
3965
#else

0 commit comments

Comments
 (0)