Skip to content

Commit

Permalink
Call Windows API directly
Browse files Browse the repository at this point in the history
We already bumped the OS requirement to Windows 7 and those functions
can be called directly without the need to load them first.
  • Loading branch information
Chocobo1 committed Sep 29, 2019
1 parent 975b44d commit 7276a79
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 39 deletions.
13 changes: 3 additions & 10 deletions src/app/application.cpp
Expand Up @@ -685,11 +685,8 @@ void Application::cleanup()
m_window->hide(); m_window->hide();


#ifdef Q_OS_WIN #ifdef Q_OS_WIN
typedef BOOL (WINAPI *PSHUTDOWNBRCREATE)(HWND, LPCWSTR); ::ShutdownBlockReasonCreate(reinterpret_cast<HWND>(m_window->effectiveWinId())
const auto shutdownBRCreate = Utils::Misc::loadWinAPI<PSHUTDOWNBRCREATE>("User32.dll", "ShutdownBlockReasonCreate"); , tr("Saving torrent progress...").toStdWString().c_str());
// Only available on Vista+
if (shutdownBRCreate)
shutdownBRCreate((HWND)m_window->effectiveWinId(), tr("Saving torrent progress...").toStdWString().c_str());
#endif // Q_OS_WIN #endif // Q_OS_WIN


