qtbase/src/corelib/kernel/qfunctions_win.cpp
Tor Arne Vestbø 1bc7e9e77b Add QComHelper class for dealing with COM on Windows
Unifies our approach to calling CoInitializeEx and CoUninitialize,
removing a lot of boilerplate in the process, and also fixes a few
bugs where we would incorrectly balance our calls to CoInitializeEx
and CoUninitialize.

The optimistic approach of qfilesystemengine_win.cpp of calling
CoCreateInstance without initializing the COM library explicitly
has been removed, as calling CoInitializeEx should be a noop in
the situation where it's already been loaded.

Change-Id: I9e2ec101678c2ebb9946504b5e8034e58f1bb56a
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
2022-08-29 15:39:34 +02:00

29 lines
711 B
C++

// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qfunctions_win_p.h"
#include <combaseapi.h>
#include <objbase.h>
QT_BEGIN_NAMESPACE
QComHelper::QComHelper(COINIT concurrencyModel)
{
// Avoid overhead of initializing and using obsolete technology
concurrencyModel = COINIT(concurrencyModel | COINIT_DISABLE_OLE1DDE);
m_initResult = CoInitializeEx(nullptr, concurrencyModel);
if (FAILED(m_initResult))
qErrnoWarning(m_initResult, "Failed to initialize COM library");
}
QComHelper::~QComHelper()
{
if (SUCCEEDED(m_initResult))
CoUninitialize();
}
QT_END_NAMESPACE