|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
void TCPClient::InitHostInfo()
{
//m_username = QString::fromLocal8Bit(qgetenv("USERNAME")).toStdString();
#ifdef _WIN32
// 使用 Windows API 获取实时账户名
TCHAR username[UNLEN + 1];
DWORD username_len = UNLEN + 1;
if (GetUserName(username, &username_len)) {
m_username = QString::fromWCharArray(username).toStdString();
}
std::cout << "电脑名称: " << m_username << std::endl;
#else
m_username = qgetenv("USER").toStdString();
#endif
m_osVersion = QSysInfo::prettyProductName().toStdString();
//m_username = getCurrentUserName().toStdString();
const QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
for (const QNetworkInterface& netInterface : interfaces) {
// 跳过虚拟网卡、未启用网卡、环回网卡
if (!(netInterface.flags() & QNetworkInterface::IsUp) || !(netInterface.flags() & QNetworkInterface::IsRunning) || (netInterface.flags() & QNetworkInterface::IsLoopBack) ||
netInterface.humanReadableName().contains("VMware", Qt::CaseInsensitive) ||
netInterface.humanReadableName().contains("VirtualBox", Qt::CaseInsensitive))
{
continue;
}
const QList<QNetworkAddressEntry> entries = netInterface.addressEntries();
for (const QNetworkAddressEntry& entry : entries) {
QHostAddress ip = entry.ip();
if (ip.protocol() == QAbstractSocket::IPv4Protocol &&
!ip.isLoopback() &&
!ip.toString().startsWith("169.254")) // 排除自动私有地址
{
m_ipAddress = ip.toString().toStdString();
m_macAddress = netInterface.hardwareAddress().toStdString();
return;
}
}
}
}
这个函数开头是获取账户名的代码,但是现在的问题是原来的账户名是PC,不管我怎么改名字,获取的始终是PC,这个是为什么? |
|