2011-09-30 3 views
11

Je cherche à obtenir l'adresse mac sur windows xp en utilisant ce code:Obtenir adresse MAC sur Windows dans Qt

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

Le code a été proposé ici: http://www.qtforum.org/post/42589/how-to-obtain-mac-address.html#post42589

quelles bibliothèques dois-je inclure pour le rendre travail?.

Répondre

37

Avec Qt et le module QtNetwork, vous pouvez obtenir l'une des adresses MAC comme ça:

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

le compilateur Windows et MSVC, vous devez remplacer la variable QNetworkInterface « interface » avec quelque chose d'autre, sinon la compilation échoue. Voir aussi ce fil pour l'explication: http://qt-project.org/forums/viewthread/19133 –

+1

Fonctionne sur Ubuntu aussi – PedroMorgan

+0

Lorsque le WiFi n'est pas connecté sur Android, l'interface devient loopback et 'hardwareAddress()' se lit comme '00 : 00: 00: 00: 00: 00 » –

3

Je cherchais même et a eu quelques problèmes avec les machines virtuelles et les différents types de porteurs, voici une autre approche que j'ai trouvé:

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