更新时间:2023-10-22 23:00:22
以下是获取通信对等方的MAC地址的代码.
在后台,它使用Windows命令 arp .
使用在Windows 7上测试过的Qt5.8:
Here is the code to get the MAC address of the communication peer.
Under the hood, it uses the Windows command arp.
Using Qt5.8, tested on Windows 7:
QString getMacForIP(QString ipAddress)
{
QString MAC;
QProcess process;
//
process.start(QString("arp -a %1").arg(ipAddress));
if(process.waitForFinished())
{
QString result = process.readAll();
QStringList list = result.split(QRegularExpression("\\s+"));
if(list.contains(ipAddress))
MAC = list.at(list.indexOf(ipAddress) + 1);
}
//
return MAC;
}
备注:远程对等方必须位于同一LAN上.
另一点注意:如果IP地址不存在,您将获得一个用于MAC的空字符串.
Remark: remote peer must be on the same LAN.
Another remark: you'll get an empty string for MAC if the IP address is not present.