Need help using the Windows IP Helper API & ParseNetworString in C#.

Posted by JohnnyNoir on Stack Overflow See other posts from Stack Overflow or by JohnnyNoir
Published on 2009-07-07T22:57:19Z Indexed on 2010/05/28 1:01 UTC
Read the original article Hit count: 236

Filed under:
|
|

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...

© Stack Overflow or respective owner

Related posts about c#

Related posts about winapi