From 5564b166a3ea00e44053edbf7b8b41f5c9bf8c39 Mon Sep 17 00:00:00 2001 From: Vladimir Belyavsky Date: Wed, 12 Apr 2023 19:34:54 +0300 Subject: [PATCH] QNetworkInformation[Win]: Catch potential WinRT exceptions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some Windows SDKs seem to throw an exception from winrt::check_hresult() We need to handle this accordingly. Catch the exception and print relevant warning. Fixes: QTBUG-110408 Pick-to: 6.5 Change-Id: I1434ec425f0d0e597308b53f25f4f15049640060 Reviewed-by: MÃ¥rten Nordheim --- .../qnetworklistmanagerevents.cpp | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/plugins/networkinformation/networklistmanager/qnetworklistmanagerevents.cpp b/src/plugins/networkinformation/networklistmanager/qnetworklistmanagerevents.cpp index 5b0a91df3e..eb6b6888af 100644 --- a/src/plugins/networkinformation/networklistmanager/qnetworklistmanagerevents.cpp +++ b/src/plugins/networkinformation/networklistmanager/qnetworklistmanagerevents.cpp @@ -102,16 +102,22 @@ bool QNetworkListManagerEvents::start() #if QT_CONFIG(cpp_winrt) using namespace winrt::Windows::Networking::Connectivity; using winrt::Windows::Foundation::IInspectable; - // Register for changes in the network and store a token to unregister later: - token = NetworkInformation::NetworkStatusChanged( - [owner = QPointer(this)](const IInspectable sender) { - Q_UNUSED(sender); - if (owner) { - std::scoped_lock locker(owner->winrtLock); - if (owner->token) - owner->emitWinRTUpdates(); - } - }); + try { + // Register for changes in the network and store a token to unregister later: + token = NetworkInformation::NetworkStatusChanged( + [owner = QPointer(this)](const IInspectable sender) { + Q_UNUSED(sender); + if (owner) { + std::scoped_lock locker(owner->winrtLock); + if (owner->token) + owner->emitWinRTUpdates(); + } + }); + } catch (const winrt::hresult_error &ex) { + qCWarning(lcNetInfoNLM) << "Failed to register network status changed callback:" + << QSystemError::windowsComString(ex.code()); + } + // Emit initial state emitWinRTUpdates(); #endif