iw4x-client/src/Utils/Library.cpp

67 lines
1.2 KiB
C++
Raw Normal View History

2017-01-19 16:23:59 -05:00
#include "STDInclude.hpp"
namespace Utils
{
2021-07-18 12:11:20 -04:00
Library Library::load(const std::string& name)
{
return Library(LoadLibraryA(name.data()));
}
Library Library::load(const std::filesystem::path& path)
{
return Library::load(path.generic_string());
}
Library Library::get_by_address(void* address)
{
HMODULE handle = nullptr;
GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, static_cast<LPCSTR>(address), &handle);
return Library(handle);
}
2020-12-09 14:13:34 -05:00
Library::Library(const std::string& buffer, bool _freeOnDestroy) : _module(nullptr), freeOnDestroy(_freeOnDestroy)
2017-01-19 16:23:59 -05:00
{
2020-12-09 14:13:34 -05:00
this->_module = LoadLibraryExA(buffer.data(), nullptr, 0);
2017-01-19 16:23:59 -05:00
}
2021-07-18 12:11:20 -04:00
Library::Library(const std::string& buffer)
{
this->_module = GetModuleHandleA(buffer.data());
this->freeOnDestroy = true;
}
Library::Library(const HMODULE handle)
{
this->_module = handle;
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
{
2017-03-26 12:00:06 -04:00
this->free();
2017-01-19 16:23:59 -05:00
}
}
2021-07-18 11:36:01 -04:00
bool Library::is_valid() const
2017-01-19 16:23:59 -05:00
{
2021-07-18 11:36:01 -04:00
return this->_module != nullptr;
2017-01-19 16:23:59 -05:00
}
HMODULE Library::getModule()
{
2020-12-09 14:13:34 -05:00
return this->_module;
2017-01-19 16:23:59 -05:00
}
2017-03-26 12:00:06 -04:00
void Library::free()
{
2021-07-18 11:36:01 -04:00
if (this->is_valid())
2017-03-26 12:00:06 -04:00
{
FreeLibrary(this->getModule());
}
2020-12-09 14:13:34 -05:00
this->_module = nullptr;
2017-03-26 12:00:06 -04:00
}
2017-01-19 16:23:59 -05:00
}