diff --git a/src/common/filesystembase.cpp b/src/common/filesystembase.cpp index 491a74e3a..3bc0a11df 100644 --- a/src/common/filesystembase.cpp +++ b/src/common/filesystembase.cpp @@ -17,6 +17,7 @@ */ #include "filesystembase.h" +#include "utility.h" #include #include @@ -140,13 +141,7 @@ bool FileSystem::rename(const QString &originFileName, (wchar_t *)dest.utf16(), MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH); if (!success) { - wchar_t *string = 0; - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, - nullptr, ::GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPWSTR)&string, 0, nullptr); - - error = QString::fromWCharArray(string); - LocalFree((HLOCAL)string); + error = Utility::formatWinError(); } } else #endif diff --git a/src/common/utility.h b/src/common/utility.h index 9e2d458d6..3fe99e821 100644 --- a/src/common/utility.h +++ b/src/common/utility.h @@ -249,6 +249,8 @@ namespace Utility { OCSYNC_EXPORT void FiletimeToLargeIntegerFiletime(FILETIME *filetime, LARGE_INTEGER *hundredNSecs); OCSYNC_EXPORT void UnixTimeToLargeIntegerFiletime(time_t t, LARGE_INTEGER *hundredNSecs); + OCSYNC_EXPORT QString formatWinError(long error = GetLastError()); + #endif } /** @} */ // \addtogroup diff --git a/src/common/utility_win.cpp b/src/common/utility_win.cpp index 20d2e62a8..f2dc8ad68 100644 --- a/src/common/utility_win.cpp +++ b/src/common/utility_win.cpp @@ -17,12 +17,16 @@ */ #include "asserts.h" +#include "utility.h" + +#include +#include #include +#include #include #include #include -#include -#include + #include static const char systemRunPathC[] = R"(HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run)"; @@ -334,4 +338,10 @@ void Utility::UnixTimeToLargeIntegerFiletime(time_t t, LARGE_INTEGER *hundredNSe hundredNSecs->HighPart = ll >>32; } + +QString Utility::formatWinError(long errorCode) +{ + return QStringLiteral("WindowsError: %1: %2").arg(QString::number(errorCode), QString::fromWCharArray(_com_error(errorCode).ErrorMessage())); +} + } // namespace OCC