Win: Move hresultToQString from vfs plugin to Utility::formatWinError
authorHannah von Reth <hannah.vonreth@owncloud.com>
Wed, 8 Jul 2020 10:38:01 +0000 (12:38 +0200)
committerKevin Ottens <kevin.ottens@nextcloud.com>
Tue, 15 Dec 2020 09:59:16 +0000 (10:59 +0100)
src/common/filesystembase.cpp
src/common/utility.h
src/common/utility_win.cpp

index 491a74e3abf4c20beee88f21494b600a49d2781f..3bc0a11df2d1b2fcd02dc434421bd38f08178efc 100644 (file)
@@ -17,6 +17,7 @@
  */
 
 #include "filesystembase.h"
+#include "utility.h"
 
 #include <QDateTime>
 #include <QDir>
@@ -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
index 9e2d458d68a6dbf26bd9d7b4f90e3c20047a5003..3fe99e82107ce64b1ceec01f8864524fb32c42c4 100644 (file)
@@ -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
index 20d2e62a86e108d0d7cc1cc9847c251606246a8b..f2dc8ad68feaa61cdabae24a37e3cd33c170e90a 100644 (file)
  */
 
 #include "asserts.h"
+#include "utility.h"
+
+#include <comdef.h>
+#include <shlguid.h>
 #include <shlobj.h>
+#include <string>
 #include <winbase.h>
 #include <windows.h>
 #include <winerror.h>
-#include <shlguid.h>
-#include <string>
+
 #include <QLibrary>
 
 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