/
DynamicLibrary.cpp
125 lines (102 loc) · 2.64 KB
/
DynamicLibrary.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "DynamicLibrary.h"
#include "string/encoding.h"
#include "itextstream.h"
namespace module
{
/**
* =============================== WIN32 ======================================
*/
#if defined(WIN32)
#define FORMAT_BUFSIZE 2048
// Helper method to retrieve the error when DLL load failed.
std::string FormatGetLastError()
{
static wchar_t buf[FORMAT_BUFSIZE];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
buf,
FORMAT_BUFSIZE, NULL);
return string::unicode_to_utf8(buf);
}
DynamicLibrary::DynamicLibrary(const std::string& filename) :
_name(filename.begin(), filename.end()),
_library(LoadLibrary(_name.c_str()))
{
if (_library == 0)
{
rError() << "LoadLibrary failed: '" << filename << "'" << std::endl;
rError() << "GetLastError: " << FormatGetLastError();
}
}
DynamicLibrary::~DynamicLibrary() {
if (!failed()) {
FreeLibrary(_library);
}
}
bool DynamicLibrary::failed() {
return _library == 0;
}
DynamicLibrary::FunctionPointer DynamicLibrary::findSymbol(const std::string& symbol) {
// Try to lookup the symbol address
FunctionPointer address = GetProcAddress(_library, symbol.c_str());
// Emit a warning if the lookup failed
if (address == 0) {
rConsoleError() << "GetProcAddress failed: '" << symbol << "'" << std::endl;
rConsoleError() << "GetLastError: " << FormatGetLastError();
}
return address;
}
std::string DynamicLibrary::getName() const
{
return string::unicode_to_utf8(_name);
}
/**
* =============================== POSIX ======================================
*/
#elif defined(POSIX)
DynamicLibrary::DynamicLibrary(const std::string& filename) :
_name(filename),
_dlHandle(dlopen(_name.c_str(), RTLD_NOW)) // load the file using the OS function dlopen
{
if (_dlHandle == nullptr)
{
const char* msg = dlerror();
rConsoleError() << "Error opening library: " << msg << std::endl;
}
}
DynamicLibrary::~DynamicLibrary()
{
if (!failed())
{
dlclose(_dlHandle);
}
}
bool DynamicLibrary::failed()
{
return _dlHandle == nullptr;
}
// Find a symbol in the library
DynamicLibrary::FunctionPointer DynamicLibrary::findSymbol(const std::string& symbol)
{
// Try to obtain the function pointer and cast it to a void* pointer
FunctionPointer p = reinterpret_cast<FunctionPointer>(
dlsym(_dlHandle, symbol.c_str())
);
if (p == nullptr)
{
const char* error = dlerror();
if (error != nullptr)
{
rConsoleError() << error << std::endl;
}
}
return p;
}
std::string DynamicLibrary::getName() const
{
return _name;
}
#else
#error "unsupported platform"
#endif
}