10#include <system_error>
22 const DWORD code = GetLastError();
23 return std::format(
"{} (error {})", std::system_category().message(
static_cast<int>(code)), code);
26static void*
OpenLibrary(
const std::filesystem::path& path)
28 return LoadLibraryW(path.c_str());
31static void*
FindExport(
void* handle,
const char* name)
33 return reinterpret_cast<void*
>(GetProcAddress(
static_cast<HMODULE
>(handle), name));
38 FreeLibrary(
static_cast<HMODULE
>(handle));
45 const char* message = dlerror();
46 return message !=
nullptr ? std::string(message) : std::string(
"no message");
52 return dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
57 return dlsym(handle, name);
79 _handle = std::exchange(other._handle,
nullptr);
87 if (handle ==
nullptr)
97 if (_handle ==
nullptr)
99 return std::unexpected(
Error::NotReady(std::format(
"cannot look up {}: nothing is loaded", name)));
103 if (address ==
nullptr)
111void SharedLibrary::Close()
113 if (_handle ==
nullptr)
static Result< SharedLibrary > Open(const std::filesystem::path &path)
Result< void * > Symbol(const char *name) const
SharedLibrary & operator=(SharedLibrary &&other) noexcept
static std::string LastError()
static void * OpenLibrary(const std::filesystem::path &path)
std::expected< T, Error > Result
static void * FindExport(void *handle, const char *name)
static void CloseLibrary(void *handle)
static Error Failed(std::string detail)
static Error NotFound(std::string detail)
static Error NotReady(std::string detail)