1 // Copyright (c) 2014-2016 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.
5 #include "winshutdownmonitor.h"
7 #if defined(Q_OS_WIN) && QT_VERSION >= 0x050000
15 #include <openssl/rand.h>
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
)
23 MSG
*pMsg
= static_cast<MSG
*>(pMessage
);
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;
30 LogPrintf("%s: OpenSSL RAND_event() failed to seed OpenSSL PRNG with enough data.\n", __func__
);
37 case WM_QUERYENDSESSION
:
39 // Initiate a client shutdown after receiving a WM_QUERYENDSESSION and block
40 // Windows session end until we have finished client shutdown.
56 void WinShutdownMonitor::registerShutdownBlockReason(const QString
& strReason
, const HWND
& mainWinId
)
58 typedef BOOL (WINAPI
*PSHUTDOWNBRCREATE
)(HWND
, LPCWSTR
);
59 PSHUTDOWNBRCREATE shutdownBRCreate
= (PSHUTDOWNBRCREATE
)GetProcAddress(GetModuleHandleA("User32.dll"), "ShutdownBlockReasonCreate");
60 if (shutdownBRCreate
== nullptr) {
61 qWarning() << "registerShutdownBlockReason: GetProcAddress for ShutdownBlockReasonCreate failed";
65 if (shutdownBRCreate(mainWinId
, strReason
.toStdWString().c_str()))
66 qWarning() << "registerShutdownBlockReason: Successfully registered: " + strReason
;
68 qWarning() << "registerShutdownBlockReason: Failed to register: " + strReason
;