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/tools/windeployqt | |
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/tools/windeployqt')
-rw-r--r-- | src/tools/windeployqt/utils.cpp | 32 |
1 files changed, 11 insertions, 21 deletions
diff --git a/src/tools/windeployqt/utils.cpp b/src/tools/windeployqt/utils.cpp index 7e1f485d68..7f4763a850 100644 --- a/src/tools/windeployqt/utils.cpp +++ b/src/tools/windeployqt/utils.cpp @@ -15,6 +15,7 @@ #include <QtCore/QStandardPaths> #if defined(Q_OS_WIN) # include <QtCore/qt_windows.h> +# include <QtCore/private/qsystemerror_p.h> # include <shlwapi.h> # include <delayimp.h> #else // Q_OS_WIN @@ -116,22 +117,6 @@ QStringList findSharedLibraries(const QDir &directory, Platform platform, } #ifdef Q_OS_WIN -QString winErrorMessage(unsigned long error) -{ - QString rc = QString::fromLatin1("#%1: ").arg(error); - char16_t *lpMsgBuf; - - const DWORD len = FormatMessage( - FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, error, 0, reinterpret_cast<LPTSTR>(&lpMsgBuf), 0, NULL); - if (len) { - rc = QString::fromUtf16(lpMsgBuf, int(len)); - LocalFree(lpMsgBuf); - } else { - rc += QString::fromLatin1("<unknown error>"); - } - return rc; -} // Case-Normalize file name via GetShortPathNameW()/GetLongPathNameW() QString normalizeFileName(const QString &name) @@ -263,8 +248,10 @@ bool runProcess(const QString &binary, const QStringList &args, CloseHandle(si.hStdOutput); if (stdErr) CloseHandle(si.hStdError); - if (errorMessage) - *errorMessage = QStringLiteral("CreateProcessW failed: ") + winErrorMessage(GetLastError()); + if (errorMessage) { + *errorMessage = QStringLiteral("CreateProcessW failed: ") + + QSystemError::windowsString(); + } return false; } @@ -778,19 +765,22 @@ bool readPeExecutable(const QString &peExecutableFileName, QString *errorMessage hFile = CreateFile(reinterpret_cast<const WCHAR*>(peExecutableFileName.utf16()), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE || hFile == NULL) { - *errorMessage = QString::fromLatin1("Cannot open '%1': %2").arg(peExecutableFileName, winErrorMessage(GetLastError())); + *errorMessage = QString::fromLatin1("Cannot open '%1': %2") + .arg(peExecutableFileName, QSystemError::windowsString()); break; } hFileMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL); if (hFileMap == NULL) { - *errorMessage = QString::fromLatin1("Cannot create file mapping of '%1': %2").arg(peExecutableFileName, winErrorMessage(GetLastError())); + *errorMessage = QString::fromLatin1("Cannot create file mapping of '%1': %2") + .arg(peExecutableFileName, QSystemError::windowsString()); break; } fileMemory = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0); if (!fileMemory) { - *errorMessage = QString::fromLatin1("Cannot map '%1': %2").arg(peExecutableFileName, winErrorMessage(GetLastError())); + *errorMessage = QString::fromLatin1("Cannot map '%1': %2") + .arg(peExecutableFileName, QSystemError::windowsString()); break; } |