iw5-mod/src/utils/nt.hpp

103 lines
2.8 KiB
C++
Raw Normal View History

2019-09-27 22:35:57 +02:00
#pragma once
namespace utils::nt
{
2022-01-15 11:00:52 +01:00
class library final
2019-09-27 22:35:57 +02:00
{
public:
2022-01-15 11:00:52 +01:00
static library load(const std::string& name);
static library load(const std::filesystem::path& path);
static library get_by_address(void* address);
2019-09-27 22:35:57 +02:00
2022-01-15 11:00:52 +01:00
library();
explicit library(const std::string& name);
explicit library(HMODULE handle);
2019-09-27 22:35:57 +02:00
2022-01-15 11:00:52 +01:00
library(const library& a) : module_(a.module_)
2019-09-27 22:35:57 +02:00
{
}
2022-01-15 11:00:52 +01:00
bool operator!=(const library& obj) const { return !(*this == obj); };
bool operator==(const library& obj) const;
2019-09-27 22:35:57 +02:00
operator bool() const;
operator HMODULE() const;
void unprotect() const;
void* get_entry_point() const;
size_t get_relative_entry_point() const;
bool is_valid() const;
std::string get_name() const;
std::string get_path() const;
2022-01-15 11:00:52 +01:00
std::string get_folder() const;
2019-09-27 22:35:57 +02:00
std::uint8_t* get_ptr() const;
void free();
HMODULE get_handle() const;
template <typename T>
T get_proc(const std::string& process) const
{
if (!this->is_valid()) T{};
return reinterpret_cast<T>(GetProcAddress(this->module_, process.data()));
}
2023-01-17 18:37:08 +01:00
template <typename T>
T get_proc(const char* name) const
{
if (!this->is_valid()) T{};
return reinterpret_cast<T>(GetProcAddress(this->module_, name));
}
2019-09-27 22:35:57 +02:00
template <typename T>
std::function<T> get(const std::string& process) const
{
2022-01-15 11:00:52 +01:00
if (!this->is_valid()) return std::function<T>();
2020-04-17 21:29:34 +02:00
return static_cast<T*>(this->get_proc<void*>(process));
2019-09-27 22:35:57 +02:00
}
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();
}
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();
}
std::vector<PIMAGE_SECTION_HEADER> get_section_headers() const;
PIMAGE_NT_HEADERS get_nt_headers() const;
PIMAGE_DOS_HEADER get_dos_header() const;
PIMAGE_OPTIONAL_HEADER get_optional_header() const;
void** get_iat_entry(const std::string& module_name, const std::string& proc_name) const;
2023-01-17 18:37:08 +01:00
void** get_iat_entry(const std::string& module_name, const char* name) const;
2019-09-27 22:35:57 +02:00
private:
HMODULE module_;
};
2022-01-15 11:00:52 +01:00
__declspec(noreturn) void raise_hard_exception();
std::string load_resource(int id);
void relaunch_self();
__declspec(noreturn) void terminate(uint32_t code = 0);
2019-09-27 22:35:57 +02:00
}