Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining MAC address on windows in Qt

Tags:

qt4

qtnetwork

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

like image 670
Gandalf Avatar asked Sep 30 '11 11:09

Gandalf


People also ask

How do I find my Qt MAC address?

QString QNetworkInterface::hardwareAddress() const Returns the low-level hardware address for this interface. On Ethernet interfaces, this will be a MAC address in string representation, separated by colons. Other interface types may have other types of hardware addresses.

Is the MAC address stored in the registry?

Your computers MAC address can be located by browsing the Windows registry to take advantage of network security features, such as limiting wireless access to only MAC addresses owned by your company.


1 Answers

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();
}
like image 75
alexisdm Avatar answered Sep 17 '22 05:09

alexisdm