winshutdownmonitor.cpp raw
1 // Copyright (c) 2014-present 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
9 #include <windows.h>
10
11 #include <QDebug>
12
13 // If we don't want a message to be processed by Qt, return true and set result to
14 // the value that the window procedure should return. Otherwise return false.
15 bool WinShutdownMonitor::nativeEventFilter(const QByteArray &eventType, void *pMessage, qintptr *pnResult)
16 {
17 Q_UNUSED(eventType);
18
19 MSG *pMsg = static_cast<MSG *>(pMessage);
20
21 switch(pMsg->message)
22 {
23 case WM_QUERYENDSESSION:
24 {
25 // Initiate a client shutdown after receiving a WM_QUERYENDSESSION and block
26 // Windows session end until we have finished client shutdown.
27 m_shutdown_fn();
28 *pnResult = FALSE;
29 return true;
30 }
31
32 case WM_ENDSESSION:
33 {
34 *pnResult = FALSE;
35 return true;
36 }
37 }
38
39 return false;
40 }
41
42 void WinShutdownMonitor::registerShutdownBlockReason(const QString& strReason, const HWND& mainWinId)
43 {
44 typedef BOOL (WINAPI *PSHUTDOWNBRCREATE)(HWND, LPCWSTR);
45 PSHUTDOWNBRCREATE shutdownBRCreate = (PSHUTDOWNBRCREATE)(void*)GetProcAddress(GetModuleHandleA("User32.dll"), "ShutdownBlockReasonCreate");
46 if (shutdownBRCreate == nullptr) {
47 qWarning() << "registerShutdownBlockReason: GetProcAddress for ShutdownBlockReasonCreate failed";
48 return;
49 }
50
51 if (shutdownBRCreate(mainWinId, strReason.toStdWString().c_str()))
52 qInfo() << "registerShutdownBlockReason: Successfully registered: " + strReason;
53 else
54 qWarning() << "registerShutdownBlockReason: Failed to register: " + strReason;
55 }
56 #endif
57