Bitcoin Core  29.1.0
P2P Digital Currency
winshutdownmonitor.cpp
Go to the documentation of this file.
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 
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 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
16 bool WinShutdownMonitor::nativeEventFilter(const QByteArray &eventType, void *pMessage, qintptr *pnResult)
17 #else
18 bool WinShutdownMonitor::nativeEventFilter(const QByteArray &eventType, void *pMessage, long *pnResult)
19 #endif
20 {
21  Q_UNUSED(eventType);
22 
23  MSG *pMsg = static_cast<MSG *>(pMessage);
24 
25  switch(pMsg->message)
26  {
27  case WM_QUERYENDSESSION:
28  {
29  // Initiate a client shutdown after receiving a WM_QUERYENDSESSION and block
30  // Windows session end until we have finished client shutdown.
31  m_shutdown_fn();
32  *pnResult = FALSE;
33  return true;
34  }
35 
36  case WM_ENDSESSION:
37  {
38  *pnResult = FALSE;
39  return true;
40  }
41  }
42 
43  return false;
44 }
45 
46 void WinShutdownMonitor::registerShutdownBlockReason(const QString& strReason, const HWND& mainWinId)
47 {
48  typedef BOOL (WINAPI *PSHUTDOWNBRCREATE)(HWND, LPCWSTR);
49  PSHUTDOWNBRCREATE shutdownBRCreate = (PSHUTDOWNBRCREATE)(void*)GetProcAddress(GetModuleHandleA("User32.dll"), "ShutdownBlockReasonCreate");
50  if (shutdownBRCreate == nullptr) {
51  qWarning() << "registerShutdownBlockReason: GetProcAddress for ShutdownBlockReasonCreate failed";
52  return;
53  }
54 
55  if (shutdownBRCreate(mainWinId, strReason.toStdWString().c_str()))
56  qInfo() << "registerShutdownBlockReason: Successfully registered: " + strReason;
57  else
58  qWarning() << "registerShutdownBlockReason: Failed to register: " + strReason;
59 }
60 #endif