mCRL2
Loading...
Searching...
No Matches
dynamiclibrary.h
Go to the documentation of this file.
1// Author(s): Jeroen Keiren, Sjoerd Cranen
2// Copyright: see the accompanying file COPYING or copy at
3// https://github.com/mCRL2org/mCRL2/blob/master/COPYING
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
10
11
12#ifndef MCRL2_UTILITIES_DYNAMIC_LIBRARY_H
13#define MCRL2_UTILITIES_DYNAMIC_LIBRARY_H
14
17
18#ifdef MCRL2_PLATFORM_WINDOWS
19 #include <windows.h>
20 typedef HMODULE library_handle;
21 typedef FARPROC library_proc;
22
23 inline
24 library_handle get_module_handle(const std::string& fname)
25 {
26 return LoadLibrary(fname.c_str());
27 }
28
29 inline
30 library_proc get_proc_address(library_handle handle, const std::string& procname)
31 {
32 return GetProcAddress(handle, procname.c_str());
33 }
34
35 inline
37 {
38 return FreeLibrary(handle);
39 }
40
41 inline
42 std::string get_last_error()
43 {
44 std::string result;
45 char *buffer = 0;
46 DWORD last = GetLastError();
47 DWORD x = FormatMessage(
48 FORMAT_MESSAGE_ALLOCATE_BUFFER | // Let Windows allocate string
49 FORMAT_MESSAGE_FROM_SYSTEM, // Retrieve a system error message
50 nullptr, // No source needed
51 last, // Requested error message
52 0, // Default language
53 reinterpret_cast<LPSTR>(&buffer), // Output buffer
54 0, // Minimum allocation size
55 nullptr // No arguments here
56 );
57 if (x)
58 {
59 result = buffer;
60 }
61 else
62 {
63 result = "Unknown error.";
64 }
65 LocalFree(buffer);
66 return result;
67 }
68#else
69 #include <dlfcn.h>
70 typedef void* library_handle;
71 typedef void* library_proc;
72
73 inline
74 library_handle get_module_handle(const std::string& fname)
75 {
76 // return dlopen(fname.c_str(), RTLD_LAZY);
77 return dlopen(fname.c_str(), RTLD_NOW);
78 }
79
80 inline
81 library_proc get_proc_address(library_handle handle, const std::string& procname)
82 {
83 return dlsym(handle, procname.c_str());
84 }
85
86 inline
88 {
89 return (dlclose(handle) == 0);
90 }
91
92 inline
93 std::string get_last_error()
94 {
95 return std::string(dlerror());
96 }
97#endif
98
100{
101 protected:
103 std::string m_filename;
104
105 void load()
106 {
107 if (m_library == nullptr)
108 {
110 if (m_library == nullptr)
111 {
112 throw std::runtime_error("Could not load library (" + m_filename + "): " + get_last_error());
113 }
114 }
115 }
116
117 void unload()
118 {
119 if (m_library!=nullptr)
120 {
122 {
123 throw std::runtime_error("Could not close library (" + m_filename + "): " + get_last_error());
124 }
125 m_library = nullptr;
126 }
127 }
128
129 public:
130 dynamic_library(const std::string& filename = std::string())
131 : m_library(nullptr),
132 m_filename(filename)
133 {
134 }
135
137 {
138 try
139 {
140 unload();
141 }
142 catch(std::runtime_error& error)
143 {
144 mCRL2log(mcrl2::log::error) << "Error while unloading dynamic library: " << error.what() << std::endl;
145 }
146 }
147
148 library_proc proc_address(const std::string& name)
149 {
150 if (m_library == nullptr)
151 {
152 load();
153 }
154 library_proc result = get_proc_address(m_library, name.c_str());
155 if (result == 0)
156 {
157 throw std::runtime_error("Could not find proc address (" + m_filename + ":" + name + "): " + get_last_error());
158 }
159 return result;
160 }
161};
162
163#endif // MCRL2_UTILITIES_DYNAMIC_LIBRARY_H
std::string m_filename
library_proc proc_address(const std::string &name)
virtual ~dynamic_library()
dynamic_library(const std::string &filename=std::string())
library_handle m_library
void * library_proc
std::string get_last_error()
library_proc get_proc_address(library_handle handle, const std::string &procname)
bool close_module_handle(library_handle handle)
void * library_handle
library_handle get_module_handle(const std::string &fname)
@ error
Definition linearise.cpp:68
#define mCRL2log(LEVEL)
mCRL2log(LEVEL) provides the stream used to log.
Definition logger.h:391
const function_symbol & last()
Constructor for function symbol @last.
Definition nat1.h:1853