C# 通过IP获取Mac地址文章来源地址https://www.toymoban.com/news/detail-779329.html
[DllImport("Iphlpapi.dll")]
private static unsafe extern int SendARP(Int32 dest, Int32 host, ref Int32 mac, ref Int32 length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);
public static string GetMACFromIP(string ip)
{
string strRet = "";
Int32 intDest = inet_addr(ip);
Int32[] arrMAC = new Int32[2];
Int32 intLen = 6;
int intResult = SendARP(intDest, 0, ref arrMAC[0], ref intLen);
if (intResult == 0)
{
Byte[] arrbyte = new Byte[8];
arrbyte[5] = (Byte)(arrMAC[1] >> 8);
arrbyte[4] = (Byte)arrMAC[1];
arrbyte[3] = (Byte)(arrMAC[0] >> 24);
arrbyte[2] = (Byte)(arrMAC[0] >> 16);
arrbyte[1] = (Byte)(arrMAC[0] >> 8);
arrbyte[0] = (Byte)arrMAC[0];
StringBuilder strbMAC = new StringBuilder();
for (int intIndex = 0; intIndex < 6; intIndex++)
{
if (intIndex > 0) strbMAC.Append("-");
strbMAC.Append(arrbyte[intIndex].ToString("X2"));
}
strRet = strbMAC.ToString();
}
return strRet;
}
文章来源:https://www.toymoban.com/news/detail-779329.html
到了这里,关于C# 通过IP获取Mac地址(ARP)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!