// Do manual cleanup in MainWindow to force widgets // Do manual cleanup in MainWindow to force widgets
Expand Down Expand Up @@ -728,11 +725,7 @@ void Application::cleanup()
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
if (m_window) { if (m_window) {
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
using PSHUTDOWNBRDESTROY = BOOL (WINAPI *)(HWND); ::ShutdownBlockReasonDestroy(reinterpret_cast<HWND>(m_window->effectiveWinId()));
const auto shutdownBRDestroy = Utils::Misc::loadWinAPI<PSHUTDOWNBRDESTROY>("User32.dll", "ShutdownBlockReasonDestroy");
// Only available on Vista+
if (shutdownBRDestroy)
shutdownBRDestroy(reinterpret_cast<HWND>(m_window->effectiveWinId()));
#endif // Q_OS_WIN #endif // Q_OS_WIN
delete m_window; delete m_window;
UIThemeManager::freeInstance(); UIThemeManager::freeInstance();
Expand Down
11 changes: 3 additions & 8 deletions src/app/qtlocalpeer/qtlocalpeer.cpp
Expand Up @@ -116,14 +116,9 @@ QtLocalPeer::QtLocalPeer(QObject* parent, const QString &appId)
+ QLatin1Char('-') + QString::number(idNum, 16); + QLatin1Char('-') + QString::number(idNum, 16);


#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
using PPROCESSIDTOSESSIONID = BOOL (WINAPI *)(DWORD, DWORD *); DWORD sessionId = 0;
const auto processIdToSessionId = Utils::Misc::loadWinAPI<PPROCESSIDTOSESSIONID>("kernel32.dll", "ProcessIdToSessionId"); ::ProcessIdToSessionId(GetCurrentProcessId(), &sessionId);

socketName += (QLatin1Char('-') + QString::number(sessionId, 16));
if (processIdToSessionId) {
DWORD sessionId = 0;
processIdToSessionId(GetCurrentProcessId(), &sessionId);
socketName += (QLatin1Char('-') + QString::number(sessionId, 16));
}
#else #else
socketName += (QLatin1Char('-') + QString::number(::getuid(), 16)); socketName += (QLatin1Char('-') + QString::number(::getuid(), 16));
#endif #endif
Expand Down
16 changes: 4 additions & 12 deletions src/base/bittorrent/session.cpp
Expand Up @@ -4531,23 +4531,15 @@ namespace
QString convertIfaceNameToGuid(const QString &name) QString convertIfaceNameToGuid(const QString &name)
{ {
// Under Windows XP or on Qt version <= 5.5 'name' will be a GUID already. // Under Windows XP or on Qt version <= 5.5 'name' will be a GUID already.
QUuid uuid(name); const QUuid uuid(name);
if (!uuid.isNull()) if (!uuid.isNull())
return uuid.toString().toUpper(); // Libtorrent expects the GUID in uppercase return uuid.toString().toUpper(); // Libtorrent expects the GUID in uppercase


using PCONVERTIFACENAMETOLUID = NETIO_STATUS (WINAPI *)(const WCHAR *, PNET_LUID); NET_LUID luid {};
const auto ConvertIfaceNameToLuid = Utils::Misc::loadWinAPI<PCONVERTIFACENAMETOLUID>("Iphlpapi.dll", "ConvertInterfaceNameToLuidW"); const LONG res = ::ConvertInterfaceNameToLuidW(name.toStdWString().c_str(), &luid);
if (!ConvertIfaceNameToLuid) return {};

using PCONVERTIFACELUIDTOGUID = NETIO_STATUS (WINAPI *)(const NET_LUID *, GUID *);
const auto ConvertIfaceLuidToGuid = Utils::Misc::loadWinAPI<PCONVERTIFACELUIDTOGUID>("Iphlpapi.dll", "ConvertInterfaceLuidToGuid");
if (!ConvertIfaceLuidToGuid) return {};

NET_LUID luid;
const LONG res = ConvertIfaceNameToLuid(name.toStdWString().c_str(), &luid);
if (res == 0) { if (res == 0) {
GUID guid; GUID guid;
if (ConvertIfaceLuidToGuid(&luid, &guid) == 0) if (::ConvertInterfaceLuidToGuid(&luid, &guid) == 0)
return QUuid(guid).toString().toUpper(); return QUuid(guid).toString().toUpper();
} }


Expand Down
10 changes: 3 additions & 7 deletions src/base/utils/misc.cpp
Expand Up @@ -30,6 +30,7 @@


#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#include <windows.h> #include <windows.h>
#include <powrprof.h>
#include <Shlobj.h> #include <Shlobj.h>
#else #else
#include <sys/types.h> #include <sys/types.h>
Expand Down Expand Up @@ -117,16 +118,11 @@ void Utils::Misc::shutdownComputer(const ShutdownDialogAction &action)
if (GetLastError() != ERROR_SUCCESS) if (GetLastError() != ERROR_SUCCESS)
return; return;


using PSETSUSPENDSTATE = BOOLEAN (WINAPI *)(BOOLEAN, BOOLEAN, BOOLEAN);
const auto setSuspendState = Utils::Misc::loadWinAPI<PSETSUSPENDSTATE>("PowrProf.dll", "SetSuspendState");

if (action == ShutdownDialogAction::Suspend) { if (action == ShutdownDialogAction::Suspend) {
if (setSuspendState) ::SetSuspendState(false, false, false);
setSuspendState(false, false, false);
} }
else if (action == ShutdownDialogAction::Hibernate) { else if (action == ShutdownDialogAction::Hibernate) {
if (setSuspendState) ::SetSuspendState(true, false, false);
setSuspendState(true, false, false);
} }
else { else {
const QString msg = QCoreApplication::translate("misc", "qBittorrent will shutdown the computer now because all downloads are complete."); const QString msg = QCoreApplication::translate("misc", "qBittorrent will shutdown the computer now because all downloads are complete.");
Expand Down
4 changes: 2 additions & 2 deletions winconf.pri
Expand Up @@ -34,15 +34,15 @@ win32-g++* {


RC_FILE = qbittorrent_mingw.rc RC_FILE = qbittorrent_mingw.rc


LIBS += libadvapi32 libshell32 libuser32 libole32 libwsock32 libws2_32 LIBS += libadvapi32 libiphlpapi libole32 libpowrprof libshell32 libuser32 libwsock32 libws2_32
} }
else:win32-msvc* { else:win32-msvc* {
CONFIG -= embed_manifest_exe CONFIG -= embed_manifest_exe
QMAKE_LFLAGS += "/MANIFEST:EMBED /MANIFESTINPUT:$$quote($${PWD}/src/qbittorrent.exe.manifest) /STACK:0x800000" QMAKE_LFLAGS += "/MANIFEST:EMBED /MANIFESTINPUT:$$quote($${PWD}/src/qbittorrent.exe.manifest) /STACK:0x800000"


RC_FILE = qbittorrent.rc RC_FILE = qbittorrent.rc


LIBS += advapi32.lib shell32.lib crypt32.lib User32.lib ole32.lib LIBS += advapi32.lib crypt32.lib Iphlpapi.lib ole32.lib PowrProf.lib shell32.lib User32.lib
} }


# See an example build configuration in "conf.pri.windows" # See an example build configuration in "conf.pri.windows"
Expand Down

0 comments on commit 7276a79

Please sign in to comment.