Need help using the Windows IP Helper API & ParseNetworString in C#.
- by JohnnyNoir
I'm attempting to rewrite some C# web service code that uses the Windows IP Helper API call "SendARP" to retreive a remote system's MAC address. SendARP works great - until you cross a network segment as Arp requests aren't routed. I've been looking at the "ParseNetworkString" documentation after happening across its existance on StackOverflow. The quick & dirty algorithm I have in mind is:
public static string GetMacAddress(string strHostOrIP)
{
if (strHostOrIP is IPAddress)
{
parse results of nbstat -A strHostOrIP
return macAddress
}
if (strHostOrIP is Hostname)
{
IPHostEntry hostEntry = null;
try
{
hostEntry = Dns.GetHostEntry(strHostOrIP);
}
catch
{
return null;
}
if (hostEntry.AddressList.Length == 0)
{
return null;
}
foreach (IPAddress ip in hostEntry.AddressList)
{
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
ipAddress = ip;
break;
}
}
}
return GetMACAddress(ipAddress);
}
"ParseNetworkString" is new with Vista/Win2008 so I haven't been able to find example C# code demonstrating its use and I'm no C++ coder so if anyone can point me in the right direction...