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