如题, QProcess 通过 调用 windows 系统 arp.exe 并解析其获取的数据,得到其mac地址,关键代码如下(从项目中摘取,放心使用):
//arp for mac;
m_process->start("c:/windows/system32/arp.exe -a "+ipAddress);
m_process->waitForStarted();
m_process->waitForFinished();
strOut = QString::fromLocal8Bit(m_process->readAllStandardOutput());
if(!strOut.isEmpty() && strOut.contains(ipAddress)){
strList = strOut.split("\r\n");
if(strList.count() >= 4 )
{
strList = strList.at(3).trimmed().split(" ");
QStringList results;
foreach(QString result,strList)
{
if(!result.isEmpty() && !result.trimmed().isEmpty()){
results << result;
}
}
if(results.count() == 3){
mac = results.at(1).trimmed();
}
}
}
if not success(cannot arp ), method 2 -- may be self pc, use ipconfig -all; //"nbtstat.exe -A ipAddress" no effect!;
// if(mac == UNKOWN){
// m_process->start("c:/windows/system32/nbtstat.exe -A " + ipAddress);
// m_process->waitForStarted();
// m_process->waitForFinished();
// QByteArray array = m_process->readAllStandardOutput();
// strOut = QString::fromLocal8Bit(array);
// if(!strOut.isEmpty() && strOut.contains(ipAddress)){
// strList = strOut.split("\r\n");
qDebug() << strList << endl;
// }
// }
if(mac == UNKOWN){
m_process->start("c:/windows/system32/ipconfig.exe -all");
m_process->waitForStarted();
m_process->waitForFinished();
QByteArray array = m_process->readAllStandardOutput();
strOut = QString::fromLocal8Bit(array);
if(!strOut.isEmpty() && strOut.contains(ipAddress)){
// strList = strOut.split("\r\n");
// if(strList.count()>0){
// QStringList newStrList;
// foreach(QString str,strList){
// if(!str.trimmed().isEmpty()){
// newStrList.append(str.trimmed());
// }
// }
// if(newStrList.count()>=2){
// hostName = newStrList.at(1).split(':').at(1).trimmed();
// }
// foreach(QString s, newStrList){
// qDebug() << s;
// }
// }
hostName = "local host";
mac = "ipconfig -all";
}
}