diff options
author | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:15:39 +0200 |
---|---|---|
committer | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:15:39 +0200 |
commit | d2854834e18443876c8f75e0a7f3b88d1d549fc4 (patch) | |
tree | e967b628081e50abc7c34cd264e6586271c7e728 /zen/com_error.h | |
parent | 4.1 (diff) | |
download | FreeFileSync-d2854834e18443876c8f75e0a7f3b88d1d549fc4.tar.gz FreeFileSync-d2854834e18443876c8f75e0a7f3b88d1d549fc4.tar.bz2 FreeFileSync-d2854834e18443876c8f75e0a7f3b88d1d549fc4.zip |
4.2
Diffstat (limited to 'zen/com_error.h')
-rw-r--r-- | zen/com_error.h | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/zen/com_error.h b/zen/com_error.h index 2ba76c0f..4546bd8a 100644 --- a/zen/com_error.h +++ b/zen/com_error.h @@ -16,6 +16,29 @@ namespace zen std::wstring generateErrorMsg(const std::wstring& input, HRESULT hr); std::wstring formatWin32Msg(DWORD dwMessageId); //return empty string on error +class ComError +{ +public: + explicit ComError(const std::wstring& msg, HRESULT hr = S_OK) : msg_(hr == S_OK ? msg : generateErrorMsg(msg, hr)) {} + const std::wstring& toString() const { return msg_; } + +private: + std::wstring msg_; +}; + +#define ZEN_CHECK_COM(func) ZEN_CHECK_COM_ERROR(func, #func) //throw ComError +/*Convenience Macro checking for COM errors: + +Example: ZEN_CHECK_COM(backupComp->InitializeForBackup()); + +Equivalent to: +{ + HRESULT hrInternal = backupComp->InitializeForBackup(); + if (FAILED(hrInternal)) + throw ComError(L"Error calling \"backupComp->InitializeForBackup()\".", hrInternal); +} +*/ + @@ -202,5 +225,18 @@ std::wstring generateErrorMsg(const std::wstring& input, HRESULT hr) } return output; } + + +#define ZEN_CHECK_COM_ERROR(func, txt) \ + { \ + HRESULT hrInternal = func; \ + if (FAILED(hrInternal)) \ + throw ComError(L"Error calling \"" ## ZEN_CONCAT_SUB(L, txt) ## L"\".", hrInternal); \ + } + +#ifndef ZEN_CONCAT //redeclare those macros: avoid dependency to scope_guard.h +#define ZEN_CONCAT_SUB(X, Y) X ## Y +#define ZEN_CONCAT(X, Y) ZEN_CONCAT_SUB(X, Y) +#endif } #endif //COM_ERROR_HEADER |