2023-05-31 21:04:01 +00:00
|
|
|
#ifndef DLHANDLE_H
|
|
|
|
#define DLHANDLE_H
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <string>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <utility>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Dlhandle {
|
|
|
|
void *chandle;
|
|
|
|
|
|
|
|
public:
|
|
|
|
class Exception : public std::runtime_error {
|
|
|
|
public:
|
|
|
|
using std::runtime_error::runtime_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
Dlhandle() : chandle(nullptr) {}
|
2023-06-13 18:52:11 +00:00
|
|
|
Dlhandle(const std::string& fpath, int flags = RTLD_LAZY | RTLD_LOCAL) {
|
2023-05-31 21:04:01 +00:00
|
|
|
chandle = dlopen(fpath.c_str(), flags);
|
|
|
|
if (!chandle) {
|
|
|
|
throw Exception("dlopen(\""+fpath+"\"): "+dlerror());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Dlhandle(const Dlhandle& o) = delete;
|
|
|
|
Dlhandle(Dlhandle&& o) : chandle(o.chandle) {
|
|
|
|
o.chandle = nullptr;
|
|
|
|
}
|
|
|
|
~Dlhandle() {
|
|
|
|
if (chandle) dlclose(chandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto operator =(Dlhandle&& o) {
|
|
|
|
chandle = std::exchange(o.chandle, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_valid() const {
|
|
|
|
return chandle != nullptr;
|
|
|
|
}
|
|
|
|
operator bool() const {
|
|
|
|
return is_valid();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-05-31 19:37:25 +00:00
|
|
|
T* get(const std::string& fname) const {
|
2023-05-31 21:04:01 +00:00
|
|
|
auto fres = reinterpret_cast<T*>(dlsym(chandle, fname.c_str()));
|
|
|
|
return (dlerror()==NULL)?fres:nullptr;
|
|
|
|
}
|
2023-05-31 19:37:25 +00:00
|
|
|
auto get_fnc(const std::string& fname) const {
|
2023-05-31 21:04:01 +00:00
|
|
|
return get<void*(...)>(fname);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
#include <string>
|
|
|
|
#include <exception>
|
|
|
|
#include <stdexcept>
|
2023-06-01 15:26:55 +00:00
|
|
|
#ifndef NOMINMAX
|
|
|
|
#define NOMINMAX
|
|
|
|
#endif
|
2023-05-31 21:04:01 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <libloaderapi.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Dlhandle {
|
|
|
|
HMODULE chandle;
|
|
|
|
|
|
|
|
public:
|
|
|
|
class Exception : public std::runtime_error {
|
|
|
|
public:
|
|
|
|
using std::runtime_error::runtime_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
Dlhandle() : chandle(nullptr) {}
|
|
|
|
Dlhandle(const std::string& fpath) {
|
2023-08-30 22:10:20 +00:00
|
|
|
chandle = LoadLibraryExA(fpath.c_str(), NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR);
|
2023-05-31 21:04:01 +00:00
|
|
|
if (!chandle) {
|
|
|
|
throw Exception("dlopen(\""+fpath+"\"): Error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Dlhandle(const Dlhandle& o) = delete;
|
|
|
|
Dlhandle(Dlhandle&& o) : chandle(o.chandle) {
|
|
|
|
o.chandle = nullptr;
|
|
|
|
}
|
|
|
|
~Dlhandle() {
|
|
|
|
if (chandle) FreeLibrary(chandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_valid() const {
|
|
|
|
return chandle != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-05-31 19:37:25 +00:00
|
|
|
T* get(const std::string& fname) const {
|
2023-05-31 21:04:01 +00:00
|
|
|
return reinterpret_cast<T*>(GetProcAddress(chandle, fname.c_str()));
|
|
|
|
}
|
2023-05-31 19:37:25 +00:00
|
|
|
auto get_fnc(const std::string& fname) const {
|
2023-05-31 21:04:01 +00:00
|
|
|
return get<void*(...)>(fname);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
#endif // DLHANDLE_H
|