diff options
Diffstat (limited to 'src/network/kernel/qnetconmonitor_win.cpp')
-rw-r--r-- | src/network/kernel/qnetconmonitor_win.cpp | 47 |
1 files changed, 23 insertions, 24 deletions
diff --git a/src/network/kernel/qnetconmonitor_win.cpp b/src/network/kernel/qnetconmonitor_win.cpp index c91c4d5c23..bf6aff1e46 100644 --- a/src/network/kernel/qnetconmonitor_win.cpp +++ b/src/network/kernel/qnetconmonitor_win.cpp @@ -8,13 +8,15 @@ #include <QtCore/quuid.h> #include <QtCore/qmetaobject.h> +#include <QtCore/private/qfunctions_win_p.h> +#include <QtCore/private/qsystemerror_p.h> + #include <QtNetwork/qnetworkinterface.h> #include <objbase.h> #include <netlistmgr.h> #include <wrl/client.h> #include <wrl/wrappers/corewrappers.h> -#include <comdef.h> #include <iphlpapi.h> #include <algorithm> @@ -26,12 +28,6 @@ QT_BEGIN_NAMESPACE Q_LOGGING_CATEGORY(lcNetMon, "qt.network.monitor"); namespace { -QString errorStringFromHResult(HRESULT hr) -{ - _com_error error(hr); - return QString::fromWCharArray(error.ErrorMessage()); -} - template<typename T> bool QueryInterfaceImpl(IUnknown *from, REFIID riid, void **ppvObject) { @@ -124,6 +120,8 @@ public: void setConnectivity(NLM_CONNECTIVITY newConnectivity); private: + QComHelper comHelper; + ComPtr<QNetworkConnectionEvents> connectionEvents; // We can assume we have access to internet/subnet when this class is created because // connection has already been established to the peer: @@ -136,7 +134,6 @@ private: bool sameSubnet = false; bool isLinkLocal = false; bool monitoring = false; - bool comInitFailed = false; bool remoteIsIPv6 = false; }; @@ -147,7 +144,7 @@ QNetworkConnectionEvents::QNetworkConnectionEvents(QNetworkConnectionMonitorPriv IID_INetworkListManager, &networkListManager); if (FAILED(hr)) { qCDebug(lcNetMon) << "Could not get a NetworkListManager instance:" - << errorStringFromHResult(hr); + << QSystemError::windowsComString(hr); return; } @@ -159,7 +156,7 @@ QNetworkConnectionEvents::QNetworkConnectionEvents(QNetworkConnectionMonitorPriv } if (FAILED(hr)) { qCDebug(lcNetMon) << "Failed to get connection point for network events:" - << errorStringFromHResult(hr); + << QSystemError::windowsComString(hr); } } @@ -170,11 +167,17 @@ QNetworkConnectionEvents::~QNetworkConnectionEvents() ComPtr<INetworkConnection> QNetworkConnectionEvents::getNetworkConnectionFromAdapterGuid(QUuid guid) { + if (!networkListManager) { + qCDebug(lcNetMon) << "Failed to enumerate network connections:" + << "NetworkListManager was not instantiated"; + return nullptr; + } + ComPtr<IEnumNetworkConnections> connections; auto hr = networkListManager->GetNetworkConnections(connections.GetAddressOf()); if (FAILED(hr)) { qCDebug(lcNetMon) << "Failed to enumerate network connections:" - << errorStringFromHResult(hr); + << QSystemError::windowsComString(hr); return nullptr; } ComPtr<INetworkConnection> connection = nullptr; @@ -182,7 +185,7 @@ ComPtr<INetworkConnection> QNetworkConnectionEvents::getNetworkConnectionFromAda hr = connections->Next(1, connection.GetAddressOf(), nullptr); if (FAILED(hr)) { qCDebug(lcNetMon) << "Failed to get next network connection in enumeration:" - << errorStringFromHResult(hr); + << QSystemError::windowsComString(hr); break; } if (connection) { @@ -190,7 +193,7 @@ ComPtr<INetworkConnection> QNetworkConnectionEvents::getNetworkConnectionFromAda hr = connection->GetAdapterId(&adapterId); if (FAILED(hr)) { qCDebug(lcNetMon) << "Failed to get adapter ID from network connection:" - << errorStringFromHResult(hr); + << QSystemError::windowsComString(hr); continue; } if (guid == adapterId) @@ -255,7 +258,8 @@ bool QNetworkConnectionEvents::setTarget(const QNetworkInterface &iface) } auto hr = connection->GetConnectionId(&guid); if (FAILED(hr)) { - qCDebug(lcNetMon) << "Failed to get the connection's GUID:" << errorStringFromHResult(hr); + qCDebug(lcNetMon) << "Failed to get the connection's GUID:" + << QSystemError::windowsComString(hr); return false; } currentConnectionId = guid; @@ -278,7 +282,7 @@ bool QNetworkConnectionEvents::startMonitoring() auto hr = connectionPoint->Advise(this, &cookie); if (FAILED(hr)) { qCDebug(lcNetMon) << "Failed to subscribe to network connectivity events:" - << errorStringFromHResult(hr); + << QSystemError::windowsComString(hr); return false; } return true; @@ -289,7 +293,7 @@ bool QNetworkConnectionEvents::stopMonitoring() auto hr = connectionPoint->Unadvise(cookie); if (FAILED(hr)) { qCDebug(lcNetMon) << "Failed to unsubscribe from network connection events:" - << errorStringFromHResult(hr); + << QSystemError::windowsComString(hr); return false; } cookie = 0; @@ -299,30 +303,25 @@ bool QNetworkConnectionEvents::stopMonitoring() QNetworkConnectionMonitorPrivate::QNetworkConnectionMonitorPrivate() { - auto hr = CoInitialize(nullptr); - if (FAILED(hr)) { - qCDebug(lcNetMon) << "Failed to initialize COM:" << errorStringFromHResult(hr); - comInitFailed = true; + if (!comHelper.isValid()) return; - } connectionEvents = new QNetworkConnectionEvents(this); } QNetworkConnectionMonitorPrivate::~QNetworkConnectionMonitorPrivate() { - if (comInitFailed) + if (!comHelper.isValid()) return; if (monitoring) stopMonitoring(); connectionEvents.Reset(); - CoUninitialize(); } bool QNetworkConnectionMonitorPrivate::setTargets(const QHostAddress &local, const QHostAddress &remote) { - if (comInitFailed) + if (!comHelper.isValid()) return false; QNetworkInterface iface = getInterfaceFromHostAddress(local); |