From cee129bf17bd604f96e3cfe62d3a55336e248ccd Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Thu, 2 Nov 2017 09:11:58 +0300 Subject: Improve failed HRESULT reporting on debug Change-Id: Ib69b72f64e8cbaef75ec88aa6b6c49383e5fa1cb Reviewed-on: https://gerrit.libreoffice.org/44187 Tested-by: Jenkins Reviewed-by: Mike Kaganski --- include/comphelper/windowserrorstring.hxx | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'include') 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 -- cgit