[Libreoffice-commits] core.git: include/comphelper vcl/win

Mike Kaganski mike.kaganski at collabora.com
Thu Nov 2 09:26:42 UTC 2017


 include/comphelper/windowserrorstring.hxx |   23 +++++++++++++++++++++++
 vcl/win/gdi/DWriteTextRenderer.cxx        |    4 +++-
 2 files changed, 26 insertions(+), 1 deletion(-)

New commits:
commit cee129bf17bd604f96e3cfe62d3a55336e248ccd
Author: Mike Kaganski <mike.kaganski at collabora.com>
Date:   Thu Nov 2 09:11:58 2017 +0300

    Improve failed HRESULT reporting on debug
    
    Change-Id: Ib69b72f64e8cbaef75ec88aa6b6c49383e5fa1cb
    Reviewed-on: https://gerrit.libreoffice.org/44187
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Mike Kaganski <mike.kaganski at collabora.com>

diff --git a/include/comphelper/windowserrorstring.hxx b/include/comphelper/windowserrorstring.hxx
index b8cac3f8dbe9..4e401571a2fb 100644
--- a/include/comphelper/windowserrorstring.hxx
+++ b/include/comphelper/windowserrorstring.hxx
@@ -38,6 +38,29 @@ inline OUString WindowsErrorString(DWORD nErrorCode)
     return result;
 }
 
+inline OUString WindowsErrorStringFromHRESULT(HRESULT hr)
+{
+    // See https://blogs.msdn.microsoft.com/oldnewthing/20061103-07/?p=29133
+    // Also https://social.msdn.microsoft.com/Forums/vstudio/en-US/c33d9a4a-1077-4efd-99e8-0c222743d2f8
+    // (which refers to https://msdn.microsoft.com/en-us/library/aa382475)
+    // explains why can't we just reinterpret_cast HRESULT to DWORD Win32 error:
+    // we might actually have a Win32 error code converted using HRESULT_FROM_WIN32 macro
+
+    DWORD nErrorCode = DWORD(hr);
+    if ((hr & 0xFFFF0000) == MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, 0) || hr == S_OK)
+    {
+        nErrorCode = HRESULT_CODE(hr);
+        // https://msdn.microsoft.com/en-us/library/ms679360 mentions that the codes might have
+        // high word bits set (e.g., bit 29 could be set if error comes from a 3rd-party library).
+        // So try to restore the original error code to avoid wrong error messages
+        DWORD nLastError = GetLastError();
+        if ((nLastError & 0xFFFF) == nErrorCode)
+            nErrorCode = nLastError;
+    }
+
+    return WindowsErrorString(nErrorCode);
+}
+
 } // anonymous namespace
 
 #endif
diff --git a/vcl/win/gdi/DWriteTextRenderer.cxx b/vcl/win/gdi/DWriteTextRenderer.cxx
index 9819ef205aa1..f043b72ce4f7 100644
--- a/vcl/win/gdi/DWriteTextRenderer.cxx
+++ b/vcl/win/gdi/DWriteTextRenderer.cxx
@@ -30,6 +30,8 @@
 #include <shlwapi.h>
 #include <winver.h>
 
+#include <comphelper/windowserrorstring.hxx>
+
 HINSTANCE D2DWriteTextOutRenderer::mmD2d1 = nullptr,
           D2DWriteTextOutRenderer::mmDWrite = nullptr;
 D2DWriteTextOutRenderer::pD2D1CreateFactory_t D2DWriteTextOutRenderer::D2D1CreateFactory = nullptr;
@@ -118,7 +120,7 @@ HRESULT checkResult(HRESULT hr, const char* file, size_t line)
         OUString sLocationString = OUString::createFromAscii(file) + ":" + OUString::number(line) + " ";
         SAL_DETAIL_LOG_STREAM(SAL_DETAIL_ENABLE_LOG_WARN, ::SAL_DETAIL_LOG_LEVEL_WARN,
                               "vcl.gdi", sLocationString.toUtf8().getStr(),
-                              "HRESULT failed with: " << (int(hr)));
+                              "HRESULT failed with: 0x" << OUString::number(hr, 16) << ": " << WindowsErrorStringFromHRESULT(hr));
     }
     return hr;
 }


More information about the Libreoffice-commits mailing list