Browse Source

Add helper for loading Windows system functions

adaptive-webui-19844
Chocobo1 7 years ago
parent
commit
b0e3d77975
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 5
      src/app/application.cpp
  2. 4
      src/base/bittorrent/session.cpp
  3. 23
      src/base/utils/misc.h

5
src/app/application.cpp

@ -64,7 +64,6 @@ @@ -64,7 +64,6 @@
#ifndef DISABLE_GUI
#ifdef Q_OS_WIN
#include <windows.h>
#include <QSessionManager>
#include <QSharedMemory>
#endif // Q_OS_WIN
@ -682,7 +681,7 @@ void Application::cleanup() @@ -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<PSHUTDOWNBRCREATE>("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() @@ -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<PSHUTDOWNBRDESTROY>("User32.dll", "ShutdownBlockReasonDestroy");
// Only available on Vista+
if (shutdownBRDestroy)
shutdownBRDestroy((HWND)m_window->effectiveWinId());

4
src/base/bittorrent/session.cpp

@ -4543,11 +4543,11 @@ namespace @@ -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<PCONVERTIFACENAMETOLUID>(::GetProcAddress(::GetModuleHandleW(L"Iphlpapi.dll"), "ConvertInterfaceNameToLuidW"));
const auto ConvertIfaceNameToLuid = Utils::Misc::loadWinAPI<PCONVERTIFACENAMETOLUID>("Iphlpapi.dll", "ConvertInterfaceNameToLuidW");
if (!ConvertIfaceNameToLuid) return QString();
using PCONVERTIFACELUIDTOGUID = NETIO_STATUS (WINAPI *)(const NET_LUID *, GUID *);
PCONVERTIFACELUIDTOGUID ConvertIfaceLuidToGuid = reinterpret_cast<PCONVERTIFACELUIDTOGUID>(::GetProcAddress(::GetModuleHandleW(L"Iphlpapi.dll"), "ConvertInterfaceLuidToGuid"));
const auto ConvertIfaceLuidToGuid = Utils::Misc::loadWinAPI<PCONVERTIFACELUIDTOGUID>("Iphlpapi.dll", "ConvertInterfaceLuidToGuid");
if (!ConvertIfaceLuidToGuid) return QString();
NET_LUID luid;

23
src/base/utils/misc.h

@ -34,6 +34,13 @@ @@ -34,6 +34,13 @@
#include <ctime>
#include <vector>
#include <QtGlobal>
#ifdef Q_OS_WIN
#include <memory>
#include <Windows.h>
#endif
#include <QDir>
#include <QFile>
#include <QPoint>
@ -109,6 +116,22 @@ namespace Utils @@ -109,6 +116,22 @@ namespace Utils
#ifdef Q_OS_WIN
QString windowsSystemPath();
template <typename T>
T loadWinAPI(const QString &source, const char *funcName)
{
QString path = windowsSystemPath();
if (!path.endsWith('\\'))
path += '\\';
path += source;
std::unique_ptr<wchar_t[]> pathWchar(new wchar_t[path.length() + 1] {});
path.toWCharArray(pathWchar.get());
return reinterpret_cast<T>(
::GetProcAddress(::LoadLibraryW(pathWchar.get()), funcName));
}
#endif
}
}

Loading…
Cancel
Save