【问题标题】:Obtaining MAC address on windows in Qt在 Qt 中获取 windows 上的 MAC 地址
【发布时间】:2011-09-30 11:53:38
【问题描述】:

我正在尝试使用此代码在 windows xp 上获取 mac 地址:

QString getMacAddress()
{
QString macaddress="??:??:??:??:??:??";
#ifdef Q_WS_WIN
PIP_ADAPTER_INFO pinfo=NULL;

unsigned long len=0;
unsigned long nError;

if (pinfo!=NULL)
delete (pinfo);

nError  =   GetAdaptersInfo(pinfo,&len);    //Have to do it 2 times?

if(nError != 0)
{
pinfo= (PIP_ADAPTER_INFO)malloc(len);
nError  =   GetAdaptersInfo(pinfo,&len);
}

if(nError == 0)
macaddress.sprintf("%02X:%02X:%02X:%02X:%02X:%02X",pinfo->Address[0],pinfo->Address[1],pinfo->Address[2],pinfo->Address[3],pinfo->Address[4],pinfo->Address[5]);
#endif
return macaddress;
}

这里建议的代码:http://www.qtforum.org/post/42589/how-to-obtain-mac-address.html#post42589

我应该包含哪些库才能使其正常工作?

【问题讨论】:

  • Getting MAC ID in Qt的可能重复
  • 我认为您应该转到您链接的问题并将其标记为重复,因为我 2 年前问过我的问题

标签: qt4 qtnetwork


【解决方案1】:

使用 Qt 和 QtNetwork 模块,您可以获得如下 MAC 地址之一:

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

【讨论】:

  • 在 Windows 和 MSVC 编译器上,您应该将 QNetworkInterface 变量“interface”替换为其他内容,否则编译失败。另请参阅此线程以获得解释:qt-project.org/forums/viewthread/19133
  • Android上未连接WiFi时,接口变为loopback,hardwareAddress()读作00:00:00:00:00:00
  • 在 Mac OS X/Qt 5.6 上,这将返回一个空字符串。您可以通过仅在值为非空时返回来解决此问题。
【解决方案2】:

我一直在寻找相同的方法,但在使用虚拟机和不同类型的承载时遇到了一些问题,这是我发现的另一种方法:

QNetworkConfiguration nc;
QNetworkConfigurationManager ncm;
QList<QNetworkConfiguration> configsForEth,configsForWLAN,allConfigs;
// getting all the configs we can
foreach (nc,ncm.allConfigurations(QNetworkConfiguration::Active))
{
    if(nc.type() == QNetworkConfiguration::InternetAccessPoint)
    {
        // selecting the bearer type here
        if(nc.bearerType() == QNetworkConfiguration::BearerWLAN)
        {
            configsForWLAN.append(nc);
        }
        if(nc.bearerType() == QNetworkConfiguration::BearerEthernet)
        {
            configsForEth.append(nc);
        }
    }
}
// further in the code WLAN's and Eth's were treated differently
allConfigs.append(configsForWLAN);
allConfigs.append(configsForEth);
QString MAC;
foreach(nc,allConfigs)
{
    QNetworkSession networkSession(nc);
    QNetworkInterface netInterface = networkSession.interface();
    // these last two conditions are for omiting the virtual machines' MAC
    // works pretty good since no one changes their adapter name
    if(!(netInterface.flags() & QNetworkInterface::IsLoopBack)
            && !netInterface.humanReadableName().toLower().contains("vmware")
            && !netInterface.humanReadableName().toLower().contains("virtual"))
    {
        MAC = QString(netInterface.hardwareAddress());
        break;
    }
}

【讨论】:

    猜你喜欢
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 2014-07-24
    相关资源
    最近更新 更多