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