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
Change-Id: I3a370354d9cce50e3d89d125ce61fc9b619294cc
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
(cherry picked from commit 0f50145e433d2ac4ad2f4371ce627d2e0c2e0efd)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Mårten Nordheim 2023-01-26 13:16:43 +01:00 committed by Qt Cherry-pick Bot
parent 744aac988b
commit 730c668612

View File

@ -936,13 +936,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