diff --git a/src/corelib/CMakeLists.txt b/src/corelib/CMakeLists.txt index 5d1fc9f61a7..dbf9cb1a446 100644 --- a/src/corelib/CMakeLists.txt +++ b/src/corelib/CMakeLists.txt @@ -148,6 +148,7 @@ qt_internal_add_module(Core io/qurlquery.cpp io/qurlquery.h io/qurlrecode.cpp io/qzipreader_p.h io/qzipwriter_p.h io/qzip.cpp + io/wcharhelpers_win_p.h kernel/qabstracteventdispatcher.cpp kernel/qabstracteventdispatcher.h kernel/qabstracteventdispatcher_p.h kernel/qabstractnativeeventfilter.cpp kernel/qabstractnativeeventfilter.h kernel/qapplicationstatic.h diff --git a/src/corelib/io/qfilesystemengine_win.cpp b/src/corelib/io/qfilesystemengine_win.cpp index 1b91cc9b800..4638e94bcff 100644 --- a/src/corelib/io/qfilesystemengine_win.cpp +++ b/src/corelib/io/qfilesystemengine_win.cpp @@ -40,6 +40,7 @@ #include #include +#include #ifndef SPI_GETPLATFORMTYPE #define SPI_GETPLATFORMTYPE 257 @@ -684,8 +685,7 @@ static QString readSymLink(const QFileSystemEntry &link) DWORD len; wchar_t buffer[MAX_PATH]; const QString volumeName = "\\\\?\\"_L1 + matchVolume.captured(); - if (GetVolumePathNamesForVolumeName(reinterpret_cast(volumeName.unicode()), - buffer, MAX_PATH, &len) + if (GetVolumePathNamesForVolumeName(qt_castToWchar(volumeName), buffer, MAX_PATH, &len) != 0) { result.replace(0, matchVolume.capturedLength(), QString::fromWCharArray(buffer)); } diff --git a/src/corelib/io/qfilesystemiterator_win.cpp b/src/corelib/io/qfilesystemiterator_win.cpp index 1be8854d81a..fb4f0a5433a 100644 --- a/src/corelib/io/qfilesystemiterator_win.cpp +++ b/src/corelib/io/qfilesystemiterator_win.cpp @@ -7,6 +7,7 @@ #include "qplatformdefs.h" #include +#include QT_BEGIN_NAMESPACE @@ -69,7 +70,8 @@ bool QFileSystemIterator::advance(QFileSystemEntry &fileEntry, QFileSystemMetaDa int searchOps = 0; // FindExSearchNameMatch if (onlyDirs) searchOps = 1 ; // FindExSearchLimitToDirectories - findFileHandle = FindFirstFileEx((const wchar_t *)nativePath.utf16(), FINDEX_INFO_LEVELS(infoLevel), &findData, + findFileHandle = FindFirstFileEx(qt_castToWchar(nativePath), + FINDEX_INFO_LEVELS(infoLevel), &findData, FINDEX_SEARCH_OPS(searchOps), 0, dwAdditionalFlags); if (findFileHandle == INVALID_HANDLE_VALUE) { if (nativePath.startsWith("\\\\?\\UNC\\"_L1)) { diff --git a/src/corelib/io/qprocess_win.cpp b/src/corelib/io/qprocess_win.cpp index e64b133815e..7d577a4ba49 100644 --- a/src/corelib/io/qprocess_win.cpp +++ b/src/corelib/io/qprocess_win.cpp @@ -544,7 +544,7 @@ void QProcessPrivate::startProcess() return; } - const QString args = qt_create_commandline(program, arguments, nativeArguments); + QString args = qt_create_commandline(program, arguments, nativeArguments); QByteArray envlist; if (!environment.inheritsFromParent()) envlist = qt_create_environment(environment.d.constData()->vars); @@ -566,7 +566,7 @@ void QProcessPrivate::startProcess() STARTUPINFOW startupInfo = createStartupInfo(); const QString nativeWorkingDirectory = QDir::toNativeSeparators(workingDirectory); QProcess::CreateProcessArguments cpargs = { - nullptr, reinterpret_cast(const_cast(args.utf16())), + nullptr, reinterpret_cast(args.data_ptr().data()), nullptr, nullptr, true, dwCreationFlags, environment.inheritsFromParent() ? nullptr : envlist.data(), nativeWorkingDirectory.isEmpty() @@ -920,7 +920,7 @@ bool QProcessPrivate::startDetached(qint64 *pid) dwCreationFlags |= CREATE_UNICODE_ENVIRONMENT; STARTUPINFOW startupInfo = createStartupInfo(); QProcess::CreateProcessArguments cpargs = { - nullptr, reinterpret_cast(const_cast(args.utf16())), + nullptr, reinterpret_cast(args.data_ptr().data()), nullptr, nullptr, true, dwCreationFlags, envPtr, workingDirectory.isEmpty() ? nullptr : reinterpret_cast(workingDirectory.utf16()), diff --git a/src/corelib/io/qsettings_win.cpp b/src/corelib/io/qsettings_win.cpp index 5434d73555d..230383ff2cd 100644 --- a/src/corelib/io/qsettings_win.cpp +++ b/src/corelib/io/qsettings_win.cpp @@ -8,6 +8,8 @@ #include "qmap.h" #include "qdebug.h" #include "qscopeguard.h" +#include + #include // See "Accessing an Alternate Registry View" at: @@ -262,7 +264,7 @@ static void deleteChildGroups(HKEY parentHandle, REGSAM access = 0) RegCloseKey(childGroupHandle); // delete group itself - LONG res = RegDeleteKey(parentHandle, reinterpret_cast(group.utf16())); + LONG res = RegDeleteKey(parentHandle, qt_castToWchar(group)); if (res != ERROR_SUCCESS) { qErrnoWarning(int(res), "QSettings: RegDeleteKey failed on subkey \"%ls\"", qUtf16Printable(group)); @@ -584,7 +586,7 @@ void QWinSettingsPrivate::remove(const QString &uKey) const QStringList childKeys = childKeysOrGroups(handle, QSettingsPrivate::ChildKeys); for (const QString &group : childKeys) { - LONG res = RegDeleteValue(handle, reinterpret_cast(group.utf16())); + LONG res = RegDeleteValue(handle, qt_castToWchar(group)); if (res != ERROR_SUCCESS) { qErrnoWarning(int(res), "QSettings: RegDeleteValue failed on subkey \"%ls\"", qUtf16Printable(group)); @@ -678,7 +680,7 @@ void QWinSettingsPrivate::set(const QString &uKey, const QVariant &value) int length = s.length(); if (type == REG_SZ) ++length; - regValueBuff = QByteArray(reinterpret_cast(s.utf16()), + regValueBuff = QByteArray(reinterpret_cast(s.constData()), int(sizeof(wchar_t)) * length); break; } diff --git a/src/corelib/io/qstorageinfo_win.cpp b/src/corelib/io/qstorageinfo_win.cpp index 3ee1df9f3c8..96547dfb00d 100644 --- a/src/corelib/io/qstorageinfo_win.cpp +++ b/src/corelib/io/qstorageinfo_win.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include "qfilesystementry_p.h" #include "private/qsystemlibrary_p.h" @@ -176,7 +177,7 @@ bool QStorageInfoPrivate::queryStorageProperty() if (path.endsWith(u'\\')) path.chop(1); - HANDLE handle = CreateFile(reinterpret_cast(path.utf16()), + HANDLE handle = CreateFile(qt_castToWchar(path), 0, // no access to the drive FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, @@ -276,7 +277,7 @@ void QStorageInfoPrivate::queryFileFsSectorSizeInformation() path.append(u'\\'); UNICODE_STRING name; - qtRtlInitUnicodeString(&name, reinterpret_cast(path.utf16())); + qtRtlInitUnicodeString(&name, qt_castToWchar(path)); InitializeObjectAttributes(&attrs, &name, 0, nullptr, nullptr); diff --git a/src/corelib/io/wcharhelpers_win_p.h b/src/corelib/io/wcharhelpers_win_p.h new file mode 100644 index 00000000000..53694085bb1 --- /dev/null +++ b/src/corelib/io/wcharhelpers_win_p.h @@ -0,0 +1,30 @@ +// Copyright (C) 2024 Ahmad Samir +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef WCHARHELPERS_WIN_P_H +#define WCHARHELPERS_WIN_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + + +#include + +QT_BEGIN_NAMESPACE + +static inline const wchar_t *qt_castToWchar(const QString &s) +{ + return reinterpret_cast(s.constData()); +} + +QT_END_NAMESPACE + +#endif // WCHARHELPERS_WIN_P_H diff --git a/src/corelib/plugin/qsystemlibrary.cpp b/src/corelib/plugin/qsystemlibrary.cpp index 7da9b823ee7..ed11080a387 100644 --- a/src/corelib/plugin/qsystemlibrary.cpp +++ b/src/corelib/plugin/qsystemlibrary.cpp @@ -5,6 +5,7 @@ #include #include #include +#include /*! @@ -81,8 +82,7 @@ HINSTANCE QSystemLibrary::load(const wchar_t *libraryName, bool onlySystemDirect fullPathAttempt.append(u'\\'); } fullPathAttempt.append(fileName); - HINSTANCE inst = - ::LoadLibrary(reinterpret_cast(fullPathAttempt.unicode())); + HINSTANCE inst = ::LoadLibrary(qt_castToWchar(fullPathAttempt)); if (inst != nullptr) return inst; } diff --git a/src/corelib/text/qstringconverter.cpp b/src/corelib/text/qstringconverter.cpp index 98f1a010829..863aaa3776b 100644 --- a/src/corelib/text/qstringconverter.cpp +++ b/src/corelib/text/qstringconverter.cpp @@ -24,6 +24,8 @@ #include #ifndef QT_BOOTSTRAPPED #include +#include + #include #include #endif // !QT_BOOTSTRAPPED @@ -1797,9 +1799,8 @@ QByteArray QLocal8Bit::convertFromUnicode_sys(QStringView in, quint32 codePage, #ifndef QT_NO_DEBUG // Can't use qWarning(), as it'll recurse to handle %ls fprintf(stderr, - "WideCharToMultiByte: Cannot convert multibyte text (error %d): %ls\n", r, - reinterpret_cast( - QStringView(ch, uclen).left(100).toString().unicode())); + "WideCharToMultiByte: Cannot convert multibyte text (error %d): %ls\n", + r, qt_castToWchar(QStringView(ch, uclen).left(100).toString())); #endif break; } diff --git a/src/corelib/time/qtimezoneprivate_win.cpp b/src/corelib/time/qtimezoneprivate_win.cpp index 4745b643404..9d37f990cf7 100644 --- a/src/corelib/time/qtimezoneprivate_win.cpp +++ b/src/corelib/time/qtimezoneprivate_win.cpp @@ -8,6 +8,7 @@ #include "qdatetime.h" #include "qdebug.h" #include +#include #include @@ -537,10 +538,9 @@ void QWinTimeZonePrivate::init(const QByteArray &ianaId) const int endYear = dynamicKey.value(L"LastEntry").value_or(0); for (int year = startYear; year <= endYear; ++year) { bool ruleOk; - QWinTransitionRule rule = - readRegistryRule(dynamicKey, - reinterpret_cast(QString::number(year).unicode()), - &ruleOk); + QWinTransitionRule rule = readRegistryRule(dynamicKey, + qt_castToWchar(QString::number(year)), + &ruleOk); if (ruleOk // Don't repeat a recurrent rule: && (m_tranRules.isEmpty()