|
2 | 2 |
|
3 | 3 | #define CATCH_ALL_EXCEPTIONS() \ |
4 | 4 | catch (const HResultException& ex) { \ |
5 | | - AtlReportError(GetObjectCLSID(), ex.LMessage(), __uuidof(IUnknown), ex.HResult()); \ |
| 5 | + ATL::AtlReportError(GetObjectCLSID(), ex.what(), __uuidof(IUnknown), ex.HResult()); \ |
6 | 6 | return ex.HResult(); \ |
7 | 7 | } \ |
8 | | - catch (const WException& ex) { \ |
9 | | - AtlReportError(GetObjectCLSID(), ex.LMessage(), __uuidof(IUnknown), E_FAIL); \ |
10 | | - return E_FAIL; \ |
11 | | - } \ |
12 | 8 | catch (const std::exception& ex) { \ |
13 | | - AtlReportError(GetObjectCLSID(), ex.what(), __uuidof(IUnknown), E_FAIL); \ |
| 9 | + ATL::AtlReportError(GetObjectCLSID(), ex.what(), __uuidof(IUnknown), E_FAIL); \ |
14 | 10 | return E_FAIL; \ |
15 | 11 | } \ |
16 | 12 | catch (...) { \ |
17 | | - AtlReportError(GetObjectCLSID(), L"Unknown exception", __uuidof(IUnknown), E_FAIL); \ |
| 13 | + ATL::AtlReportError(GetObjectCLSID(), L"Unknown exception", __uuidof(IUnknown), E_FAIL); \ |
18 | 14 | return E_FAIL; \ |
19 | 15 | } |
20 | 16 |
|
|
36 | 32 | } \ |
37 | 33 | } while (0) |
38 | 34 |
|
39 | | -class WException : public std::exception |
40 | | -{ |
| 35 | +#define THROW_WINAPI_EX(winApiFuncName) do { throw WinApiException(BOOST_PP_WSTRINGIZE(winApiFuncName), LINE_INFO); } while(0) |
| 36 | +#define THROW_WINAPI_EX_MSG(winApiFuncName, ...) do { throw WinApiException(BOOST_PP_WSTRINGIZE(winApiFuncName), LINE_INFO, std::format(__VA_ARGS__)); } while(0) |
| 37 | + |
| 38 | +class WException : public std::exception { |
41 | 39 | public: |
42 | 40 | WException(const std::wstring_view msg, const std::wstring_view lineInfo) |
43 | | - : msg(std::format(L"{}: {}", lineInfo, msg)) { } |
44 | | - |
45 | | - auto LMessage() const { return msg.data(); } |
46 | | - |
47 | | -private: |
48 | | - std::wstring msg; |
| 41 | + : std::exception(ToUtf8(std::format(L"{}: {}", lineInfo, msg)).c_str()) {} |
49 | 42 | }; |
50 | 43 |
|
51 | | -class HResultException : public WException |
52 | | -{ |
| 44 | +class HResultException : public WException { |
53 | 45 | public: |
54 | 46 | HResultException(const HRESULT res, const std::wstring_view msg, const std::wstring_view lineInfo) : |
55 | 47 | WException( |
56 | 48 | std::format(L"{}. HRESULT=0x{:08X}({}): {}", msg, static_cast<unsigned long>(res), static_cast<unsigned long>(res), _com_error(res).ErrorMessage()), |
57 | 49 | lineInfo), |
58 | | - res(res) { } |
| 50 | + res(res) {} |
59 | 51 |
|
60 | 52 | auto HResult() const { return res; } |
61 | 53 |
|
62 | 54 | private: |
63 | 55 | HRESULT res; |
64 | 56 | }; |
| 57 | + |
| 58 | +class WinApiException final : public WException { |
| 59 | +public: |
| 60 | + explicit WinApiException(std::wstring_view winApiFuncName, const std::wstring_view lineInfo, std::wstring_view msg = L"") |
| 61 | + : WException{ |
| 62 | + std::format(L"WinApi function '{}' failed. {}. ErrorCode: 0x{:08X}({}). ErrorMessage: {}", winApiFuncName, msg, GetLastError(), GetLastError(), GetLastErrorMessage()), |
| 63 | + lineInfo } {} |
| 64 | + |
| 65 | +private: |
| 66 | + std::wstring GetLastErrorMessage() const { |
| 67 | + return ToUtf16(boost::system::error_code(GetLastError(), boost::system::system_category()).message()); |
| 68 | + } |
| 69 | +}; |
0 commit comments