diff options
Diffstat (limited to 'shared/com_error.h')
-rw-r--r-- | shared/com_error.h | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/shared/com_error.h b/shared/com_error.h new file mode 100644 index 00000000..ab365977 --- /dev/null +++ b/shared/com_error.h @@ -0,0 +1,77 @@ +// ************************************************************************** +// * This file is part of the FreeFileSync project. It is distributed under * +// * GNU General Public License: http://www.gnu.org/licenses/gpl.html * +// * Copyright (C) 2008-2010 ZenJu (zhnmju123 AT gmx.de) * +// ************************************************************************** +// +#ifndef COM_ERROR_HEADER +#define COM_ERROR_HEADER + +#include <string> +#include <cstdio> +#include <comdef.h> + +namespace util +{ +std::wstring generateErrorMsg(const std::wstring& input, HRESULT hr); + + +class ComException +{ +public: + ComException(const std::wstring& message, HRESULT hr = NO_ERROR) + : message_(message), hr_(hr) {} + + std::wstring show() const + { + return hr_ != NO_ERROR ? generateErrorMsg(message_, hr_) : message_; + } + +private: + const std::wstring message_; + const HRESULT hr_; +}; + + + + + + + + + + + + + + + + + + + + + +//################# implementation ##################### +inline +std::wstring numberToHexString(long number) +{ + wchar_t result[100]; + swprintf(result, 100, L"0x%08x", number); + return std::wstring(result); +} + + +inline +std::wstring generateErrorMsg(const std::wstring& input, HRESULT hr) +{ + std::wstring output(input); + output += L" ("; + output += numberToHexString(hr); + output += L": "; + output += _com_error(hr).ErrorMessage(); + output += L")"; + return output; +} +} +#endif //COM_ERROR_HEADER
\ No newline at end of file |