move module loading functions out of shortcutcreator.cpp

This commit is contained in:
mjk 2021-03-20 19:28:21 +00:00 committed by LAGonauta
parent e8d84d2eda
commit 8f8dfa2e70
4 changed files with 63 additions and 27 deletions

View file

@ -40,6 +40,7 @@ set(WINDOWS_API_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/api/src/gobject/winrt.cpp
${CMAKE_CURRENT_SOURCE_DIR}/api/src/win32.cpp
${CMAKE_CURRENT_SOURCE_DIR}/api/src/converter.cpp
${CMAKE_CURRENT_SOURCE_DIR}/api/src/dyn_mod.cpp
${CMAKE_CURRENT_SOURCE_DIR}/api/src/ginvoke.cpp
${CMAKE_CURRENT_SOURCE_DIR}/api/src/shortcutcreator.cpp
)

View file

@ -0,0 +1,29 @@
#ifndef DYN_MOD_HPP
#define DYN_MOD_HPP
namespace dyn_mod
{
using punny_func = void();
punny_func &load_symbol(
const wchar_t *mod_path,
const char *mod_dbgnym,
const char *symbol);
template<typename T>
inline T &load_symbol(
const wchar_t *const mod_path, const char *const mod_dbgnym,
const char *const symbol)
{
return reinterpret_cast<T &>(load_symbol(mod_path, mod_dbgnym, symbol));
}
}
#define dyn_load_symbol_ns(mod_name, namespace, symbol) \
::dyn_mod::load_symbol<decltype(namespace::symbol)>( \
L ## mod_name, mod_name, #symbol)
#define dyn_load_symbol(mod_name, symbol) dyn_load_symbol_ns(mod_name, ,symbol)
#endif

View file

@ -0,0 +1,32 @@
#include "dyn_mod.hpp"
#include "win32.hpp"
#include <glib.h>
#include <windows.h>
namespace dyn_mod
{
auto load_module(const wchar_t *const path, const char *const dbgnym)
{
const auto mod = ::LoadLibraryW(path);
if (mod)
return mod;
const win32_error e{};
g_warning("failed to load %s", dbgnym);
throw e;
}
punny_func &load_symbol(
const wchar_t *const mod_path, const char *const mod_dbgnym,
const char *const symbol)
{
const auto p = reinterpret_cast<punny_func *>(
::GetProcAddress(load_module(mod_path, mod_dbgnym), symbol));
if (p)
return *p;
const win32_error e{};
g_warning("couldn't find %s in %s", symbol, mod_dbgnym);
throw e;
}
}

View file

@ -3,6 +3,7 @@
#include "win32.hpp"
#include "converter.hpp"
#include "ginvoke.hpp"
#include "dyn_mod.hpp"
#include <objbase.h> // COM stuff
#include <shlobj.h> // IShellLink
@ -15,33 +16,6 @@
namespace dyn
{
inline auto load_module(const wchar_t *const path, const char *const dbgnym)
{
const auto mod = ::LoadLibraryW(path);
if (mod)
return mod;
const win32_error e{};
g_warning("failed to load %s", dbgnym);
throw e;
}
template<typename T>
inline T &load_symbol(
const wchar_t *const mod_path, const char *const mod_dbgnym,
const char *const symbol)
{
const auto p = reinterpret_cast<T *>(
::GetProcAddress(load_module(mod_path, mod_dbgnym), symbol));
if (p)
return *p;
const win32_error e{};
g_warning("couldn't find %s in %s", symbol, mod_dbgnym);
throw e;
}
#define dyn_load_symbol(mod_name, symbol) \
::dyn::load_symbol<decltype(::symbol)>(L ## mod_name, mod_name, #symbol)
// PropVariantToString is a pain to use, and
// MinGW 6.0.0 doesn't have libpropsys.a in the first place;
// MinGW 9.0.0 doesn't have PropVariantToStringAlloc in its libpropsys.a.