office-gobmx/include/o3tl/safeCoInitUninit.hxx
Julien Nabet 08e02ae960 Improve COM library management in fpicker
Inspired from:
author	Julien Nabet <serval2412@yahoo.fr>	2020-05-29 23:58:59 +0200
committer	Mike Kaganski <mike.kaganski@collabora.com>	2020-06-03 23:04:27 +0200
commit	95e5d37b6e (patch)
tree	5488826b39
parent	3d3cb4328e (diff)
Improve COM library management in ADO

Change-Id: If0b136cdcc89baa6bc90912d42b3ba07fa6c0efb
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/95498
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Tested-by: Jenkins
2020-06-05 15:31:48 +02:00

58 lines
1.8 KiB
C++

/* -*- 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: */