QSysInfo: Work around erroneous warning output from Windows

gethostname is in no way labeled deprecated, but it _tries_ to query
some deprecated functionality, thus some warning like this is printed:

""
LogHr(1) tid(6e14) 8007277C No such service is known.
The service cannot be found in the specified name space.
""

By using GetComputerNameEx we work around that. Bonus side effect is
that it gives us UTF-16 right away so we save a conversion.

Fixes: QTBUG-110468
Pick-to: 6.5
Change-Id: I3a370354d9cce50e3d89d125ce61fc9b619294cc
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
bb10
Mårten Nordheim 2023-01-26 13:16:43 +01:00
parent 08ea2c5dc8
commit 0f50145e43
1 changed files with 14 additions and 1 deletions

View File

@ -938,13 +938,26 @@ QString QSysInfo::machineHostName()
# ifdef Q_OS_WIN
// Important: QtNetwork depends on machineHostName() initializing ws2_32.dll
winsockInit();
# endif
QString hostName;
hostName.resize(512);
unsigned long len = hostName.size();
BOOL res = GetComputerNameEx(ComputerNameDnsHostname,
reinterpret_cast<wchar_t *>(const_cast<quint16 *>(hostName.utf16())), &len);
if (!res && len > 512) {
hostName.resize(len - 1);
GetComputerNameEx(ComputerNameDnsHostname,
reinterpret_cast<wchar_t *>(const_cast<quint16 *>(hostName.utf16())), &len);
}
hostName.truncate(len);
return hostName;
# else // !Q_OS_WIN
char hostName[512];
if (gethostname(hostName, sizeof(hostName)) == -1)
return QString();
hostName[sizeof(hostName) - 1] = '\0';
return QString::fromLocal8Bit(hostName);
# endif
#endif
}
#endif // QT_BOOTSTRAPPED