From 8d9e4509c07785819116eea51224271d5cb10a63 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 14 May 2020 14:46:02 +0200 Subject: 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 Change-Id: I16227077b31f02fbbefe1a83e4dfbd2d5333a0f7 Reviewed-by: Friedemann Kleint --- src/activeqt/axbase/qaxutils_p.h | 7 +++++++ src/activeqt/container/qaxbase.cpp | 26 ++++++++++++++------------ src/activeqt/control/qaxserver.cpp | 10 ++++++---- 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 #include #include +#include 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(reinterpret_cast(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(const_cast(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(reinterpret_cast(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(user.utf16()) : nullptr; - authIdentity.DomainLength = ULONG(domain.length()); - authIdentity.Domain = authIdentity.DomainLength - ? const_cast(domain.utf16()) : nullptr; - authIdentity.PasswordLength = ULONG(passwd.length()); - authIdentity.Password = authIdentity.PasswordLength - ? const_cast(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(const_cast(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 + #include #include #include @@ -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(const_cast(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(const_cast(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(const_cast(uni_name.utf16())); + OLECHAR *olename = qaxQString2MutableOleChars(uni_name); eventInfo->GetIDsOfNames(&olename, 1, &eventId); eventInfo->Release(); } -- cgit v1.2.3