forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DllOpen.cpp
115 lines (93 loc) · 3.39 KB
/
DllOpen.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
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidKernel/DllOpen.h"
#include "MantidKernel/Logger.h"
#if _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif
#include <boost/algorithm/string/predicate.hpp>
#include <string>
namespace Mantid::Kernel {
namespace {
// Static logger object
Logger g_log("DllOpen");
} // namespace
// -----------------------------------------------------------------------------
// Windows-specific implementations
// -----------------------------------------------------------------------------
#if defined(_WIN32)
const std::string LIB_SUFFIX = ".dll";
/**
* Does the file have the expected form for this platform
* @param filename The file name of the library
* @return True if it matches the expected format, false otherwise
*/
bool DllOpen::isValidFilename(const std::string &filename) { return boost::ends_with(filename, LIB_SUFFIX); }
/* Opens the Windows .dll file.
* @param filePath :: Filepath of the library.
* @return Pointer to library (of type void).
**/
void *DllOpen::openDll(const std::string &filePath) {
void *handle = LoadLibrary(filePath.c_str());
if (!handle) {
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
// Display the error message and exit the process
size_t n = lstrlen((LPCTSTR)lpMsgBuf) + 40;
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, n * sizeof(TCHAR));
_snprintf((char *)lpDisplayBuf, n, "failed with error %lu: %s", dw, (char *)lpMsgBuf);
g_log.error() << "Could not open library " << filePath << ": " << (LPCTSTR)lpDisplayBuf << '\n';
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}
return handle;
}
/* Closes an open .dll file.
* @param handle :: A handle to the open library.
**/
void DllOpen::closeDll(void *handle) { FreeLibrary((HINSTANCE)handle); }
#else
const std::string LIB_PREFIX = "lib";
#ifdef __linux__
const std::string LIB_SUFFIX = ".so";
#elif defined __APPLE__
const std::string LIB_SUFFIX = ".dylib";
#endif
/**
* Does the file have the expected form for this platform
* @param filename The file name of the library
* @return True if it matches the expected format, false otherwise
*/
bool DllOpen::isValidFilename(const std::string &filename) {
return boost::starts_with(filename, LIB_PREFIX) && boost::ends_with(filename, LIB_SUFFIX);
}
/* Opens the Linux .so file
* @param filePath :: Filepath of the library.
* @return Pointer to library (of type void).
**/
void *DllOpen::openDll(const std::string &filepath) {
void *handle = dlopen(filepath.c_str(), RTLD_NOW | RTLD_GLOBAL);
if (!handle) {
g_log.error("Could not open library " + filepath + ": " + dlerror());
}
return handle;
}
/* Closes an open .so file.
* @param handle :: A handle to the open library.
**/
void DllOpen::closeDll(void *handle) {
UNUSED_ARG(handle);
// A bug in glibc prevents us from calling this.
// dlclose(handle);
}
#endif /* _WIN32 */
} // namespace Mantid::Kernel