Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qwindows_propertystore.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtCore/qstring.h>
7#include <QtCore/qdebug.h>
8#include <QtCore/private/qsystemerror_p.h>
9#include <QtMultimedia/private/qcomtaskresource_p.h>
10#include <QtMultimedia/private/qwindows_scopedpropvariant_p.h>
11
12#include <mmdeviceapi.h>
13#include <propsys.h>
14#include <propvarutil.h>
15
17
18namespace QtMultimediaPrivate {
19
20PropertyStoreHelper::PropertyStoreHelper(ComPtr<IPropertyStore> props) : m_props(std::move(props))
21{
22}
23
24q23::expected<PropertyStoreHelper, QString>
25PropertyStoreHelper::open(const ComPtr<IMMDevice> &device)
26{
27 ComPtr<IPropertyStore> props;
28 HRESULT hr = device->OpenPropertyStore(STGM_READ, props.GetAddressOf());
29 if (!SUCCEEDED(hr)) {
30 return q23::unexpected{ QSystemError::windowsComString(hr) };
31 }
32 return PropertyStoreHelper(std::move(props));
33}
34
36{
37 ScopedPropVariant variant;
38
39 if (!SUCCEEDED(m_props->GetValue(property, variant.get())))
40 return std::nullopt;
41
42 QComTaskResource<WCHAR> str;
43 HRESULT hr = PropVariantToStringAlloc(*variant, str.address());
44 if (SUCCEEDED(hr))
45 return QString::fromWCharArray(variant->pwszVal);
46
47 qWarning() << "PropertyStoreHelper::getString: PropVariantToStringAlloc failed"
48 << QSystemError::windowsComString(hr);
49 return std::nullopt;
50}
51
53{
54 ScopedPropVariant variant;
55
56 if (!SUCCEEDED(m_props->GetValue(property, variant.get())))
57 return std::nullopt;
58
59 ULONG ret;
60 HRESULT hr = PropVariantToUInt32(*variant, &ret);
61 if (SUCCEEDED(hr))
62 return uint32_t{ ret };
63
64 qWarning() << "PropertyStoreHelper::getUInt32: PropVariantToUInt32 failed"
65 << QSystemError::windowsComString(hr);
66 return std::nullopt;
67}
68
69namespace {
70
71// mingw workaround: PropVariantToGUID does not provide correct signature
72// https://github.com/mingw-w64/mingw-w64/issues/153
73
74template <typename T>
75struct first_arg_is_ptr;
76
77// Function type
78template <typename Result, typename Arg, typename... Args>
79struct first_arg_is_ptr<Result (*)(Arg, Args...)> : std::bool_constant<std::is_pointer_v<Arg>>
80{
81};
82
83auto wrapPropVariantArg(const PROPVARIANT &arg)
84{
85 if constexpr (first_arg_is_ptr<decltype(&PropVariantToGUID)>::value) {
86 return &arg;
87 } else {
88 return arg;
89 }
90}
91
92} // namespace
93
95{
96 ScopedPropVariant variant;
97
98 if (!SUCCEEDED(m_props->GetValue(property, variant.get())))
99 return std::nullopt;
100
101 GUID ret;
102 HRESULT hr = PropVariantToGUID(wrapPropVariantArg(*variant), &ret);
103 if (SUCCEEDED(hr))
104 return QUuid{ ret };
105
106 qWarning() << "PropertyStoreHelper::getUInt32: PropVariantToUInt32 failed"
107 << QSystemError::windowsComString(hr);
108 return std::nullopt;
109}
110
111} // namespace QtMultimediaPrivate
112
113QT_END_NAMESPACE
std::optional< uint32_t > getUInt32(const PROPERTYKEY &)
std::optional< QUuid > getGUID(const PROPERTYKEY &)
std::optional< QString > getString(const PROPERTYKEY &)
Combined button and popup list for selecting options.
struct _tagpropertykey PROPERTYKEY