Mantid
Loading...
Searching...
No Matches
DllOpen.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
9
10#if _WIN32
11#include <windows.h>
12#else
13#include <dlfcn.h>
14#endif
15
16#include <boost/algorithm/string/predicate.hpp>
17
18#include <string>
19
20namespace Mantid::Kernel {
21
22namespace {
23// Static logger object
24Logger g_log("DllOpen");
25} // namespace
26
27// -----------------------------------------------------------------------------
28// Windows-specific implementations
29// -----------------------------------------------------------------------------
30#if defined(_WIN32)
31
32const std::string LIB_SUFFIX = ".dll";
33
39bool DllOpen::isValidFilename(const std::string &filename) { return boost::ends_with(filename, LIB_SUFFIX); }
40
41/* Opens the Windows .dll file.
42 * @param filePath :: Filepath of the library.
43 * @return Pointer to library (of type void).
44 **/
45void *DllOpen::openDll(const std::string &filePath) {
46 void *handle = LoadLibrary(filePath.c_str());
47 if (!handle) {
48
49 LPVOID lpMsgBuf;
50 LPVOID lpDisplayBuf;
51 DWORD dw = GetLastError();
52
53 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw,
54 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
55
56 // Display the error message and exit the process
57 size_t n = lstrlen((LPCTSTR)lpMsgBuf) + 40;
58
59 lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, n * sizeof(TCHAR));
60 _snprintf((char *)lpDisplayBuf, n, "failed with error %lu: %s", dw, (char *)lpMsgBuf);
61 g_log.error() << "Could not open library " << filePath << ": " << (LPCTSTR)lpDisplayBuf << '\n';
62
63 LocalFree(lpMsgBuf);
64 LocalFree(lpDisplayBuf);
65 }
66 return handle;
67}
68
69/* Closes an open .dll file.
70 * @param handle :: A handle to the open library.
71 **/
72void DllOpen::closeDll(void *handle) { FreeLibrary((HINSTANCE)handle); }
73
74#else
75
76const std::string LIB_PREFIX = "lib";
77#ifdef __linux__
78const std::string LIB_SUFFIX = ".so";
79#elif defined __APPLE__
80const std::string LIB_SUFFIX = ".dylib";
81#endif
82
88bool DllOpen::isValidFilename(const std::string &filename) {
89 return boost::starts_with(filename, LIB_PREFIX) && boost::ends_with(filename, LIB_SUFFIX);
90}
91
92/* Opens the Linux .so file
93 * @param filePath :: Filepath of the library.
94 * @return Pointer to library (of type void).
95 **/
96void *DllOpen::openDll(const std::string &filepath) {
97 void *handle = dlopen(filepath.c_str(), RTLD_NOW | RTLD_GLOBAL);
98 if (!handle) {
99 g_log.error("Could not open library " + filepath + ": " + dlerror());
100 }
101 return handle;
102}
103
104/* Closes an open .so file.
105 * @param handle :: A handle to the open library.
106 **/
107void DllOpen::closeDll(void *handle) {
108 UNUSED_ARG(handle);
109 // A bug in glibc prevents us from calling this.
110 // dlclose(handle);
111}
112
113#endif /* _WIN32 */
114
115} // namespace Mantid::Kernel
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition: System.h:64
static bool isValidFilename(const std::string &filename)
Check if the filename conforms to the expected style for this platform.
Definition: DllOpen.cpp:88
static void * openDll(const std::string &filepath)
Static method for opening the shared library.
Definition: DllOpen.cpp:96
static void closeDll(void *handle)
Static method for closing the shared library.
Definition: DllOpen.cpp:107
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
const std::string LIB_PREFIX
Definition: DllOpen.cpp:76