Skip to content

Commit

Permalink
QSysInfo: Work around erroneous warning output from Windows
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Oliver Wolff <[email protected]>
  • Loading branch information
Morten242 committed Feb 9, 2023
1 parent 08ea2c5 commit 0f50145
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/corelib/global/qsysinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0f50145

Please sign in to comment.