5

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

主文件

#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();
}

我在控制台中一无所获?指导我谢谢...

4

1 回答 1

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();
}
于 2013-03-08T08:06:37.530 回答