How to get MAC address from c# [migrated]
- by Andrew Simpson
I have a C# application. In a routine I have code to get the MAC address from using SendARP.
It works on Windows 7 but does not work on Windows XP. I just get a null string returned.
This is my code.
Thanks...
System.Runtime.InteropServices.DllImport("iphlpapi.dll", ExactSpelling = true)]
static extern int SendARP(int DestIP, int SrcIP, byte[] pMacAddr, ref int PhyAddrLen);
public static PhysicalAddress GetMacAddress(IPAddress ipAddress)
{
const int MacAddressLength = 6; //i know it is has a length of 6
int length = MacAddressLength;
var macBytes = new byte[MacAddressLength];
SendARP(BitConverter.ToInt32(ipAddress.GetAddressBytes(), 0), 0, macBytes, ref length);
return new PhysicalAddress(macBytes);
}