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 <string>
17
18namespace Mantid::Kernel {
19
20namespace {
21// Static logger object
22Logger g_log("DllOpen");
23} // namespace
24
25// -----------------------------------------------------------------------------
26// Windows-specific implementations
27// -----------------------------------------------------------------------------
28#if defined(_WIN32)
29
30const std::string LIB_SUFFIX = ".dll";
31
37bool DllOpen::isValidFilename(const std::string &filename) { return filename.ends_with(LIB_SUFFIX); }
38
39/* Opens the Windows .dll file.
40 * @param filePath :: Filepath of the library.
41 * @return Pointer to library (of type void).
42 **/
43void *DllOpen::openDll(const std::string &filePath) {
44 void *handle = LoadLibrary(filePath.c_str());
45 if (!handle) {
46
47 LPVOID lpMsgBuf;
48 LPVOID lpDisplayBuf;
49 DWORD dw = GetLastError();
50
51 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw,
52 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
53
54 // Display the error message and exit the process
55 size_t n = lstrlen((LPCTSTR)lpMsgBuf) + 40;
56
57 lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, n * sizeof(TCHAR));
58 _snprintf((char *)lpDisplayBuf, n, "failed with error %lu: %s", dw, (char *)lpMsgBuf);
59 g_log.error() << "Could not open library " << filePath << ": " << (LPCTSTR)lpDisplayBuf << '\n';
60
61 LocalFree(lpMsgBuf);
62 LocalFree(lpDisplayBuf);
63 }
64 return handle;
65}
66
67/* Closes an open .dll file.
68 * @param handle :: A handle to the open library.
69 **/
70void DllOpen::closeDll(void *handle) { FreeLibrary((HINSTANCE)handle); }
71
72#else
73
74const std::string LIB_PREFIX = "lib";
75#ifdef __linux__
76const std::string LIB_SUFFIX = ".so";
77#elif defined __APPLE__
78const std::string LIB_SUFFIX = ".dylib";
79#endif
80
86bool DllOpen::isValidFilename(const std::string &filename) {
87 return filename.starts_with(LIB_PREFIX) && filename.ends_with(LIB_SUFFIX);
88}
89
90/* Opens the Linux .so file
91 * @param filePath :: Filepath of the library.
92 * @return Pointer to library (of type void).
93 **/
94void *DllOpen::openDll(const std::string &filepath) {
95 void *handle = dlopen(filepath.c_str(), RTLD_NOW | RTLD_GLOBAL);
96 if (!handle) {
97 g_log.error("Could not open library " + filepath + ": " + dlerror());
98 }
99 return handle;
100}
101
102/* Closes an open .so file.
103 * @param handle :: A handle to the open library.
104 **/
105void DllOpen::closeDll(void *handle) {
106 UNUSED_ARG(handle);
107 // A bug in glibc prevents us from calling this.
108 // dlclose(handle);
109}
110
111#endif /* _WIN32 */
112
113} // namespace Mantid::Kernel
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition System.h:48
static bool isValidFilename(const std::string &filename)
Check if the filename conforms to the expected style for this platform.
Definition DllOpen.cpp:86
static void * openDll(const std::string &filepath)
Static method for opening the shared library.
Definition DllOpen.cpp:94
static void closeDll(void *handle)
Static method for closing the shared library.
Definition DllOpen.cpp:105
void error(const std::string &msg)
Logs at error level.
Definition Logger.cpp:108
const std::string LIB_PREFIX
Definition DllOpen.cpp:74