QT 获取本地IP MAC地址
2016-11-22 15:23
399 查看
std::string CControl::gethostIp()
{
QString strIpAddress;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
// 获取第一个本主机的IPv4地址
int nListSize = ipAddressesList.size();
for (int i = 0; i < nListSize; ++i)
{
if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
ipAddressesList.at(i).toIPv4Address())
{
strIpAddress = ipAddressesList.at(i).toString();
break;
}
}
// 如果没有找到,则以本地IP地址为IP
if (strIpAddress.isEmpty())
strIpAddress = QHostAddress(QHostAddress::LocalHost).toString();
return strIpAddress.toStdString();
}
std::string CControl::gethostMac()
{
QList<QNetworkInterface> nets = QNetworkInterface::allInterfaces();// 获取所有网络接口列表
int nCnt = nets.count();
QString strMacAddr = "";
for(int i = 0; i < nCnt; i ++)
{
// 如果此网络接口被激活并且正在运行并且不是回环地址,则就是我们需要找的Mac地址
if(nets[i].flags().testFlag(QNetworkInterface::IsUp) && nets[i].flags().testFlag(QNetworkInterface::IsRunning)
&& !nets[i].flags().testFlag(QNetworkInterface::IsLoopBack))
{
strMacAddr = nets[i].hardwareAddress();
break;
}
}
return strMacAddr.toStdString();
}
{
QString strIpAddress;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
// 获取第一个本主机的IPv4地址
int nListSize = ipAddressesList.size();
for (int i = 0; i < nListSize; ++i)
{
if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
ipAddressesList.at(i).toIPv4Address())
{
strIpAddress = ipAddressesList.at(i).toString();
break;
}
}
// 如果没有找到,则以本地IP地址为IP
if (strIpAddress.isEmpty())
strIpAddress = QHostAddress(QHostAddress::LocalHost).toString();
return strIpAddress.toStdString();
}
std::string CControl::gethostMac()
{
QList<QNetworkInterface> nets = QNetworkInterface::allInterfaces();// 获取所有网络接口列表
int nCnt = nets.count();
QString strMacAddr = "";
for(int i = 0; i < nCnt; i ++)
{
// 如果此网络接口被激活并且正在运行并且不是回环地址,则就是我们需要找的Mac地址
if(nets[i].flags().testFlag(QNetworkInterface::IsUp) && nets[i].flags().testFlag(QNetworkInterface::IsRunning)
&& !nets[i].flags().testFlag(QNetworkInterface::IsLoopBack))
{
strMacAddr = nets[i].hardwareAddress();
break;
}
}
return strMacAddr.toStdString();
}
相关文章推荐
- QT获取本机IP,本地IP
- QT获取本地ip
- C#获取本地计算机名IP,Mac地址
- C#获取本地计算机名,IP,MAC地址,硬盘ID
- 获取本地IP与网络Ip及Mac地址
- (原创)C#获取本地计算机名,IP,MAC地址
- JS获取本地IP、MAC地址
- QT获取本机IP,本地IP
- C#获取本地计算机名,IP,MAC地址,硬盘ID
- Qt获取IP地址、MAC地址等网卡信息,区分本地网卡、无线网卡和虚拟网卡
- C#获取本地计算机名,IP,MAC地址,硬盘ID
- C#获取本地计算机名,IP,MAC地址,硬盘ID
- qt获取本地ip
- Shell脚本获取本地网卡IP、mac地址、子网掩码、dns IP、外网IP
- QT获取本机IP和MAC地址的方法
- c#获取本地IP和MAC地址
- qt获取本地IP
- Qt 获取本地IP 和 本地主机名称
- Android系统下获取本地IP和mac地址的方法
- Shell脚本获取本地网卡IP、mac地址、子网掩码、dns IP、外网IP