以下代码只能获得本网段MAC地址.
对于通过Request.UserHostAddress得到的真实MAC地址,SendARP却是无法解析其MAC地址!
谁能告诉我,如何才能获得任意IP的MAC地址?
-------------------------------------------------------------------------
using System;
using System.Runtime.InteropServices;namespace WebMakefeel
{
public class InternetInformation
{
[DllImport("Iphlpapi.dll")]
public static extern int SendARP (Int32 dest,Int32 host,ref Int64 mac,ref Int32 length);
[DllImport("Ws2_32.dll")]
public static extern Int32 inet_addr (string ip);
private string ipAddr;
public InternetInformation(string ipAddress)
{
ipAddr = ipAddress;
}
public string getMAC_Address()
{
Int32 mysource=inet_addr("172.16.13.91");
Int32 tosource=inet_addr(ipAddr);
Int64 macinfo=new Int64();
Int32 len=6;
SendARP(tosource,mysource,ref macinfo,ref len);
string originalMAC = System.Convert.ToString(macinfo,16);
if (originalMAC.Length != 12)
return originalMAC; string [] factMAC = new string [6];
factMAC[0] = originalMAC.Substring(10,2).ToUpper();
factMAC[1] = originalMAC.Substring(8,2).ToUpper();
factMAC[2] = originalMAC.Substring(6,2).ToUpper();
factMAC[3] = originalMAC.Substring(4,2).ToUpper();
factMAC[4] = originalMAC.Substring(2,2).ToUpper();
factMAC[5] = originalMAC.Substring(0,2).ToUpper(); string finalMAC = "";
for (int i=0;i<=5;i++)
{
if (i==0)
finalMAC = factMAC[i];
else
finalMAC += "-" + factMAC[i];
}
return finalMAC;
//DOS下查看MAC地址命令为: nbtstat -a 计算机名或IP
}
}
}
对于通过Request.UserHostAddress得到的真实MAC地址,SendARP却是无法解析其MAC地址!
谁能告诉我,如何才能获得任意IP的MAC地址?
-------------------------------------------------------------------------
using System;
using System.Runtime.InteropServices;namespace WebMakefeel
{
public class InternetInformation
{
[DllImport("Iphlpapi.dll")]
public static extern int SendARP (Int32 dest,Int32 host,ref Int64 mac,ref Int32 length);
[DllImport("Ws2_32.dll")]
public static extern Int32 inet_addr (string ip);
private string ipAddr;
public InternetInformation(string ipAddress)
{
ipAddr = ipAddress;
}
public string getMAC_Address()
{
Int32 mysource=inet_addr("172.16.13.91");
Int32 tosource=inet_addr(ipAddr);
Int64 macinfo=new Int64();
Int32 len=6;
SendARP(tosource,mysource,ref macinfo,ref len);
string originalMAC = System.Convert.ToString(macinfo,16);
if (originalMAC.Length != 12)
return originalMAC; string [] factMAC = new string [6];
factMAC[0] = originalMAC.Substring(10,2).ToUpper();
factMAC[1] = originalMAC.Substring(8,2).ToUpper();
factMAC[2] = originalMAC.Substring(6,2).ToUpper();
factMAC[3] = originalMAC.Substring(4,2).ToUpper();
factMAC[4] = originalMAC.Substring(2,2).ToUpper();
factMAC[5] = originalMAC.Substring(0,2).ToUpper(); string finalMAC = "";
for (int i=0;i<=5;i++)
{
if (i==0)
finalMAC = factMAC[i];
else
finalMAC += "-" + factMAC[i];
}
return finalMAC;
//DOS下查看MAC地址命令为: nbtstat -a 计算机名或IP
}
}
}
对于通过Request.UserHostAddress得到的真实IP地址,SendARP却是无法解析其MAC地址!
需要客户端装插件: By WMI#region By WMI public static void GetMACByWMI()
{
string query = "select MACAddress from Win32_NetworkAdapterConfiguration where IPEnabled='TRUE'";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection collection = searcher.Get();
foreach (ManagementObject mo in collection)
{
string mac = mo["MACAddress"].ToString();
Console.WriteLine(" Network card MAC Address is :{0}", mac);
}
} #endregion