diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2020-05-14 14:46:02 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-05-20 09:38:35 +0200 |
commit | 8d9e4509c07785819116eea51224271d5cb10a63 (patch) | |
tree | 37135525692099ad4fa8ee5ad0b2d23e39ba4556 /src | |
parent | 55b91dc4714d36d457fbfa32bc6b412415362430 (diff) |
Adapt to QString::utf16() now returning char16_t *
Make the code agnostic of the exact return type. In one case, were
filling a C# IntPtr, drop the cast and use .data(), as IntPtr has a
ctor from void* and the code stores an explicit string length, so we
don't need the NUL-termination that utf16() guarantees.
There are a handful of casts remaining, so write a small helper that
allows to comment on certain aspects of the conversion.
Done-with: Marc Mutz <marc.mutz@kdab.com>
Change-Id: I16227077b31f02fbbefe1a83e4dfbd2d5333a0f7
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/activeqt/axbase/qaxutils_p.h | 7 | ||||
-rw-r--r-- | src/activeqt/container/qaxbase.cpp | 26 | ||||
-rw-r--r-- | src/activeqt/control/qaxserver.cpp | 10 | ||||
-rw-r--r-- | src/activeqt/control/qaxserverbase.cpp | 4 |
4 files changed, 29 insertions, 18 deletions
diff --git a/src/activeqt/axbase/qaxutils_p.h b/src/activeqt/axbase/qaxutils_p.h index f56644b..5324dc5 100644 --- a/src/activeqt/axbase/qaxutils_p.h +++ b/src/activeqt/axbase/qaxutils_p.h @@ -66,6 +66,7 @@ #include <QtCore/qmetatype.h> #include <QtCore/qpair.h> #include <QtCore/qrect.h> +#include <QtCore/qstring.h> QT_BEGIN_NAMESPACE @@ -107,6 +108,12 @@ static inline RECT qaxContentRect(const QSize &size) // Size with topleft = 0,0 return result; } +static inline wchar_t *qaxQString2MutableOleChars(QString &s) // must be passed an lvalue +{ + // using utf16() to force NUL-termination: + return const_cast<wchar_t *>(reinterpret_cast<const wchar_t *>(s.utf16())); +} + #ifdef QT_WIDGETS_LIB SIZEL qaxMapPixToLogHiMetrics(const QSize &s, const QWidget *widget); QSize qaxMapLogHiMetricsToPix(const SIZEL &s, const QWidget *widget); diff --git a/src/activeqt/container/qaxbase.cpp b/src/activeqt/container/qaxbase.cpp index 53fbe81..cb4893f 100644 --- a/src/activeqt/container/qaxbase.cpp +++ b/src/activeqt/container/qaxbase.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2015 The Qt Company Ltd. +** Copyright (C) 2020 The Qt Company Ltd. ** Contact: http://www.qt.io/licensing/ ** ** This file is part of the ActiveQt framework of the Qt Toolkit. @@ -220,7 +220,7 @@ inline DISPID QMetaObjectExtra::dispIDofName(const QByteArray &name, IDispatch * if (dispid == DISPID_UNKNOWN) { // get the Dispatch ID from the object QString unicodeName = QLatin1String(name); - OLECHAR *names = reinterpret_cast<wchar_t *>(const_cast<ushort *>(unicodeName.utf16())); + OLECHAR *names = qaxQString2MutableOleChars(unicodeName); disp->GetIDsOfNames(IID_NULL, &names, 1, LOCALE_USER_DEFAULT, &dispid); if (dispid != DISPID_UNKNOWN) dispIDs.insert(name, dispid); @@ -1377,6 +1377,14 @@ bool QAxBase::initializeFromFile(IUnknown** ptr) #define COAUTHIDENTITY AUTH_IDENTITY #endif +// Set string values to COAUTHIDENTITY fields. Use data(), no need for 0-termination. +static inline void setIdentityString(const QString &v, ULONG &length, USHORT *&target) +{ + length = ULONG(v.size()); + target = length > 0 + ? const_cast<USHORT *>(reinterpret_cast<const USHORT *>(v.data())) + : nullptr; +} /*! Creates the instance on a remote server, and returns the IUnknown interface @@ -1429,15 +1437,9 @@ bool QAxBase::initializeRemote(IUnknown** ptr) d->ctrl = d->ctrl + QChar::fromLatin1(':') + key; COAUTHIDENTITY authIdentity; - authIdentity.UserLength = ULONG(user.length()); - authIdentity.User = authIdentity.UserLength - ? const_cast<ushort *>(user.utf16()) : nullptr; - authIdentity.DomainLength = ULONG(domain.length()); - authIdentity.Domain = authIdentity.DomainLength - ? const_cast<ushort *>(domain.utf16()) : nullptr; - authIdentity.PasswordLength = ULONG(passwd.length()); - authIdentity.Password = authIdentity.PasswordLength - ? const_cast<ushort *>(passwd.utf16()) : nullptr; + setIdentityString(user, authIdentity.UserLength, authIdentity.User); + setIdentityString(domain, authIdentity.DomainLength, authIdentity.Domain); + setIdentityString(passwd, authIdentity.PasswordLength, authIdentity.Password); authIdentity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE; COAUTHINFO authInfo; @@ -1453,7 +1455,7 @@ bool QAxBase::initializeRemote(IUnknown** ptr) serverInfo.dwReserved1 = 0; serverInfo.dwReserved2 = 0; serverInfo.pAuthInfo = &authInfo; - serverInfo.pwszName = reinterpret_cast<wchar_t *>(const_cast<ushort *>(server.utf16())); + serverInfo.pwszName = qaxQString2MutableOleChars(server); IClassFactory *factory = nullptr; HRESULT res = CoGetClassObject(QUuid(clsid), CLSCTX_REMOTE_SERVER, &serverInfo, diff --git a/src/activeqt/control/qaxserver.cpp b/src/activeqt/control/qaxserver.cpp index 7e85158..7932b1d 100644 --- a/src/activeqt/control/qaxserver.cpp +++ b/src/activeqt/control/qaxserver.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2015 The Qt Company Ltd. +** Copyright (C) 2020 The Qt Company Ltd. ** Contact: http://www.qt.io/licensing/ ** ** This file is part of the ActiveQt framework of the Qt Toolkit. @@ -51,6 +51,8 @@ #include "qaxbindable.h" #include "qaxfactory.h" +#include <QtAxBase/private/qaxutils_p.h> + #include <qapplication.h> #include <qdatetime.h> #include <qdir.h> @@ -437,7 +439,7 @@ HRESULT UpdateRegistry(bool bRegister, bool perUser) QString file = QString::fromWCharArray(qAxModuleFilename); const QString module = QFileInfo(file).baseName(); - const QString libFile = qAxInit(); + QString libFile = qAxInit(); auto libFile_cleanup = qScopeGuard([] { qAxCleanup(); }); TLIBATTR *libAttr = nullptr; @@ -449,7 +451,7 @@ HRESULT UpdateRegistry(bool bRegister, bool perUser) if (bRegister) { if (!perUser) { - HRESULT hr = RegisterTypeLib(qAxTypeLibrary, reinterpret_cast<wchar_t *>(const_cast<ushort *>(libFile.utf16())), nullptr); + HRESULT hr = RegisterTypeLib(qAxTypeLibrary, qaxQString2MutableOleChars(libFile), nullptr); if (FAILED(hr)) { qWarning("Failing to register %s due to insufficient permission.", qPrintable(module)); return hr; @@ -457,7 +459,7 @@ HRESULT UpdateRegistry(bool bRegister, bool perUser) } else { #ifndef Q_CC_MINGW // MinGW does not have RegisterTypeLibForUser() implemented so we cannot fallback in this case - RegisterTypeLibForUser(qAxTypeLibrary, reinterpret_cast<wchar_t *>(const_cast<ushort *>(libFile.utf16())), nullptr); + RegisterTypeLibForUser(qAxTypeLibrary, qaxQString2MutableOleChars(libFile), nullptr); #endif } } else { diff --git a/src/activeqt/control/qaxserverbase.cpp b/src/activeqt/control/qaxserverbase.cpp index eb07d2b..99a6bf0 100644 --- a/src/activeqt/control/qaxserverbase.cpp +++ b/src/activeqt/control/qaxserverbase.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2015 The Qt Company Ltd. +** Copyright (C) 2020 The Qt Company Ltd. ** Contact: http://www.qt.io/licensing/ ** ** This file is part of the ActiveQt framework of the Qt Toolkit. @@ -2003,7 +2003,7 @@ int QAxServerBase::qt_metacall(QMetaObject::Call call, int index, void **argv) qAxTypeLibrary->GetTypeInfoOfGuid(qAxFactory()->eventsID(class_name), &eventInfo); if (eventInfo) { QString uni_name = QLatin1String(name); - OLECHAR *olename = reinterpret_cast<OLECHAR *>(const_cast<ushort *>(uni_name.utf16())); + OLECHAR *olename = qaxQString2MutableOleChars(uni_name); eventInfo->GetIDsOfNames(&olename, 1, &eventId); eventInfo->Release(); } |