summaryrefslogtreecommitdiffstats
path: root/include/o3tl/safeCoInitUninit.hxx
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
commited5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch)
tree7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /include/o3tl/safeCoInitUninit.hxx
parentInitial commit. (diff)
downloadlibreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.tar.xz
libreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.zip
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'include/o3tl/safeCoInitUninit.hxx')
-rw-r--r--include/o3tl/safeCoInitUninit.hxx58
1 files changed, 58 insertions, 0 deletions
diff --git a/include/o3tl/safeCoInitUninit.hxx b/include/o3tl/safeCoInitUninit.hxx
new file mode 100644
index 000000000..0ceb4a974
--- /dev/null
+++ b/include/o3tl/safeCoInitUninit.hxx
@@ -0,0 +1,58 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#pragma once
+
+#if defined _WIN32
+#include <prewin.h>
+
+// for CoInitializeEx / CoUninitialize
+#include <combaseapi.h>
+
+#include <postwin.h>
+
+// for std::abort
+#include <cstdlib>
+
+namespace o3tl
+{
+// Helpers for safe calls to CoInitializeEx and CoUninitialize in MSVC
+// Indeed if a thread has been already initialized with a concurrency model
+// (in LO case COINIT_APARTMENTTHREADED or COINIT_MULTITHREADED)
+// CoInitializeEx can't succeed without calling first CoUninitialize
+// also, CoUninitialize must be called the number of times CoInitializeEx has been called
+inline HRESULT safeCoInitializeEx(DWORD dwCoInit, int& nbReinit)
+{
+ HRESULT hr;
+ while ((hr = CoInitializeEx(nullptr, dwCoInit)) == RPC_E_CHANGED_MODE)
+ {
+ // so we're in RPC_E_CHANGED_MODE case
+ // the pb was it was already initialized with a different concurrency model
+ // close this init
+ CoUninitialize();
+ // and increment counter for dtr part
+ ++nbReinit;
+
+ // and keep on the loop if there were multi initializations
+ }
+ if (FAILED(hr))
+ std::abort();
+ return hr;
+}
+
+inline void safeCoUninitializeReinit(DWORD dwCoInit, int nbReinit)
+{
+ CoUninitialize();
+ // Put back all the inits, if there were, before the use of the caller to safeCoInitializeEx
+ for (int i = 0; i < nbReinit; ++i)
+ CoInitializeEx(nullptr, dwCoInit);
+}
+}
+#endif
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */