2017-01-20 08:36:52 -05:00
|
|
|
#pragma once
|
|
|
|
|
2017-01-19 16:23:59 -05:00
|
|
|
namespace Utils
|
|
|
|
{
|
|
|
|
class Library
|
|
|
|
{
|
|
|
|
public:
|
2021-07-21 12:44:58 -04:00
|
|
|
static Library Load(const std::filesystem::path& path);
|
|
|
|
static Library GetByAddress(void* address);
|
2021-07-18 12:11:20 -04:00
|
|
|
|
2022-08-02 07:24:22 -04:00
|
|
|
Library() : module_(nullptr), freeOnDestroy_(false) {}
|
2022-04-07 04:12:52 -04:00
|
|
|
Library(const std::string& name, bool freeOnDestroy);
|
2022-08-02 07:24:22 -04:00
|
|
|
explicit Library(const std::string& name) : module_(GetModuleHandleA(name.data())), freeOnDestroy_(true) {}
|
2021-07-18 12:11:20 -04:00
|
|
|
explicit Library(HMODULE handle);
|
2017-01-19 16:23:59 -05:00
|
|
|
~Library();
|
|
|
|
|
2022-05-05 10:03:14 -04:00
|
|
|
bool operator!=(const Library& obj) const { return !(*this == obj); }
|
|
|
|
bool operator==(const Library& obj) const;
|
|
|
|
|
|
|
|
operator bool() const;
|
|
|
|
operator HMODULE() const;
|
|
|
|
|
2022-11-21 19:34:17 -05:00
|
|
|
[[nodiscard]] bool isValid() const;
|
|
|
|
[[nodiscard]] HMODULE getModule() const;
|
|
|
|
[[nodiscard]] std::string getName() const;
|
|
|
|
[[nodiscard]] std::filesystem::path getPath() const;
|
|
|
|
[[nodiscard]] std::filesystem::path getFolder() const;
|
2022-08-02 07:24:22 -04:00
|
|
|
void free();
|
2017-01-19 16:23:59 -05:00
|
|
|
|
|
|
|
template <typename T>
|
2022-11-21 19:34:17 -05:00
|
|
|
[[nodiscard]] T getProc(const std::string& process) const
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
2021-08-19 05:45:40 -04:00
|
|
|
if (!this->isValid()) T{};
|
2022-05-05 10:03:14 -04:00
|
|
|
return reinterpret_cast<T>(GetProcAddress(this->module_, process.data()));
|
2021-07-18 11:36:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2022-11-21 19:34:17 -05:00
|
|
|
[[nodiscard]] std::function<T> get(const std::string& process) const
|
2021-07-18 11:36:01 -04:00
|
|
|
{
|
2021-08-19 05:45:40 -04:00
|
|
|
if (!this->isValid()) return std::function<T>();
|
|
|
|
return static_cast<T*>(this->getProc<void*>(process));
|
2021-07-18 11:36:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename... Args>
|
2021-08-19 05:45:40 -04:00
|
|
|
T invoke(const std::string& process, Args ... args) const
|
2021-07-18 11:36:01 -04:00
|
|
|
{
|
2021-08-19 05:45:40 -04:00
|
|
|
auto method = this->get<T(__cdecl)(Args ...)>(process);
|
2021-07-18 11:36:01 -04:00
|
|
|
if (method) return method(args...);
|
|
|
|
return T();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename... Args>
|
2021-08-19 05:45:40 -04:00
|
|
|
T invokePascal(const std::string& process, Args ... args) const
|
2021-07-18 11:36:01 -04:00
|
|
|
{
|
2021-08-19 05:45:40 -04:00
|
|
|
auto method = this->get<T(__stdcall)(Args ...)>(process);
|
2021-07-18 11:36:01 -04:00
|
|
|
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>
|
2021-08-19 05:45:40 -04:00
|
|
|
T invokeThis(const std::string& process, void* this_ptr, Args ... args) const
|
2021-07-18 11:36:01 -04:00
|
|
|
{
|
2021-08-19 05:45:40 -04:00
|
|
|
auto method = this->get<T(__thiscall)(void*, Args ...)>(this_ptr, process);
|
2021-07-18 11:36:01 -04:00
|
|
|
if (method) return method(args...);
|
|
|
|
return T();
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|
|
|
|
|
2022-08-02 07:24:22 -04:00
|
|
|
static void LaunchProcess(const std::string& process, const std::string& commandLine, const std::string& currentDir);
|
2017-03-26 12:00:06 -04:00
|
|
|
|
2017-01-19 16:23:59 -05:00
|
|
|
private:
|
2022-05-05 10:03:14 -04:00
|
|
|
HMODULE module_;
|
2022-08-02 07:24:22 -04:00
|
|
|
bool freeOnDestroy_;
|
2017-01-19 16:23:59 -05:00
|
|
|
};
|
|
|
|
}
|