2017-01-20 08:36:52 -05:00
|
|
|
#pragma once
|
|
|
|
|
2017-01-19 16:23:59 -05:00
|
|
|
namespace Utils
|
|
|
|
{
|
|
|
|
class Library
|
|
|
|
{
|
|
|
|
public:
|
2020-12-09 14:13:34 -05:00
|
|
|
Library() : _module(nullptr), freeOnDestroy(false) {};
|
2018-12-17 08:29:18 -05:00
|
|
|
Library(const std::string& buffer, bool freeOnDestroy = true);
|
2017-01-19 16:23:59 -05:00
|
|
|
~Library();
|
|
|
|
|
2021-07-18 11:36:01 -04:00
|
|
|
bool is_valid() const;
|
2017-01-19 16:23:59 -05:00
|
|
|
HMODULE getModule();
|
|
|
|
|
|
|
|
template <typename T>
|
2021-07-18 11:36:01 -04:00
|
|
|
T get_proc(const std::string& process) const
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
2021-07-18 11:36:01 -04:00
|
|
|
if (!this->is_valid()) T{};
|
|
|
|
return reinterpret_cast<T>(GetProcAddress(this->_module, process.data()));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::function<T> get(const std::string& process) const
|
|
|
|
{
|
|
|
|
if (!this->is_valid()) return std::function<T>();
|
|
|
|
return static_cast<T*>(this->get_proc<void*>(process));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename... Args>
|
|
|
|
T invoke(const std::string& process, Args ... args) const
|
|
|
|
{
|
|
|
|
auto method = this->get<T(__cdecl)(Args ...)>(process);
|
|
|
|
if (method) return method(args...);
|
|
|
|
return T();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename... Args>
|
|
|
|
T invoke_pascal(const std::string& process, Args ... args) const
|
|
|
|
{
|
|
|
|
auto method = this->get<T(__stdcall)(Args ...)>(process);
|
|
|
|
if (method) return method(args...);
|
|
|
|
return T();
|
|
|
|
}
|
2017-01-19 16:23:59 -05:00
|
|
|
|
2021-07-18 11:36:01 -04:00
|
|
|
template <typename T, typename... Args>
|
|
|
|
T invoke_this(const std::string& process, void* this_ptr, Args ... args) const
|
|
|
|
{
|
|
|
|
auto method = this->get<T(__thiscall)(void*, Args ...)>(this_ptr, process);
|
|
|
|
if (method) return method(args...);
|
|
|
|
return T();
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|
|
|
|
|
2017-03-26 12:00:06 -04:00
|
|
|
void free();
|
|
|
|
|
2017-01-19 16:23:59 -05:00
|
|
|
private:
|
2020-12-09 14:13:34 -05:00
|
|
|
HMODULE _module;
|
2017-01-19 16:23:59 -05:00
|
|
|
bool freeOnDestroy;
|
|
|
|
};
|
|
|
|
}
|