diff options
author | Mike Kaganski <mike.kaganski@collabora.com> | 2021-12-21 11:47:30 +0300 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2021-12-21 13:45:55 +0100 |
commit | 996610352fd0fc5d57a9231fa7fb3d43533863d6 (patch) | |
tree | 06103facf8acbbda8089f35d0e34226aa54f4dd2 /include/systools | |
parent | 26f46b861d8504033685b2eec4d003eef8109a27 (diff) |
Use sal::systools::COMReference in getAdoDatalink
Change-Id: If0c474209da5390c0c6e28c11ca26a1c915ab51f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/127218
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'include/systools')
-rw-r--r-- | include/systools/win32/comtools.hxx | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/include/systools/win32/comtools.hxx b/include/systools/win32/comtools.hxx index 2e19f1f9e677..c6f9c4f200f7 100644 --- a/include/systools/win32/comtools.hxx +++ b/include/systools/win32/comtools.hxx @@ -20,6 +20,7 @@ #pragma once #include <string> +#include <string_view> #include <stdexcept> #include <type_traits> #include <utility> @@ -45,6 +46,38 @@ namespace sal::systools HRESULT hr_; }; + /* Convert failed HRESULT to thrown ComError */ + inline void ThrowIfFailed(HRESULT hr, std::string_view msg) + { + if (FAILED(hr)) + throw ComError(std::string(msg), hr); + } + + /* A guard class to call CoInitializeEx/CoUninitialize in proper pairs + * See also: o3tl::safeCoInitializeEx doing dangerous re-initialization + */ + class CoInitializeGuard + { + public: + explicit CoInitializeGuard(DWORD dwCoInit, bool bThrowOnChangeMode = false) + { + HRESULT hr = ::CoInitializeEx(nullptr, dwCoInit); + if (FAILED(hr) && (bThrowOnChangeMode || hr != RPC_E_CHANGED_MODE)) + throw ComError("CoInitializeEx failed", hr); + mbUninit = SUCCEEDED(hr); + } + CoInitializeGuard(const CoInitializeGuard&) = delete; // non-construction-copyable + void operator=(const CoInitializeGuard&) = delete; // non-copyable + ~CoInitializeGuard() + { + if (mbUninit) + CoUninitialize(); + } + + private: + bool mbUninit; + }; + struct COM_QUERY_TAG {} constexpr COM_QUERY; struct COM_QUERY_THROW_TAG {} constexpr COM_QUERY_THROW; template <typename TAG> @@ -108,7 +141,7 @@ namespace sal::systools COMReference<T2> QueryInterface(TAG) const { void* ip = nullptr; - HRESULT hr = E_FAIL; + HRESULT hr = E_POINTER; if (com_ptr_) hr = com_ptr_->QueryInterface(__uuidof(T2), &ip); |