2022-02-27 07:53:44 -05:00
|
|
|
#include <STDInclude.hpp>
|
2017-01-19 16:23:59 -05:00
|
|
|
|
|
|
|
namespace Utils
|
|
|
|
{
|
2021-07-21 12:44:58 -04:00
|
|
|
Library Library::Load(const std::string& name)
|
2021-07-18 12:11:20 -04:00
|
|
|
{
|
|
|
|
return Library(LoadLibraryA(name.data()));
|
|
|
|
}
|
|
|
|
|
2021-07-21 12:44:58 -04:00
|
|
|
Library Library::Load(const std::filesystem::path& path)
|
2021-07-18 12:11:20 -04:00
|
|
|
{
|
2021-07-21 12:44:58 -04:00
|
|
|
return Library::Load(path.generic_string());
|
2021-07-18 12:11:20 -04:00
|
|
|
}
|
|
|
|
|
2021-07-21 12:44:58 -04:00
|
|
|
Library Library::GetByAddress(void* address)
|
2021-07-18 12:11:20 -04:00
|
|
|
{
|
|
|
|
HMODULE handle = nullptr;
|
|
|
|
GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, static_cast<LPCSTR>(address), &handle);
|
|
|
|
return Library(handle);
|
|
|
|
}
|
|
|
|
|
2022-05-05 10:03:14 -04:00
|
|
|
Library::Library(const std::string& name, bool _freeOnDestroy) : module_(nullptr), freeOnDestroy(_freeOnDestroy)
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
2022-05-05 10:03:14 -04:00
|
|
|
this->module_ = LoadLibraryExA(name.data(), nullptr, 0);
|
2021-07-18 12:11:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Library::Library(const HMODULE handle)
|
|
|
|
{
|
2022-05-05 10:03:14 -04:00
|
|
|
this->module_ = handle;
|
2021-07-18 12:11:20 -04:00
|
|
|
this->freeOnDestroy = true;
|
|
|
|
}
|
|
|
|
|
2017-01-19 16:23:59 -05:00
|
|
|
Library::~Library()
|
|
|
|
{
|
2017-03-26 12:00:06 -04:00
|
|
|
if (this->freeOnDestroy)
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
2021-08-19 05:45:40 -04:00
|
|
|
this->free();
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 10:03:14 -04:00
|
|
|
bool Library::operator==(const Library& obj) const
|
|
|
|
{
|
|
|
|
return this->module_ == obj.module_;
|
|
|
|
}
|
|
|
|
|
|
|
|
Library::operator bool() const
|
|
|
|
{
|
|
|
|
return this->isValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
Library::operator HMODULE() const
|
|
|
|
{
|
|
|
|
return this->getModule();
|
|
|
|
}
|
|
|
|
|
2021-08-19 05:45:40 -04:00
|
|
|
bool Library::isValid() const
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
2022-05-05 10:03:14 -04:00
|
|
|
return this->module_ != nullptr;
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|
|
|
|
|
2022-03-20 05:05:45 -04:00
|
|
|
HMODULE Library::getModule() const
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
2022-05-05 10:03:14 -04:00
|
|
|
return this->module_;
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|
2017-03-26 12:00:06 -04:00
|
|
|
|
2021-08-19 05:45:40 -04:00
|
|
|
void Library::free()
|
2017-03-26 12:00:06 -04:00
|
|
|
{
|
2021-08-19 05:45:40 -04:00
|
|
|
if (this->isValid())
|
2017-03-26 12:00:06 -04:00
|
|
|
{
|
2022-05-05 10:03:14 -04:00
|
|
|
FreeLibrary(this->module_);
|
2017-03-26 12:00:06 -04:00
|
|
|
}
|
|
|
|
|
2022-05-05 10:03:14 -04:00
|
|
|
this->module_ = nullptr;
|
2017-03-26 12:00:06 -04:00
|
|
|
}
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|