diff options
author | Yuhang Zhao <2546789017@qq.com> | 2022-10-26 13:34:35 +0800 |
---|---|---|
committer | Yuhang Zhao <2546789017@qq.com> | 2022-11-16 19:44:43 +0800 |
commit | bd7fa4a53791ead147393d3a4a797d2e14ee3343 (patch) | |
tree | ef50df5bf2a48546bfe0ac3d884f244c458d0d9c /src/plugins/platforms | |
parent | e2f895db2eb80f2735e89763a047571410565369 (diff) |
Windows: centralize how we handle error messages
Currently QtBase contains multiple implementation of how to get the Win32
and COM error messages, and they are almost exactly the same, what's worse,
Qt already has a private QSystemError class to do such things, so we are
re-inventing the wheel in many places. This patch removes all other custom
error message implementations besides the QSystemError one. And since there
are a lot of places need the COM error message, move the implementation to
QSystemError so that it can handle both Win32 error and COM error.
Since I'm touching these lines anyway, break them into short lines if they
are above the length limit.
Change-Id: I1067c874011800303f0f114b5cb8830ac6810fc0
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/plugins/platforms')
4 files changed, 9 insertions, 100 deletions
diff --git a/src/plugins/platforms/windows/qwindowsclipboard.cpp b/src/plugins/platforms/windows/qwindowsclipboard.cpp index 5b9c4cc340..7a6d41e0b3 100644 --- a/src/plugins/platforms/windows/qwindowsclipboard.cpp +++ b/src/plugins/platforms/windows/qwindowsclipboard.cpp @@ -16,6 +16,7 @@ #include <QtCore/qthread.h> #include <QtCore/qvariant.h> #include <QtCore/qurl.h> +#include <QtCore/private/qsystemerror_p.h> #include <QtGui/private/qwindowsguieventdispatcher_p.h> @@ -300,7 +301,7 @@ void QWindowsClipboard::setMimeData(QMimeData *mimeData, QClipboard::Mode mode) mimeData->formats().join(u", ") : QString(QStringLiteral("NULL")); qErrnoWarning("OleSetClipboard: Failed to set mime data (%s) on clipboard: %s", qPrintable(mimeDataFormats), - QWindowsContext::comErrorString(src).constData()); + qPrintable(QSystemError::windowsComString(src))); releaseIData(); return; } diff --git a/src/plugins/platforms/windows/qwindowscontext.cpp b/src/plugins/platforms/windows/qwindowscontext.cpp index 0242b4096c..e11bb5e335 100644 --- a/src/plugins/platforms/windows/qwindowscontext.cpp +++ b/src/plugins/platforms/windows/qwindowscontext.cpp @@ -46,13 +46,13 @@ #include <QtCore/quuid.h> #include <QtCore/private/qwinregistry_p.h> #include <QtCore/private/qfactorycacheregistration_p.h> +#include <QtCore/private/qsystemerror_p.h> #include <QtGui/private/qwindowsguieventdispatcher_p.h> #include <stdlib.h> #include <stdio.h> #include <windowsx.h> -#include <comdef.h> #include <dbt.h> #include <wtsapi32.h> #include <shellscalingapi.h> @@ -192,7 +192,7 @@ QWindowsContextPrivate::QWindowsContextPrivate() m_darkMode = QWindowsTheme::queryDarkMode(); if (FAILED(m_oleInitializeResult)) { qWarning() << "QWindowsContext: OleInitialize() failed: " - << QWindowsContext::comErrorString(m_oleInitializeResult); + << QSystemError::windowsComString(m_oleInitializeResult); } } @@ -383,7 +383,7 @@ void QWindowsContext::setProcessDpiAwareness(QtWindows::ProcessDpiAwareness dpiA // Silence warning in that case unless debug is enabled. if (FAILED(hr) && hr != E_ACCESSDENIED) { qCWarning(lcQpaWindow).noquote().nospace() << "SetProcessDpiAwareness(" - << dpiAwareness << ") failed: " << QWindowsContext::comErrorString(hr) + << dpiAwareness << ") failed: " << QSystemError::windowsComString(hr) << ", using " << QWindowsContext::processDpiAwareness(); } } @@ -399,7 +399,7 @@ bool QWindowsContext::setProcessDpiV2Awareness() if (dwError != ERROR_ACCESS_DENIED) { qCWarning(lcQpaWindow).noquote().nospace() << "SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2) failed: " - << QWindowsContext::comErrorString(HRESULT(dwError)); + << QSystemError::windowsComString(HRESULT(dwError)); return false; } } @@ -622,23 +622,6 @@ int QWindowsContext::screenDepth() const return GetDeviceCaps(d->m_displayContext, BITSPIXEL); } -QString QWindowsContext::windowsErrorMessage(unsigned long errorCode) -{ - QString rc = QString::fromLatin1("#%1: ").arg(errorCode); - char16_t *lpMsgBuf; - - const DWORD len = FormatMessage( - FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - nullptr, errorCode, 0, reinterpret_cast<LPTSTR>(&lpMsgBuf), 0, nullptr); - if (len) { - rc = QString::fromUtf16(lpMsgBuf, int(len)); - LocalFree(lpMsgBuf); - } else { - rc += QString::fromLatin1("<unknown error>"); - } - return rc; -} - void QWindowsContext::addWindow(HWND hwnd, QWindowsWindow *w) { d->m_windows.insert(hwnd, w); @@ -839,79 +822,6 @@ HWND QWindowsContext::createDummyWindow(const QString &classNameIn, HWND_MESSAGE, nullptr, static_cast<HINSTANCE>(GetModuleHandle(nullptr)), nullptr); } -/*! - \brief Common COM error strings. -*/ - -QByteArray QWindowsContext::comErrorString(HRESULT hr) -{ - QByteArray result = QByteArrayLiteral("COM error 0x") - + QByteArray::number(quintptr(hr), 16) + ' '; - switch (hr) { - case S_OK: - result += QByteArrayLiteral("S_OK"); - break; - case S_FALSE: - result += QByteArrayLiteral("S_FALSE"); - break; - case E_UNEXPECTED: - result += QByteArrayLiteral("E_UNEXPECTED"); - break; - case E_ACCESSDENIED: - result += QByteArrayLiteral("E_ACCESSDENIED"); - break; - case CO_E_ALREADYINITIALIZED: - result += QByteArrayLiteral("CO_E_ALREADYINITIALIZED"); - break; - case CO_E_NOTINITIALIZED: - result += QByteArrayLiteral("CO_E_NOTINITIALIZED"); - break; - case RPC_E_CHANGED_MODE: - result += QByteArrayLiteral("RPC_E_CHANGED_MODE"); - break; - case OLE_E_WRONGCOMPOBJ: - result += QByteArrayLiteral("OLE_E_WRONGCOMPOBJ"); - break; - case CO_E_NOT_SUPPORTED: - result += QByteArrayLiteral("CO_E_NOT_SUPPORTED"); - break; - case E_NOTIMPL: - result += QByteArrayLiteral("E_NOTIMPL"); - break; - case E_INVALIDARG: - result += QByteArrayLiteral("E_INVALIDARG"); - break; - case E_NOINTERFACE: - result += QByteArrayLiteral("E_NOINTERFACE"); - break; - case E_POINTER: - result += QByteArrayLiteral("E_POINTER"); - break; - case E_HANDLE: - result += QByteArrayLiteral("E_HANDLE"); - break; - case E_ABORT: - result += QByteArrayLiteral("E_ABORT"); - break; - case E_FAIL: - result += QByteArrayLiteral("E_FAIL"); - break; - case RPC_E_WRONG_THREAD: - result += QByteArrayLiteral("RPC_E_WRONG_THREAD"); - break; - case RPC_E_THREAD_NOT_INIT: - result += QByteArrayLiteral("RPC_E_THREAD_NOT_INIT"); - break; - default: - break; - } - _com_error error(hr); - result += QByteArrayLiteral(" ("); - result += QString::fromWCharArray(error.ErrorMessage()).toUtf8(); - result += ')'; - return result; -} - void QWindowsContext::forceNcCalcSize(HWND hwnd) { // Force WM_NCCALCSIZE to adjust margin diff --git a/src/plugins/platforms/windows/qwindowscontext.h b/src/plugins/platforms/windows/qwindowscontext.h index d7c9a7f3a5..6b3010f33c 100644 --- a/src/plugins/platforms/windows/qwindowscontext.h +++ b/src/plugins/platforms/windows/qwindowscontext.h @@ -88,8 +88,6 @@ public: static QWindowsContext *instance(); - static QString windowsErrorMessage(unsigned long errorCode); - void addWindow(HWND, QWindowsWindow *w); void removeWindow(HWND); @@ -139,7 +137,6 @@ public: QWindowsScreenManager &screenManager(); QWindowsTabletSupport *tabletSupport() const; - static QByteArray comErrorString(HRESULT hr); bool asyncExpose() const; void setAsyncExpose(bool value); diff --git a/src/plugins/platforms/windows/qwindowsdialoghelpers.cpp b/src/plugins/platforms/windows/qwindowsdialoghelpers.cpp index c81e53e7ab..8f0cd3857e 100644 --- a/src/plugins/platforms/windows/qwindowsdialoghelpers.cpp +++ b/src/plugins/platforms/windows/qwindowsdialoghelpers.cpp @@ -31,6 +31,7 @@ #include <QtCore/qmutex.h> #include <QtCore/quuid.h> #include <QtCore/qtemporaryfile.h> +#include <QtCore/private/qsystemerror_p.h> #include <algorithm> #include <vector> @@ -631,7 +632,7 @@ bool QWindowsShellItem::copyData(QIODevice *out, QString *errorMessage) HRESULT hr = m_item->BindToHandler(nullptr, BHID_Stream, IID_PPV_ARGS(&istream)); if (FAILED(hr)) { *errorMessage = "BindToHandler() failed: "_L1 - + QLatin1StringView(QWindowsContext::comErrorString(hr)); + + QSystemError::windowsComString(hr); return false; } enum : ULONG { bufSize = 102400 }; @@ -648,7 +649,7 @@ bool QWindowsShellItem::copyData(QIODevice *out, QString *errorMessage) istream->Release(); if (hr != S_OK && hr != S_FALSE) { *errorMessage = "Read() failed: "_L1 - + QLatin1StringView(QWindowsContext::comErrorString(hr)); + + QSystemError::windowsComString(hr); return false; } return true; |