Get MAC Address from IP in Vista with C# (500pts!)

Posted on 2009-02-16
Last Modified: 2013-12-17
What I need is C# code that will take an IP Address and return the MAC Address of a remote device.

The code below accomplishes that in XP but not VISTA.  After reading a few articles it seems that the "Iphlpapi.dll" is not the same in Vista as it is in XP.

How can I accomplish this.  Specific examples would be great!

public string GetMacAddress(object response)


            System.Net.IPAddress ip = System.Net.IPAddress.Parse(this.currentIp);

            System.Net.NetworkInformation.PhysicalAddress mac = GetMacFromIP(ip);

            //Format MAC Address.

            string macString = mac.ToString();

            for (int i = 2; i < macString.Length; i = i + 3)


               macString = macString.Insert(i, ":");


            return macString;


        [System.Runtime.InteropServices.DllImport("Iphlpapi.dll", EntryPoint = "SendARP")]

        internal extern static Int32 SendArp(Int32 destIpAddress, Int32 srcIpAddress, byte[] macAddress, ref Int32 macAddressLength);


        public static System.Net.NetworkInformation.PhysicalAddress GetMacFromIP(System.Net.IPAddress IP)


            if (IP.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)

                throw new ArgumentException("supports just IPv4 addresses");

            Int32 addrInt = IpToInt(IP);

            Int32 srcAddrInt = IpToInt(IP);

            byte[] mac = new byte[6]; // 48 bit    

            int length = mac.Length;

            int reply = SendArp(addrInt, srcAddrInt, mac, ref length);

            byte[] emptyMac = new byte[12];


            if (reply != 0)


                MessageBox.Show("No MAC Address found for IP Address: " + IP.ToString());

                return new System.Net.NetworkInformation.PhysicalAddress(emptyMac);


            return new System.Net.NetworkInformation.PhysicalAddress(mac);


        private static Int32 IpToInt(System.Net.IPAddress IP)


            byte[] bytes = IP.GetAddressBytes();

            return BitConverter.ToInt32(bytes, 0);


Open in new window

Question by:psft
    LVL 1

    Accepted Solution

    Source and destination IPs are the same in your SendARP call.  A source address which is not linked to any network interface will return an error according to

    Try to put 0 as source IP.
    LVL 1

    Author Closing Comment

    You were right thanks for the insight

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now