From b0e3d779753ecd2c6a78a1b3f911f0db11e0a232 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 7 Apr 2018 15:35:35 +0800 Subject: [PATCH] Add helper for loading Windows system functions --- src/app/application.cpp | 5 ++--- src/base/bittorrent/session.cpp | 4 ++-- src/base/utils/misc.h | 23 +++++++++++++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/app/application.cpp b/src/app/application.cpp index 4e1f31b65..c92146c7d 100644 --- a/src/app/application.cpp +++ b/src/app/application.cpp @@ -64,7 +64,6 @@ #ifndef DISABLE_GUI #ifdef Q_OS_WIN -#include #include #include #endif // Q_OS_WIN @@ -682,7 +681,7 @@ void Application::cleanup() #ifdef Q_OS_WIN typedef BOOL (WINAPI *PSHUTDOWNBRCREATE)(HWND, LPCWSTR); - PSHUTDOWNBRCREATE shutdownBRCreate = (PSHUTDOWNBRCREATE)::GetProcAddress(::GetModuleHandleW(L"User32.dll"), "ShutdownBlockReasonCreate"); + const auto shutdownBRCreate = Utils::Misc::loadWinAPI("User32.dll", "ShutdownBlockReasonCreate"); // Only available on Vista+ if (shutdownBRCreate) shutdownBRCreate((HWND)m_window->effectiveWinId(), tr("Saving torrent progress...").toStdWString().c_str()); @@ -724,7 +723,7 @@ void Application::cleanup() if (m_window) { #ifdef Q_OS_WIN typedef BOOL (WINAPI *PSHUTDOWNBRDESTROY)(HWND); - PSHUTDOWNBRDESTROY shutdownBRDestroy = (PSHUTDOWNBRDESTROY)::GetProcAddress(::GetModuleHandleW(L"User32.dll"), "ShutdownBlockReasonDestroy"); + const auto shutdownBRDestroy = Utils::Misc::loadWinAPI("User32.dll", "ShutdownBlockReasonDestroy"); // Only available on Vista+ if (shutdownBRDestroy) shutdownBRDestroy((HWND)m_window->effectiveWinId()); diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 68aa34b9b..b8189d126 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -4543,11 +4543,11 @@ namespace return uuid.toString().toUpper(); // Libtorrent expects the GUID in uppercase using PCONVERTIFACENAMETOLUID = NETIO_STATUS (WINAPI *)(const WCHAR *, PNET_LUID); - PCONVERTIFACENAMETOLUID ConvertIfaceNameToLuid = reinterpret_cast(::GetProcAddress(::GetModuleHandleW(L"Iphlpapi.dll"), "ConvertInterfaceNameToLuidW")); + const auto ConvertIfaceNameToLuid = Utils::Misc::loadWinAPI("Iphlpapi.dll", "ConvertInterfaceNameToLuidW"); if (!ConvertIfaceNameToLuid) return QString(); using PCONVERTIFACELUIDTOGUID = NETIO_STATUS (WINAPI *)(const NET_LUID *, GUID *); - PCONVERTIFACELUIDTOGUID ConvertIfaceLuidToGuid = reinterpret_cast(::GetProcAddress(::GetModuleHandleW(L"Iphlpapi.dll"), "ConvertInterfaceLuidToGuid")); + const auto ConvertIfaceLuidToGuid = Utils::Misc::loadWinAPI("Iphlpapi.dll", "ConvertInterfaceLuidToGuid"); if (!ConvertIfaceLuidToGuid) return QString(); NET_LUID luid; diff --git a/src/base/utils/misc.h b/src/base/utils/misc.h index 7e49d9830..db9617ce3 100644 --- a/src/base/utils/misc.h +++ b/src/base/utils/misc.h @@ -34,6 +34,13 @@ #include #include +#include + +#ifdef Q_OS_WIN +#include +#include +#endif + #include #include #include @@ -109,6 +116,22 @@ namespace Utils #ifdef Q_OS_WIN QString windowsSystemPath(); + + template + T loadWinAPI(const QString &source, const char *funcName) + { + QString path = windowsSystemPath(); + if (!path.endsWith('\\')) + path += '\\'; + + path += source; + + std::unique_ptr pathWchar(new wchar_t[path.length() + 1] {}); + path.toWCharArray(pathWchar.get()); + + return reinterpret_cast( + ::GetProcAddress(::LoadLibraryW(pathWchar.get()), funcName)); + } #endif } }