Obtaining MAC address on windows in Qt
Asked Answered
J

2

13

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

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?.

Jointress answered 30/9, 2011 at 11:53 Comment(2)
Possible duplicate of Getting MAC ID in QtTelepathist
I think you should go to the question you linked and mark it as duplicate since i asked mine 2 years earlierJointress
J
45

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();
}
Japhetic answered 30/9, 2011 at 21:8 Comment(3)
On Windows and MSVC compiler, you should replace the QNetworkInterface variable "interface" with something else, otherwise compiling fails. Also see this thread for explanation: qt-project.org/forums/viewthread/19133Spiracle
When WiFi is not connected on Android, the interface becomes loopback and hardwareAddress() reads as 00:00:00:00:00:00.Postobit
On Mac OS X/Qt 5.6 this returns an empty string. You can fix this by only returning if the value is non-empty.Fosterling
V
3

I was looking for the same and had some problems with virtual machines and different types of bearers, here's another approach that I found:

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;
    }
}
Vaud answered 9/8, 2016 at 5:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.