2022-07-28 16:07:38 +00:00
|
|
|
#pragma once
|
|
|
|
#include "exception.hpp"
|
2023-10-24 13:18:03 +00:00
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
#include <llarp/util/str.hpp>
|
|
|
|
|
2023-10-24 13:18:03 +00:00
|
|
|
#include <windows.h>
|
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
namespace llarp::win32
|
|
|
|
{
|
2022-09-12 12:40:33 +00:00
|
|
|
namespace detail
|
2022-07-28 16:07:38 +00:00
|
|
|
{
|
2022-09-12 12:40:33 +00:00
|
|
|
HMODULE
|
|
|
|
load_dll(const std::string& dll);
|
2022-07-28 16:07:38 +00:00
|
|
|
|
2022-09-12 12:40:33 +00:00
|
|
|
template <typename Func, typename... More>
|
2022-07-28 16:07:38 +00:00
|
|
|
void
|
2022-09-12 12:40:33 +00:00
|
|
|
load_funcs(HMODULE handle, const std::string& name, Func*& f, More&&... more)
|
2022-07-28 16:07:38 +00:00
|
|
|
{
|
2022-09-12 12:40:33 +00:00
|
|
|
if (auto ptr = GetProcAddress(handle, name.c_str()))
|
|
|
|
f = reinterpret_cast<Func*>(ptr);
|
|
|
|
else
|
2022-07-28 16:07:38 +00:00
|
|
|
throw win32::error{fmt::format("function '{}' not found", name)};
|
2022-09-12 12:40:33 +00:00
|
|
|
if constexpr (sizeof...(More) > 0)
|
|
|
|
load_funcs(handle, std::forward<More>(more)...);
|
2022-07-28 16:07:38 +00:00
|
|
|
}
|
2022-09-12 12:40:33 +00:00
|
|
|
} // namespace detail
|
2022-07-28 16:07:38 +00:00
|
|
|
|
2022-09-12 12:40:33 +00:00
|
|
|
// Loads a DLL and extracts function pointers from it. Takes the dll name and pairs of
|
|
|
|
// name/function pointer arguments. Throws on failure.
|
|
|
|
template <typename Func, typename... More>
|
|
|
|
void
|
|
|
|
load_dll_functions(const std::string& dll, const std::string& fname, Func*& f, More&&... funcs)
|
|
|
|
{
|
|
|
|
detail::load_funcs(detail::load_dll(dll), fname, f, std::forward<More>(funcs)...);
|
|
|
|
}
|
2022-07-28 16:07:38 +00:00
|
|
|
} // namespace llarp::win32
|