diff options
author | Mike Kaganski <mike.kaganski@collabora.com> | 2017-11-02 09:11:58 +0300 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2017-11-02 10:26:16 +0100 |
commit | cee129bf17bd604f96e3cfe62d3a55336e248ccd (patch) | |
tree | fc30901b18f4d7809ff01c752c035ec3ff8cac0a /include/comphelper | |
parent | 4c404950225e07ec7ee5275657a3192a5af79a04 (diff) |
Improve failed HRESULT reporting on debug
Change-Id: Ib69b72f64e8cbaef75ec88aa6b6c49383e5fa1cb
Reviewed-on: https://gerrit.libreoffice.org/44187
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'include/comphelper')
-rw-r--r-- | include/comphelper/windowserrorstring.hxx | 23 |
1 files changed, 23 insertions, 0 deletions
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 |