From f778e698c907c08e17fc467e672fe02bc51f24fe Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Fri, 8 Dec 2023 10:09:47 +0100 Subject: [PATCH] COM: Make com_initializer constructors throw on error This aligns the class with other COM classes (see com_obj) and makes client code simpler as it does not need to check for error explicitly. Signed-off-by: Simon Rozman --- include/WinStd/COM.h | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/include/WinStd/COM.h b/include/WinStd/COM.h index d06b0eb4..1a66b0f2 100644 --- a/include/WinStd/COM.h +++ b/include/WinStd/COM.h @@ -1144,9 +1144,11 @@ namespace winstd /// /// \sa [CoInitialize function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms678543.aspx) /// - com_initializer(_In_opt_ LPVOID pvReserved) noexcept + com_initializer(_In_opt_ LPVOID pvReserved) { - m_result = CoInitialize(pvReserved); + HRESULT hr = CoInitialize(pvReserved); + if (FAILED(hr)) + throw com_runtime_error(hr, "CoInitialize failed"); } /// @@ -1154,9 +1156,11 @@ namespace winstd /// /// \sa [CoInitializeEx function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms695279.aspx) /// - com_initializer(_In_opt_ LPVOID pvReserved, _In_ DWORD dwCoInit) noexcept + com_initializer(_In_opt_ LPVOID pvReserved, _In_ DWORD dwCoInit) { - m_result = CoInitializeEx(pvReserved, dwCoInit); + HRESULT hr = CoInitializeEx(pvReserved, dwCoInit); + if (FAILED(hr)) + throw com_runtime_error(hr, "CoInitializeEx failed"); } /// @@ -1166,22 +1170,8 @@ namespace winstd /// virtual ~com_initializer() { - if (SUCCEEDED(m_result)) - CoUninitialize(); + CoUninitialize(); } - - /// - /// Return result of `CoInitialize()` call. - /// - /// \sa [CoInitialize function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms678543.aspx) - /// - HRESULT status() const noexcept - { - return m_result; - } - - protected: - HRESULT m_result; ///< Result of CoInitialize call }; /// @}