Home All Groups Group Topic Archive Search About

Enumerating MAC addresses

Author
21 Dec 2005 2:23 AM
Bob Altman
Hi all,

I'm looking for a way to enumerate the MAC addresses of all of "live" NICs
on my computer.  I can enumerate all of the "live" IP addresses like this:

    Dns.GetHostByName(Dns.GetHostName).AddressList

but I don't know how to get the associated MAC addresses.

Here's the problem I'm actually trying to solve:  When my app runs, I want
it to always talk to the same network wire, even if the host IP address on
that wire changes.  My solution is to enumerate the network connections
(using code like that shown above) when the app starts.  If there are more
than one connection then I would prompt the user for the connection to use,
and I would remember the MAC address so that I don't need to bother the user
the next the app is run (even if DHCP gives the computer a new IP address).

TIA!

  - Bob

Author
21 Dec 2005 4:35 AM
Remy
I think that should solve it:
http://www.codeproject.com/csharp/Host_Info_within_Network.asp

(from the page)
The physical MAC address is collected using the DLL "iphlpapi.dll" with
the API SendARP().

For each System, we use the Hostname to retrieve the MAC address.

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

Cheers

Remy Blaettler
<a href="http://www.collaboral.com">www.collaboral.com</A>
Author
21 Dec 2005 4:51 AM
Bob Altman
Thanks Remy!  While waiting for a reply to my posting, I dug through the
docs and put together this solution to my problem using WMI:

Imports System.Management
Private Sub Test()
  ' Ask WMI to fetch the network adapter info
  ' for enabled adapters
  Dim WMISearcher As New ManagementObjectSearcher( _
    "Select MACAddress, IPAddress " & _
    "from Win32_NetworkAdapterConfiguration " & _
    "where IPEnabled=TRUE")

  For Each mo As ManagementObject In WMISearcher.Get()
    Dim ipAddrs() As String = DirectCast(mo("IPAddress"), String())
    Dim macAddr As String = CStr(mo("MACAddress"))

    ' Each adapter has one MAC address and an array of IP addresses
    For Each ipAddr As String In ipAddrs
      Debug.WriteLine(macAddr & " - " & ipAddr)
    Next
  Next
End Sub
Author
12 Mar 2006 5:20 PM
John Vorchak eMVP
"Bob Altman" wrote:

> Thanks Remy!  While waiting for a reply to my posting, I dug through the
> docs and put together this solution to my problem using WMI:
>

Instead of using WMI to do this, you can perform this through the Net and
NetworkInformation class.  This is a portion of a piece of code that I'm
currently working on that I just happen to need managed call to the MAC (I
ripped this out of my code and tossed it into a console app so it's not
exactly neat...):

//*****Start C# Snippet
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.NetworkInformation;

namespace getMac
{
    class Program
    {
        static void Main(string[] args)
        {

            int adaptNum = 0;
            NetworkInterface[] adapters =
NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in adapters)
            {
                IPInterfaceProperties adapterProperties =
adapter.GetIPProperties();
                UnicastIPAddressInformationCollection uniCast =
adapterProperties.UnicastAddresses;

                foreach (IPAddressInformation uni in uniCast)
                {
                    //increment adapt number
                    adaptNum++;
                    //get mac
                    System.Net.NetworkInformation.PhysicalAddress mac =
adapter.GetPhysicalAddress();
                    Console.WriteLine("Adapter #{0} -------- {1}",
adaptNum.ToString(), mac.ToString());
                }
            }
        }
    }
}
//*****End C# Snippet

One word of caution, this will bring back everything, including loopback
adapter, so make sure you build handling in there for empty MACs.

John Vorchak eMVP
Author
22 Dec 2005 4:08 PM
Peter Franks
Bob Altman wrote:
> Hi all,
>
> I'm looking for a way to enumerate the MAC addresses of all of "live" NICs
> on my computer.  I can enumerate all of the "live" IP addresses like this:

static string[] MacAddresses
{
   get
   {
     ArrayList macAddresses = new ArrayList();

     ManagementObjectSearcher query = new
ManagementObjectSearcher("SELECT * FROM
Win32_NetworkAdapterConfiguration");
     foreach (ManagementObject each in query.Get())
     {
       string macAddress = each["MACAddress"] != null ?
each["MACAddress"].ToString() : null;
       bool ipEnabled = (bool)each["IPEnabled"];
       if (macAddress != null && ipEnabled)
       {
         if (!macAddresses.Contains(macAddress))
         {
           macAddresses.Add(macAddress);
         }
       }
     }
     return macAddresses.Count > 0 ?
(string[])macAddresses.ToArray(typeof(string)) : null;
   }
}

AddThis Social Bookmark Button