9ad6746ccd Use static_cast instead of C-style casts for non-fundamental types (practicalswift) Pull request description: A C-style cast is equivalent to try casting in the following order: 1. `const_cast(...)` 2. `static_cast(...)` 3. `const_cast(static_cast(...))` 4. `reinterpret_cast(...)` 5. `const_cast(reinterpret_cast(...))` By using `static_cast<T>(...)` explicitly we avoid the possibility of an unintentional and dangerous `reinterpret_cast`. Furthermore `static_cast<T>(...)` allows for easier grepping of casts. For a more thorough discussion, see ["ES.49: If you must use a cast, use a named cast"](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es49-if-you-must-use-a-cast-use-a-named-cast) in the C++ Core Guidelines (Stroustrup & Sutter). Tree-SHA512: bd6349b7ea157da93a47b8cf238932af5dff84731374ccfd69b9f732fabdad1f9b1cdfca67497040f14eaa85346391404f4c0495e22c467f26ca883cd2de4d3c
71 lines
2.4 KiB
C++
71 lines
2.4 KiB
C++
// Copyright (c) 2014-2017 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#include <qt/winshutdownmonitor.h>
|
|
|
|
#if defined(Q_OS_WIN) && QT_VERSION >= 0x050000
|
|
#include <init.h>
|
|
#include <util.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
// If we don't want a message to be processed by Qt, return true and set result to
|
|
// the value that the window procedure should return. Otherwise return false.
|
|
bool WinShutdownMonitor::nativeEventFilter(const QByteArray &eventType, void *pMessage, long *pnResult)
|
|
{
|
|
Q_UNUSED(eventType);
|
|
|
|
MSG *pMsg = static_cast<MSG *>(pMessage);
|
|
|
|
// Seed OpenSSL PRNG with Windows event data (e.g. mouse movements and other user interactions)
|
|
if (RAND_event(pMsg->message, pMsg->wParam, pMsg->lParam) == 0) {
|
|
// Warn only once as this is performance-critical
|
|
static bool warned = false;
|
|
if (!warned) {
|
|
LogPrintf("%s: OpenSSL RAND_event() failed to seed OpenSSL PRNG with enough data.\n", __func__);
|
|
warned = true;
|
|
}
|
|
}
|
|
|
|
switch(pMsg->message)
|
|
{
|
|
case WM_QUERYENDSESSION:
|
|
{
|
|
// Initiate a client shutdown after receiving a WM_QUERYENDSESSION and block
|
|
// Windows session end until we have finished client shutdown.
|
|
StartShutdown();
|
|
*pnResult = FALSE;
|
|
return true;
|
|
}
|
|
|
|
case WM_ENDSESSION:
|
|
{
|
|
*pnResult = FALSE;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
void WinShutdownMonitor::registerShutdownBlockReason(const QString& strReason, const HWND& mainWinId)
|
|
{
|
|
typedef BOOL (WINAPI *PSHUTDOWNBRCREATE)(HWND, LPCWSTR);
|
|
PSHUTDOWNBRCREATE shutdownBRCreate = static_cast<PSHUTDOWNBRCREATE>(GetProcAddress(GetModuleHandleA("User32.dll"), "ShutdownBlockReasonCreate"));
|
|
if (shutdownBRCreate == nullptr) {
|
|
qWarning() << "registerShutdownBlockReason: GetProcAddress for ShutdownBlockReasonCreate failed";
|
|
return;
|
|
}
|
|
|
|
if (shutdownBRCreate(mainWinId, strReason.toStdWString().c_str()))
|
|
qWarning() << "registerShutdownBlockReason: Successfully registered: " + strReason;
|
|
else
|
|
qWarning() << "registerShutdownBlockReason: Failed to register: " + strReason;
|
|
}
|
|
#endif
|