6arelyFuture eb318d32c5
All checks were successful
check-formatting / check-formatting (push) Successful in 4m8s
fix many issues with this repo
2025-04-11 10:41:28 +02:00

159 lines
3.9 KiB
C++

#pragma once
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
// min and max is required by gdi, therefore NOMINMAX won't work
#ifdef max
#undef max
#endif
#ifdef min
#undef min
#endif
#include <filesystem>
#include <functional>
#include <string>
namespace utils::nt {
class library final {
public:
static library load(const std::string& name);
static library load(const std::filesystem::path& path);
static library get_by_address(void* address);
library();
explicit library(const std::string& name);
explicit library(HMODULE handle);
library(const library& a) : module_(a.module_) {}
bool operator!=(const library& obj) const { return !(*this == obj); };
bool operator==(const library& obj) const;
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;
std::string get_folder() const;
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.c_str()));
}
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();
}
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;
[[nodiscard]] void** get_iat_entry(const std::string& module_name,
const std::string& proc_name) const;
static void set_dll_directory(const std::string& directory);
static std::string get_dll_directory();
private:
HMODULE module_;
};
template <HANDLE InvalidHandle = nullptr> class handle {
public:
handle() = default;
handle(const HANDLE h) : handle_(h) {}
~handle() {
if (*this) {
CloseHandle(this->handle_);
this->handle_ = InvalidHandle;
}
}
handle(const handle&) = delete;
handle& operator=(const handle&) = delete;
handle(handle&& obj) noexcept : handle() { this->operator=(std::move(obj)); }
handle& operator=(handle&& obj) noexcept {
if (this != &obj) {
this->~handle();
this->handle_ = obj.handle_;
obj.handle_ = InvalidHandle;
}
return *this;
}
handle& operator=(HANDLE h) noexcept {
this->~handle();
this->handle_ = h;
return *this;
}
operator bool() const { return this->handle_ != InvalidHandle; }
operator HANDLE() const { return this->handle_; }
private:
HANDLE handle_{InvalidHandle};
};
bool is_wine();
__declspec(noreturn) void raise_hard_exception();
std::string load_resource(int id);
void launch_process(const std::string& process,
const std::string& command_line);
void relaunch_self(const std::string& command_line = "");
void update_dll_search_path(const std::string& directory);
__declspec(noreturn) void terminate(uint32_t code = 0);
} // namespace utils::nt