From 7f21dae378fea5a3bb1ee68d9218bb1603f9dd38 Mon Sep 17 00:00:00 2001 From: Roland Winklmeier Date: Fri, 6 Jan 2017 18:37:21 +0100 Subject: [PATCH] Pin QtDBus shared library on Windows after being loaded by X-Plane refs #850 --- src/blackmisc/dbus.cpp | 34 ++++++++++++++++++++++++++++++++++ src/blackmisc/dbus.h | 6 ++++++ src/xbus/main.cpp | 5 +++++ 3 files changed, 45 insertions(+) create mode 100644 src/blackmisc/dbus.cpp diff --git a/src/blackmisc/dbus.cpp b/src/blackmisc/dbus.cpp new file mode 100644 index 000000000..72f4858fb --- /dev/null +++ b/src/blackmisc/dbus.cpp @@ -0,0 +1,34 @@ +/* Copyright (C) 2016 + * swift Project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "dbus.h" + +#ifdef Q_OS_WIN +#include +#include + +// https://blogs.msdn.microsoft.com/oldnewthing/20131105-00/?p=2733 +// See https://bugreports.qt.io/browse/QTBUG-53031 for more details +// why this is necessary. +void preventQtDBusDllUnload() +{ + // Only Qt 5.8.0 is affected. + if (qVersion() != QByteArray("5.8.0")) { return; } + + static HMODULE dbusDll; + GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | + GET_MODULE_HANDLE_EX_FLAG_PIN, + reinterpret_cast(&QDBusConnection::staticMetaObject), + &dbusDll); + Q_ASSERT(dbusDll); +} +#else +void preventQtDBusDllUnload() +{ } +#endif diff --git a/src/blackmisc/dbus.h b/src/blackmisc/dbus.h index a67476113..440898f86 100644 --- a/src/blackmisc/dbus.h +++ b/src/blackmisc/dbus.h @@ -12,6 +12,7 @@ #ifndef BLACKMISC_DBUS_H #define BLACKMISC_DBUS_H +#include "blackmisc/blackmiscexport.h" #include "blackmisc/metaclass.h" #include "blackmisc/inheritancetraits.h" #include @@ -159,4 +160,9 @@ const QDBusArgument &operator>>(const QDBusArgument &argument, QPixmap &pixmap); */ QDBusArgument &operator<<(QDBusArgument &argument, const QPixmap &pixmap); +//! Windows: prevents unloading of QtDBus shared library until the process is terminated. +//! QtDBus must have been loaded already by the calling process. +//! Does nothing on non-Windows platforms. +BLACKMISC_EXPORT void preventQtDBusDllUnload(); + #endif // guard diff --git a/src/xbus/main.cpp b/src/xbus/main.cpp index 4afc614c1..28b7fc5e9 100644 --- a/src/xbus/main.cpp +++ b/src/xbus/main.cpp @@ -14,6 +14,7 @@ #include "utils.h" #include "traffic.h" #include "service.h" +#include "blackmisc/dbus.h" #include #if ! defined(XPLM210) @@ -57,6 +58,10 @@ PLUGIN_API int XPluginEnable() QXPlaneEventLoop::exec(); g_plugin = new XBus::CPlugin; + + // Here we can be safely assume that QtDBus was loaded by the process + preventQtDBusDllUnload(); + return 1; }