From 644e7d04509c7000c89338fed3de51676000f87d Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 7 Apr 2018 15:56:09 +0800 Subject: [PATCH] Use dynamic linking for SetSuspendState function --- src/base/utils/misc.cpp | 19 +++++++++++++------ winconf.pri | 8 ++++---- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/base/utils/misc.cpp b/src/base/utils/misc.cpp index 2ed1d341b..3309bccdd 100644 --- a/src/base/utils/misc.cpp +++ b/src/base/utils/misc.cpp @@ -35,7 +35,6 @@ #ifdef Q_OS_WIN #include -#include #include #else #include @@ -113,12 +112,20 @@ void Utils::Misc::shutdownComputer(const ShutdownDialogAction &action) if (GetLastError() != ERROR_SUCCESS) return; - if (action == ShutdownDialogAction::Suspend) - SetSuspendState(false, false, false); - else if (action == ShutdownDialogAction::Hibernate) - SetSuspendState(true, false, false); - else + using PSETSUSPENDSTATE = BOOLEAN (WINAPI *)(BOOLEAN, BOOLEAN, BOOLEAN); + const auto setSuspendState = Utils::Misc::loadWinAPI("PowrProf.dll", "SetSuspendState"); + + if (action == ShutdownDialogAction::Suspend) { + if (setSuspendState) + setSuspendState(false, false, false); + } + else if (action == ShutdownDialogAction::Hibernate) { + if (setSuspendState) + setSuspendState(true, false, false); + } + else { InitiateSystemShutdownA(0, QCoreApplication::translate("misc", "qBittorrent will shutdown the computer now because all downloads are complete.").toLocal8Bit().data(), 10, true, false); + } // Disable shutdown privilege. tkp.Privileges[0].Attributes = 0; diff --git a/winconf.pri b/winconf.pri index 146e5eebb..19f240ba9 100644 --- a/winconf.pri +++ b/winconf.pri @@ -33,8 +33,8 @@ win32-g++* { } RC_FILE = qbittorrent_mingw.rc - - LIBS += libadvapi32 libshell32 libuser32 libole32 libwsock32 libws2_32 libpowrprof + + LIBS += libadvapi32 libshell32 libuser32 libole32 libwsock32 libws2_32 } else:win32-msvc* { CONFIG -= embed_manifest_exe @@ -42,8 +42,8 @@ else:win32-msvc* { QMAKE_LFLAGS_RELEASE += "/OPT:REF /OPT:ICF" RC_FILE = qbittorrent.rc - - LIBS += advapi32.lib shell32.lib crypt32.lib User32.lib ole32.lib PowrProf.lib + + LIBS += advapi32.lib shell32.lib crypt32.lib User32.lib ole32.lib } # See an example build configuration in "conf.pri.windows"