Qt 获取本机网卡的IP与MAC信息

在Qt中,我们可以使用以下代码来获取本机网卡的IP地址和MAC地址:

#include <QtNetwork>
#include <QDebug>
#include <QList>
#include <QNetworkInterface>

int main()
{
    int i = 0;
    QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
    foreach (QNetworkInterface iface, ifaces) 
    {
        ++i;
        if(iface.hardwareAddress() != NULL)
        {
            qDebug() << "Name:" << iface.name() << ", DisplayName:" << iface.humanReadableName();
            qDebug() << "Address:" << iface.addressEntries().first().ip().toString() << ", HardwareAddress:" << 				iface.hardwareAddress();
            cb->addItem(iface.humanReadableName());
        }
    }
    if(i == 0)
    {
        qDebug("No interfaces found! Make sure WinPcap is installed.");
    }
    return 0;
}

在这里插入图片描述

posted @ 2023-03-28 15:34  h云淡风轻  阅读(83)  评论(0编辑  收藏  举报  来源