1 // Copyright (c) 2014-2019 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5 #include <qt/winshutdownmonitor.h>
6
7 #if defined(Q_OS_WIN)
8 #include <shutdown.h>
9
10 #include <windows.h>
11
12 #include <QDebug>
13
14 // If we don't want a message to be processed by Qt, return true and set result to
15 // the value that the window procedure should return. Otherwise return false.
nativeEventFilter(const QByteArray & eventType,void * pMessage,long * pnResult)16 bool WinShutdownMonitor::nativeEventFilter(const QByteArray &eventType, void *pMessage, long *pnResult)
17 {
18 Q_UNUSED(eventType);
19
20 MSG *pMsg = static_cast<MSG *>(pMessage);
21
22 switch(pMsg->message)
23 {
24 case WM_QUERYENDSESSION:
25 {
26 // Initiate a client shutdown after receiving a WM_QUERYENDSESSION and block
27 // Windows session end until we have finished client shutdown.
28 StartShutdown();
29 *pnResult = FALSE;
30 return true;
31 }
32
33 case WM_ENDSESSION:
34 {
35 *pnResult = FALSE;
36 return true;
37 }
38 }
39
40 return false;
41 }
42
registerShutdownBlockReason(const QString & strReason,const HWND & mainWinId)43 void WinShutdownMonitor::registerShutdownBlockReason(const QString& strReason, const HWND& mainWinId)
44 {
45 typedef BOOL (WINAPI *PSHUTDOWNBRCREATE)(HWND, LPCWSTR);
46 PSHUTDOWNBRCREATE shutdownBRCreate = (PSHUTDOWNBRCREATE)GetProcAddress(GetModuleHandleA("User32.dll"), "ShutdownBlockReasonCreate");
47 if (shutdownBRCreate == nullptr) {
48 qWarning() << "registerShutdownBlockReason: GetProcAddress for ShutdownBlockReasonCreate failed";
49 return;
50 }
51
52 if (shutdownBRCreate(mainWinId, strReason.toStdWString().c_str()))
53 qInfo() << "registerShutdownBlockReason: Successfully registered: " + strReason;
54 else
55 qWarning() << "registerShutdownBlockReason: Failed to register: " + strReason;
56 }
57 #endif
58