在Qt中获取MAC ID

时间:2013-03-08 06:10:11

标签: qt qtnetwork

我使用以下代码获取Qt中的MAC ID。

的main.cpp

#include <QtCore/QCoreApplication>
#include "QtNetwork/QNetworkInterface"
#include "QString"

QString getMacAddress()
{
    foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces())
    {
        // Return only the first non-loopback MAC Address
        if (!(interface.flags() & QNetworkInterface::IsLoopBack))
            return interface.hardwareAddress();
        QString text = interface.hardwareAddress();
        qDebug() << text;
    }
    return QString();
}

int main(int argc, char *argv[])
{
    getMacAddress();
    QCoreApplication a(argc, argv);
    return a.exec();
}

我在控制台中什么都没得到?引导我,谢谢...

1 个答案:

答案 0 :(得分:2)

尝试此代码,以显示每个界面的硬件地址:

QString getMacAddress()
{
    QString text;
    foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces())
    {
        text += "Interface:"+interface.hardwareAddress()+"\n";
    }
    return text;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    printf( "%s\n", getMacAddress().toAscii().constData() );
    exit(1);
    return a.exec();
}