Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qbluetoothutils_winrt.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 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#include <QtBluetooth/private/qtbluetoothglobal_p.h>
6#include <QtCore/private/qfunctions_winrt_p.h>
7#include <QtCore/QLoggingCategory>
8
9#include <robuffer.h>
10#include <wrl.h>
11#include <winrt/windows.foundation.metadata.h>
12#include <windows.storage.streams.h>
13
14using namespace Microsoft::WRL;
15using namespace Microsoft::WRL::Wrappers;
16using namespace winrt::Windows::Foundation::Metadata;
17using namespace ABI::Windows::Storage::Streams;
18
20
21Q_DECLARE_LOGGING_CATEGORY(QT_BT_WINDOWS)
22
24{
25 if (!buffer) {
26 qErrnoWarning("nullptr passed to byteArrayFromBuffer");
27 return QByteArray();
28 }
30 HRESULT hr = buffer.As(&byteAccess);
31 RETURN_IF_FAILED("Could not cast buffer", return QByteArray())
32 char *data;
33 hr = byteAccess->Buffer(reinterpret_cast<byte **>(&data));
34 RETURN_IF_FAILED("Could not obtain buffer data", return QByteArray())
35 UINT32 size;
36 hr = buffer->get_Length(&size);
37 RETURN_IF_FAILED("Could not obtain buffer size", return QByteArray())
38 if (isWCharString) {
39 QString valueString = QString::fromUtf16(reinterpret_cast<char16_t *>(data)).left(size / 2);
40 return valueString.toUtf8();
41 }
42 return QByteArray(data, qint32(size));
43}
44
46
47void mainThreadCoInit(void* caller)
48{
49 Q_ASSERT(caller);
50
52 qCWarning(QT_BT_WINDOWS) << "Main thread COM init tried from another thread";
53 return;
54 }
55
56 if (successfulInits.contains(caller)) {
57 qCWarning(QT_BT_WINDOWS) << "Multiple COM inits by same object";
58 return;
59 }
60
61 // This executes in main thread which may run Gui => use apartment threaded
62 if (!SUCCEEDED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED))) {
63 qCWarning(QT_BT_WINDOWS) << "Unexpected COM initialization result";
64 return;
65 }
66 successfulInits.insert(caller);
67}
68
69void mainThreadCoUninit(void* caller)
70{
71 Q_ASSERT(caller);
72
74 qCWarning(QT_BT_WINDOWS) << "Main thread COM uninit tried from another thread";
75 return;
76 }
77
78 if (!successfulInits.contains(caller)) {
79 qCWarning(QT_BT_WINDOWS) << "COM uninitialization without initialization";
80 return;
81 }
82 CoUninitialize();
83 successfulInits.remove(caller);
84
85}
86
\inmodule QtCore
Definition qbytearray.h:57
static QCoreApplication * instance() noexcept
Returns a pointer to the application's QCoreApplication (or QGuiApplication/QApplication) instance.
Definition qset.h:18
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
static QString fromUtf16(const char16_t *, qsizetype size=-1)
Definition qstring.cpp:5883
QString left(qsizetype n) const
Returns a substring that contains the n leftmost characters of the string.
Definition qstring.cpp:5161
QByteArray toUtf8() const &
Definition qstring.h:563
static QThread * currentThread()
Definition qthread.cpp:966
void qErrnoWarning(const char *msg,...)
Combined button and popup list for selecting options.
static QSet< void * > successfulInits
QT_BEGIN_NAMESPACE QByteArray byteArrayFromBuffer(const ComPtr< NativeBuffer > &buffer, bool isWCharString)
void mainThreadCoInit(void *caller)
void mainThreadCoUninit(void *caller)
ABI::Windows::Storage::Streams::IBuffer NativeBuffer
#define qCWarning(category,...)
#define Q_DECLARE_LOGGING_CATEGORY(name)
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLuint buffer
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
int qint32
Definition qtypes.h:44
long HRESULT