Blue Brain BioExplorer
DynamicLib.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2024, EPFL/Blue Brain Project
3  * All rights reserved. Do not distribute without permission.
4  * Responsible Author: Juan Hernando <juan.hernando@epfl.ch>
5  *
6  * This file is part of Blue Brain BioExplorer <https://github.com/BlueBrain/BioExplorer>
7  *
8  * This library is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License version 3.0 as published
10  * by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #ifdef _WIN32
23 #ifndef WIN32_LEAN_AND_MEAN
24 #define WIN32_LEAN_AND_MEAN
25 #endif
26 #include <windows.h>
27 #else
28 #include <dlfcn.h>
29 #include <sys/times.h>
30 #endif
31 
32 #include "DynamicLib.h"
33 
34 namespace core
35 {
36 DynamicLib::DynamicLib(const std::string& name)
37 {
38  std::string file = name;
39  std::string errorMessage;
40 #ifdef _WIN32
41  std::string filename = name + ".dll";
42  _handler = LoadLibrary(fullName.c_str());
43  if (!_handler)
44  {
45  DWORD err = GetLastError();
46  LPTSTR buffer;
47  FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
48  err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&buffer, 0, NULL);
49  errorMessage = buffer;
50  LocalFree(buffer);
51  }
52 
53 #else
54 #if defined(__MACOSX__) || defined(__APPLE__)
55  std::string filename = "lib" + file + ".dylib";
56 #else
57  std::string filename = "lib" + file + ".so";
58 #endif
59  _handler = dlopen(filename.c_str(), RTLD_LAZY | RTLD_GLOBAL);
60  if (!_handler)
61  errorMessage = dlerror();
62 #endif
63  if (!_handler)
64  throw std::runtime_error("Error opening dynamic library: " + filename + ": " + errorMessage);
65 }
66 
68 {
69  if (!_handler)
70  return;
71 
72 #ifdef _WIN32
73  if (_handler != GetModuleHandle(0))
74  FreeLibrary(_handler);
75 #else
76  if (_handler != RTLD_DEFAULT)
77  dlclose(_handler);
78 #endif
79  _handler = 0;
80 }
81 
83 {
84  _handler = other._handler;
85  other._handler = nullptr;
86 }
87 
89 {
90  _handler = other._handler;
91  other._handler = nullptr;
92  return *this;
93 }
94 
95 void* DynamicLib::getSymbolAddress(const std::string& name) const
96 {
97  if (!_handler)
98  return nullptr;
99 
100 #ifdef _WIN32 //_MSC_VER
101  return GetProcAddress((HMODULE)_handler, name.c_str());
102 #else
103  return dlsym(_handler, name.c_str());
104 #endif
105 }
106 } // namespace core
void * getSymbolAddress(const std::string &name) const
Definition: DynamicLib.cpp:95
DynamicLib & operator=(DynamicLib &&other)
Definition: DynamicLib.cpp:88
DynamicLib(const std::string &name)
Definition: DynamicLib.cpp:36