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/corelib/kernel | |
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/corelib/kernel')
-rw-r--r-- | src/corelib/kernel/qsystemerror.cpp | 12 | ||||
-rw-r--r-- | src/corelib/kernel/qsystemerror_p.h | 2 |
2 files changed, 13 insertions, 1 deletions
diff --git a/src/corelib/kernel/qsystemerror.cpp b/src/corelib/kernel/qsystemerror.cpp index 05edd9b5c3..428ce62984 100644 --- a/src/corelib/kernel/qsystemerror.cpp +++ b/src/corelib/kernel/qsystemerror.cpp @@ -9,6 +9,7 @@ #endif #ifdef Q_OS_WIN # include <qt_windows.h> +# include <comdef.h> #endif #ifndef QT_BOOTSTRAPPED # include <qcoreapplication.h> @@ -46,7 +47,7 @@ static QString windowsErrorString(int errorCode) { QString ret; wchar_t *string = nullptr; - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM, + FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), @@ -133,6 +134,15 @@ QString QSystemError::windowsString(int errorCode) return windowsErrorString(errorCode == -1 ? GetLastError() : errorCode); } +QString QSystemError::windowsComString(HRESULT hr) +{ + const _com_error comError(hr); + QString result = "COM error 0x"_L1 + QString::number(ulong(hr), 16); + if (const wchar_t *msg = comError.ErrorMessage()) + result += ": "_L1 + QString::fromWCharArray(msg); + return result; +} + QString qt_error_string(int code) { return windowsErrorString(code == -1 ? GetLastError() : code); diff --git a/src/corelib/kernel/qsystemerror_p.h b/src/corelib/kernel/qsystemerror_p.h index 9f3ec7427d..66c434cb13 100644 --- a/src/corelib/kernel/qsystemerror_p.h +++ b/src/corelib/kernel/qsystemerror_p.h @@ -44,6 +44,8 @@ public: static Q_CORE_EXPORT QString stdString(int errorCode = -1); #ifdef Q_OS_WIN static Q_CORE_EXPORT QString windowsString(int errorCode = -1); + using HRESULT = long; + static Q_CORE_EXPORT QString windowsComString(HRESULT hr); #endif // data members |