2019-06-16 15:11:13 +00:00
|
|
|
|
#include "stdafx.h"
|
2016-05-08 07:38:40 +00:00
|
|
|
|
#include "dynamic_library.h"
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
#include <Windows.h>
|
|
|
|
|
#else
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
namespace utils
|
|
|
|
|
{
|
2019-11-28 18:18:37 +00:00
|
|
|
|
dynamic_library::dynamic_library(const std::string& path)
|
2016-05-08 07:38:40 +00:00
|
|
|
|
{
|
|
|
|
|
load(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dynamic_library::~dynamic_library()
|
|
|
|
|
{
|
|
|
|
|
close();
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-28 18:18:37 +00:00
|
|
|
|
bool dynamic_library::load(const std::string& path)
|
2016-05-08 07:38:40 +00:00
|
|
|
|
{
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
m_handle = LoadLibraryA(path.c_str());
|
|
|
|
|
#else
|
|
|
|
|
m_handle = dlopen(path.c_str(), RTLD_LAZY);
|
|
|
|
|
#endif
|
|
|
|
|
return loaded();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void dynamic_library::close()
|
|
|
|
|
{
|
|
|
|
|
#ifdef _WIN32
|
2019-11-28 18:18:37 +00:00
|
|
|
|
FreeLibrary(reinterpret_cast<HMODULE>(m_handle));
|
2016-05-08 07:38:40 +00:00
|
|
|
|
#else
|
|
|
|
|
dlclose(m_handle);
|
|
|
|
|
#endif
|
|
|
|
|
m_handle = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-28 18:18:37 +00:00
|
|
|
|
void* dynamic_library::get_impl(const std::string& name) const
|
2016-05-08 07:38:40 +00:00
|
|
|
|
{
|
|
|
|
|
#ifdef _WIN32
|
2019-11-28 18:18:37 +00:00
|
|
|
|
return reinterpret_cast<void*>(GetProcAddress(reinterpret_cast<HMODULE>(m_handle), name.c_str()));
|
2016-05-08 07:38:40 +00:00
|
|
|
|
#else
|
2019-11-28 18:18:37 +00:00
|
|
|
|
return dlsym(m_handle, name.c_str());
|
2016-05-08 07:38:40 +00:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool dynamic_library::loaded() const
|
|
|
|
|
{
|
2019-06-16 15:11:13 +00:00
|
|
|
|
return m_handle != nullptr;
|
2016-05-08 07:38:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dynamic_library::operator bool() const
|
|
|
|
|
{
|
|
|
|
|
return loaded();
|
|
|
|
|
}
|
2017-01-24 13:52:15 +00:00
|
|
|
|
|
|
|
|
|
void* get_proc_address(const char* lib, const char* name)
|
|
|
|
|
{
|
|
|
|
|
#ifdef _WIN32
|
2017-03-05 16:00:08 +00:00
|
|
|
|
return reinterpret_cast<void*>(GetProcAddress(GetModuleHandleA(lib), name));
|
2017-01-24 13:52:15 +00:00
|
|
|
|
#else
|
|
|
|
|
return dlsym(dlopen(lib, RTLD_NOLOAD), name);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2016-05-08 07:38:40 +00:00
|
|
|
|
}
|