11# include <qt_windows.h>
14#ifndef QT_BOOTSTRAPPED
15# include <qcoreapplication.h>
20using namespace Qt::StringLiterals;
22#if !defined(Q_OS_WIN) && QT_CONFIG(thread) && !defined(Q_OS_INTEGRITY) && !defined(Q_OS_QNX) &&
23 defined(_POSIX_THREAD_SAFE_FUNCTIONS) && _POSIX_VERSION >= 200112L
34 [[maybe_unused]]
static inline QString fromstrerror_helper(
int,
const QByteArray &buf)
36 return QString::fromLocal8Bit(buf);
38 [[maybe_unused]]
static inline QString fromstrerror_helper(
const char *str,
const QByteArray &)
40 return QString::fromLocal8Bit(str);
46static QString windowsErrorString(
int errorCode)
49 wchar_t *string =
nullptr;
50 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
53 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
57 ret = QString::fromWCharArray(string);
58 LocalFree((HLOCAL)string);
60 if (ret.isEmpty() && errorCode == ERROR_MOD_NOT_FOUND)
61 ret = QString::fromLatin1(
"The specified module could not be found.");
62 if (ret.endsWith(
"\r\n"_L1))
65 ret = QString::fromLatin1(
"Unknown error 0x%1.")
66 .arg(
unsigned(errorCode), 8, 16,
'0'_L1);
73 const char *s =
nullptr;
79 s = QT_TRANSLATE_NOOP(
"QIODevice",
"Permission denied");
82 s = QT_TRANSLATE_NOOP(
"QIODevice",
"Too many open files");
85 s = QT_TRANSLATE_NOOP(
"QIODevice",
"No such file or directory");
88 s = QT_TRANSLATE_NOOP(
"QIODevice",
"No space left on device");
91 #if QT_CONFIG(thread) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && _POSIX_VERSION >= 200112L
&& !defined(Q_OS_INTEGRITY) && !defined(Q_OS_QNX)
92 QByteArray buf(1024, Qt::Uninitialized);
93 ret = fromstrerror_helper(strerror_r(errorCode, buf.data(), buf.size()), buf);
95 ret = QString::fromLocal8Bit(strerror(errorCode));
100#ifndef QT_BOOTSTRAPPED
101 ret = QCoreApplication::translate(
"QIODevice", s);
103 ret = QString::fromLatin1(s);
106 return ret.trimmed();
111 switch (errorScope) {
114 return windowsErrorString(errorCode);
116 case StandardLibraryError:
117 return standardLibraryErrorString(errorCode);
119 qWarning(
"invalid error scope");
122 return u"No error"_s;
128 return standardLibraryErrorString(errorCode == -1 ? errno : errorCode);
132QString QSystemError::windowsString(
int errorCode)
134 return windowsErrorString(errorCode == -1 ? GetLastError() : errorCode);
137QString QSystemError::windowsComString(HRESULT hr)
139 const _com_error comError(hr);
140 QString result = u"COM error 0x"_s + QString::number(ulong(hr), 16);
141 if (
const wchar_t *msg = comError.ErrorMessage())
142 result += u": "_s + QString::fromWCharArray(msg);
146QString qt_error_string(
int code)
148 return windowsErrorString(code == -1 ? GetLastError() : code);
153 return standardLibraryErrorString(code == -1 ? errno : code);
QString qt_error_string(int code)
static QString standardLibraryErrorString(int errorCode)