[DllImport("Iphlpapi.dll")]
public static extern uint SendARP(uint DestIP, uint SrcIP, ref ulong pMacAddr, ref uint PhyAddrLen);
public string GetMac(string IP) {
string Mac = "";
IPAddress _Address;
if (!IPAddress.TryParse(IP, out _Address)) return null;
uint DestIP = System.BitConverter.ToUInt32(_Address.GetAddressBytes(), 0);
ulong pMacAddr = 0;
uint PhyAddrLen = 6;
//UpdateList showmac = new UpdateList(UpdateMyList);
//UpdateLabel upmylabel = new UpdateLabel(UpdateMyLabel);
try//捕获程序异常
{
uint error_code = SendARP(DestIP, 0, ref pMacAddr, ref PhyAddrLen);//发送ARP包,获取MAC
byte[] _Bytes1 = BitConverter.GetBytes(pMacAddr);
Mac = BitConverter.ToString(_Bytes1, 0, 6);
}
catch
{
Mac = "00-00-00-00-00-00";
}
return Mac;
//if (showmac != null) {
// //listBox4.Invoke(showmac, IP, Mac);//激活委托,执行委托入口程序 //label5.Invoke(upmylabel );
//}
}