summaryrefslogtreecommitdiff
path: root/include/systools
diff options
context:
space:
mode:
authorMike Kaganski <mike.kaganski@collabora.com>2021-12-20 19:25:58 +0300
committerMike Kaganski <mike.kaganski@collabora.com>2021-12-21 07:31:55 +0100
commit64e46c7cb0968a6249dd37ac0a7b12bccc19ee58 (patch)
tree5ac589ec706efb1a83eb0b188a7456b195d013e7 /include/systools
parent64065ade86cac68b9d500f0495ffc5745a944819 (diff)
Introduce sal::systools::COMReference::TryCoCreateInstance
Helps fixing uncaught exceptions introduced in commit 00eb9b8954a129fb365191ce8cdcbc4cf66a7333. TryCoCreateInstance returns HRESULT, because it allows to pass the info to the caller, which must check the result (either HRESULT, or validity of the reference) anyway. In case of failure, the previous contents of com_ptr_ is kept, which would be consistent with existing code in connectivity/ado replaced in a follow-up patch. Change-Id: I4c94695de96861ec312247acaa0c8fd35a23010a Reviewed-on: https://gerrit.libreoffice.org/c/core/+/127192 Tested-by: Mike Kaganski <mike.kaganski@collabora.com> Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'include/systools')
-rw-r--r--include/systools/win32/comtools.hxx15
1 files changed, 12 insertions, 3 deletions
diff --git a/include/systools/win32/comtools.hxx b/include/systools/win32/comtools.hxx
index bc9ecdf5c12c..d26f680569e5 100644
--- a/include/systools/win32/comtools.hxx
+++ b/include/systools/win32/comtools.hxx
@@ -110,12 +110,21 @@ namespace sal::systools
return operator=(p.template QueryInterface<T>(t));
}
+ HRESULT TryCoCreateInstance(REFCLSID clsid, IUnknown* pOuter = nullptr,
+ DWORD nCtx = CLSCTX_ALL)
+ {
+ T* ip;
+ HRESULT hr = ::CoCreateInstance(clsid, pOuter, nCtx, __uuidof(T),
+ reinterpret_cast<void**>(&ip));
+ if (SUCCEEDED(hr))
+ release(std::exchange(com_ptr_, ip));
+ return hr;
+ }
+
COMReference<T>& CoCreateInstance(REFCLSID clsid, IUnknown* pOuter = nullptr,
DWORD nCtx = CLSCTX_ALL)
{
- clear();
- HRESULT hr = ::CoCreateInstance(clsid, pOuter, nCtx, __uuidof(T),
- reinterpret_cast<void**>(&com_ptr_));
+ HRESULT hr = TryCoCreateInstance(clsid, pOuter, nCtx);
if (FAILED(hr))
throw ComError("CoCreateInstance failed!", hr);