1

I'm trying to convert IP address to MAC address and then convert it to byte array. I'm stuck in the first part and not sure how to do this. I seen some search results talking about System.Net.NetworkInformation.NetworkInterface but not sure how to use it.

This is my code which needs MAC byte array. How to do this?

[DllImport("iphlpapi.dll", ExactSpelling = true)]
public static extern int SendARP(int DestIP, int SrcIP, byte[] pMacAddr, ref uint PhyAddrLen);

private void Ping(IPAddress address)
{
    byte[] macAddr = new byte[6];
    uint macAddrLen = uint.Parse(macAddr.Length.ToString());

    if (SendARP(int.Parse(address.ToString()), 0, macAddr, ref macAddrLen) == 0)
    {
        //SUCCESS!
    }
}
3
  • 1
    What are you trying to do? MAC address is not accessible, except on the host machine, so I don't understand your question Commented Jan 19, 2012 at 15:16
  • @CharlesB, I'm trying to make this work. The problem is that my last variable macAddrLen is not assigned. Commented Jan 19, 2012 at 15:17
  • 2
    The MAC address of an adapter is only available on the same network segment. Not on the other side of a router. Commented Jan 19, 2012 at 15:17

3 Answers 3

6

Get about about what you try to do. It isakin to translate your phone number to the name of the street - there is NO correlation between them.

The MAC address is coded in the ethernet level driver while the IP address is an artificial construct higher up by the IP protocol. THey ahve ZERo relationship. Routers find the MAC addresses to send IP packets to via aprotocol (ARP - Address Resolution Protocol) and this can not cross network segments.

Sign up to request clarification or add additional context in comments.

1 Comment

Very clear explanation. Ok, so I been looking at it the wrong way. +1.
4

Have a look here:

byte[] macAddr = new byte[6];
uint macAddrLen = (uint) macAddr.Length;
if (SendARP((int)address.Address, 0, macAddr, ref macAddrLen) != 0)
    throw new InvalidOperationException("SendARP failed.");

string[] str = new string[(int)macAddrLen];
for (int i = 0; i < macAddrLen; i++)
    str[i] = macAddr[i].ToString("x2");

Console.WriteLine(string.Join(":", str));

5 Comments

I'm trying this code but same as mine it causes error at if statement line: Input string was not in a correct format. You know why is that?
@HelpNeeder please see my edit. The error is in the call to int.Parse (converting the address to a string would return an IP in the form "x.x.x.x" which cannot be converted to int)
The code doesn't work because you're trying to convert an IP to string and then parse that into an int -> int.Parse(address.ToString()). For the code to work you need an IPaddress object and then use it like this: (int)YourIPObject.Address
@R34lthing, int.Parse(address.Address) doesn't quite cut it. I need int.Parse(address.Address.ToString()) so it would even compile. Now the problem is that my address range goes for example from 255.255.255.0 to 255.255.255.127 and then I get an error: Value was either too large or too small for an Int32.
Never mind. I have solved it to change type of my first parameter to string.
3

Actually the MAC Address is given to you. You do not need to know what MAC Address the Computer has. macAddr will hold the MAC address after you've called the function SendARP.

In the MSDN documentation you can see that the parameters macAddr and macAddrLen are marked as [out] which means the function uses this arguments to give you a value back.

The whole point of the Address Resolution Protocol is that you can resolve Network layer addresses (IP) to physical adresses (MAC)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.