MAC address code issue [modified]
-
This code to find the MAC address of a PC connected to the internet. What's a sName in the code?
public class GetMacAddressFromIPAddress
{
[DllImport("iphlpapi.dll", ExactSpelling=true)]
public static extern int SendARP( int DestIP, int SrcIP, [Out] byte[] pMacAddr, ref int PhyAddrLen );
public string GetMacAddress(string sName)
{
string s = string.Empty ;
System.Net.IPHostEntry Tempaddr = null;
Tempaddr = (System.Net.IPHostEntry)Dns.GetHostByName(sName);
System.Net.IPAddress[] TempAd = Tempaddr.AddressList;
string[] Ipaddr = new string[3];
foreach(IPAddress TempA in TempAd)
{
Ipaddr[1] = TempA.ToString();
byte[] ab = new byte[6];
int len = ab.Length;
int r = SendARP( (int) TempA.Address, 0, ab, ref len );
string sMAC = BitConverter.ToString( ab, 0, 6 );
Ipaddr[2] = sMAC;
s = sMAC;
}
return s;
}
}modified on Tuesday, May 25, 2010 4:01 AM
-
This code to find the MAC address of a PC connected to the internet. What's a sName in the code?
public class GetMacAddressFromIPAddress
{
[DllImport("iphlpapi.dll", ExactSpelling=true)]
public static extern int SendARP( int DestIP, int SrcIP, [Out] byte[] pMacAddr, ref int PhyAddrLen );
public string GetMacAddress(string sName)
{
string s = string.Empty ;
System.Net.IPHostEntry Tempaddr = null;
Tempaddr = (System.Net.IPHostEntry)Dns.GetHostByName(sName);
System.Net.IPAddress[] TempAd = Tempaddr.AddressList;
string[] Ipaddr = new string[3];
foreach(IPAddress TempA in TempAd)
{
Ipaddr[1] = TempA.ToString();
byte[] ab = new byte[6];
int len = ab.Length;
int r = SendARP( (int) TempA.Address, 0, ab, ref len );
string sMAC = BitConverter.ToString( ab, 0, 6 );
Ipaddr[2] = sMAC;
s = sMAC;
}
return s;
}
}modified on Tuesday, May 25, 2010 4:01 AM
-
And this case , it's the internet ip address and not the pc ip address, isn't it? 2nd thing is that, when using this code, how to suppply internet address for it, do I have to get a code to detect the internet ip address and then pass it to the MAC Function? THanks