Newer
Older
#include "MantidKernel/DllOpen.h"
#include "MantidKernel/Logger.h"
Roman Tolchenov
committed
#define _WIN32_WINNT 0x0510
#include <windows.h>
#else
#include <dlfcn.h>
#endif /* _WIN32 */
#include <boost/algorithm/string/predicate.hpp>
namespace Mantid {
namespace Kernel {
namespace {
// Static logger object
Logger g_log("DllOpen");
}
// -----------------------------------------------------------------------------
// Windows-specific implementations
// -----------------------------------------------------------------------------
#if defined(_WIN32)
/**
* 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.
Janik Zikovsky
committed
* @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) {
Russell Taylor
committed
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);
Russell Taylor
committed
// 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,
g_log.error() << "Could not open library " << filePath << ": "
<< (LPCTSTR)lpDisplayBuf << '\n';
Russell Taylor
committed
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}
return handle;
* @param handle :: A handle to the open library.
void DllOpen::closeDll(void *handle) { FreeLibrary((HINSTANCE)handle); }
/**
* 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);
}
Janik Zikovsky
committed
* @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);
g_log.error("Could not open library " + filepath + ": " + dlerror());
Russell Taylor
committed
}
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 Kernel
} // namespace Mantid