在Qt的Windows上获取MAC地址 [英] Obtaining MAC address on windows in Qt

查看:314
本文介绍了在Qt的Windows上获取MAC地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码在Windows XP上获取Mac地址:

I am attempting to obtain mac address on windows xp using this code:

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

The code was suggested here: http://www.qtforum.org/post/42589/how-to-obtain-mac-address.html#post42589

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

What libraries should i include to make it work?.

推荐答案

借助Qt和QtNetwork模块,您可以获取如下所示的MAC地址之一:

With Qt and the QtNetwork module, you can get one of the MAC addresses like that:

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

这篇关于在Qt的Windows上获取MAC地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