Raven Core  3.0.0
P2P Digital Currency
winshutdownmonitor.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT 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) && QT_VERSION >= 0x050000
9 #include "init.h"
10 #include "util.h"
11 
12 #include <windows.h>
13 
14 #include <QDebug>
15 
16 #include <openssl/rand.h>
17 
18 // If we don't want a message to be processed by Qt, return true and set result to
19 // the value that the window procedure should return. Otherwise return false.
20 bool WinShutdownMonitor::nativeEventFilter(const QByteArray &eventType, void *pMessage, long *pnResult)
21 {
22  Q_UNUSED(eventType);
23 
24  MSG *pMsg = static_cast<MSG *>(pMessage);
25 
26  // Seed OpenSSL PRNG with Windows event data (e.g. mouse movements and other user interactions)
27  if (RAND_event(pMsg->message, pMsg->wParam, pMsg->lParam) == 0) {
28  // Warn only once as this is performance-critical
29  static bool warned = false;
30  if (!warned) {
31  LogPrintf("%s: OpenSSL RAND_event() failed to seed OpenSSL PRNG with enough data.\n", __func__);
32  warned = true;
33  }
34  }
35 
36  switch(pMsg->message)
37  {
38  case WM_QUERYENDSESSION:
39  {
40  // Initiate a client shutdown after receiving a WM_QUERYENDSESSION and block
41  // Windows session end until we have finished client shutdown.
42  StartShutdown();
43  *pnResult = FALSE;
44  return true;
45  }
46 
47  case WM_ENDSESSION:
48  {
49  *pnResult = FALSE;
50  return true;
51  }
52  }
53 
54  return false;
55 }
56 
57 void WinShutdownMonitor::registerShutdownBlockReason(const QString& strReason, const HWND& mainWinId)
58 {
59  typedef BOOL (WINAPI *PSHUTDOWNBRCREATE)(HWND, LPCWSTR);
60  PSHUTDOWNBRCREATE shutdownBRCreate = (PSHUTDOWNBRCREATE)GetProcAddress(GetModuleHandleA("User32.dll"), "ShutdownBlockReasonCreate");
61  if (shutdownBRCreate == nullptr) {
62  qWarning() << "registerShutdownBlockReason: GetProcAddress for ShutdownBlockReasonCreate failed";
63  return;
64  }
65 
66  if (shutdownBRCreate(mainWinId, strReason.toStdWString().c_str()))
67  qWarning() << "registerShutdownBlockReason: Successfully registered: " + strReason;
68  else
69  qWarning() << "registerShutdownBlockReason: Failed to register: " + strReason;
70 }
71 #endif
void StartShutdown()
Definition: init.cpp:128
#define LogPrintf(...)
Definition: util.h:149