Click here to Skip to main content
15,113,873 members
Articles / Programming Languages / C#
Article
Posted 7 Dec 2006

Stats

104.3K views
2.7K downloads
41 bookmarked

Network Adapter MAC / IP Address Info Display Application Using C# and .NET

Rate me:
Please Sign up or sign in to vote.
3.32/5 (12 votes)
14 Aug 2007CPOL
Network Adapter MAC / IP Address Info Display Application in C# .NET

Sample Image - AdapterInfo.jpg

Introduction

The AdapterInfo project is a simple C# .NET project that will report the "friendly" adapter name, the IP address assigned, and the physical (MAC) address for the user. This is helpful when an application needs to retrieve the MAC address for a certain IP address, or vice versa, when having only one piece of the puzzle. The project also demonstrates simple registry reads.

Project highlights

I found it advantageous to write this application because of the fact that the MAC address of a network adapter is not stored in an easy to get to location, such as the Registry. So, I needed a mechanism to connect the IP address of an adapter to the MAC address of it. Once the two are "matched", I then have the MAC address for my use. This is done by retrieving the "AdapterName" field form the IP_Adapter_Addresses data structure.

C#
IntPtr PAdaptersAddresses = new IntPtr();
bool AdapterFound = false;
uint pOutLen = 100;

PAdaptersAddresses = Marshal.AllocHGlobal(100);
uint ret = GetAdaptersAddresses(0, 0, (IntPtr)0, 
               PAdaptersAddresses, ref pOutLen);

// if 111 error, use 
if (ret == 111)
{
    Marshal.FreeHGlobal(PAdaptersAddresses);
    PAdaptersAddresses = Marshal.AllocHGlobal((int) pOutLen);
    ret = GetAdaptersAddresses(0, 0, (IntPtr)0, 
                               PAdaptersAddresses, ref pOutLen);
}

IP_Adapter_Addresses adds = new IP_Adapter_Addresses();
IntPtr pTemp = PAdaptersAddresses;
IntPtr pTempIP = new IntPtr();

while (pTemp != (IntPtr)0)
{
    Marshal.PtrToStructure(pTemp, adds);
    string adapterName = Marshal.PtrToStringAnsi(adds.AdapterName);
    string FriendlyName = Marshal.PtrToStringAuto(adds.FriendlyName);
    string tmpString = string.Empty;
    for (int i = 0; i < 6; i++)
    {
        tmpString += string.Format("{0:X2}", adds.PhysicalAddress[i]);
        if (i < 5)
        {
            tmpString += ":";
        }
    }

    RegistryKey theLocalMachine = Registry.LocalMachine; 
    RegistryKey theSystem = theLocalMachine.OpenSubKey(@"SYSTEM\Current" + 
                            @"ControlSet\Services\Tcpip\Parameters\Interfaces"); 
    RegistryKey theInterfaceKey = theSystem.OpenSubKey(adapterName); 

    if (theInterfaceKey != null)
    {
        string DhcpIPAddress = (string)theInterfaceKey.GetValue("DhcpIPAddress");
        // system is using DHCP
        if (DhcpIPAddress != null)
        {
            string tArray = (string)
            theInterfaceKey.GetValue("DhcpIPAddress", theInterfaceKey);
            AdapterInfoTextBox.Text += "Network adapter: " + 
                                       FriendlyName.ToString() + "\r\n";
            AdapterInfoTextBox.Text += "IP Address: " + tArray + "\r\n";
            AdapterInfoTextBox.Text += "MAC Address: " + tmpString + "\r\n\r\n";
            AdapterFound = true;
        }
        else
        {
            string[] tArray = (string[])
            theInterfaceKey.GetValue("IPAddress", theInterfaceKey);
            AdapterInfoTextBox.Text += "Network adapter: " + 
                                       FriendlyName.ToString() + "\r\n";
            for (int Interface = 0; Interface < tArray.Length; Interface++)
            {
                AdapterInfoTextBox.Text += "IP Address: " + 
                                           tArray[Interface] + "\r\n";
                AdapterFound = true;
            }
            AdapterInfoTextBox.Text += "MAC Address: " + tmpString + "\r\n\r\n";
        }
    }
    pTemp = adds.Next;
}

if (AdapterFound != true)
{
    AdapterInfoTextBox.Text += "No network adapters configured/present\r\n";
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Brian Riek
Technical Lead
United States United States
Wisconsin native living in Phoenix, AZ and enjoying the sun.

Software design and architecture for desktop and web solutions.

brian.d.riek@gmail.com

Comments and Discussions

 
Questiongood information Pin
srilekhamenon6-Jun-18 21:31
professionalsrilekhamenon6-Jun-18 21:31 
GeneralMy vote of 1 Pin
vinu paul15-Apr-13 0:07
Membervinu paul15-Apr-13 0:07 
Questionget inf from network card only Pin
imans6222-Jun-10 21:33
Memberimans6222-Jun-10 21:33 
GeneralThe recommended method of calling the GetAdaptersAddresses... Pin
Jeffrey Walton2-Jun-10 11:57
MemberJeffrey Walton2-Jun-10 11:57 
PAdaptersAddresses = Marshal.AllocHGlobal(100);
uint ret = GetAdaptersAddresses(0, 0, (IntPtr)0, 
               PAdaptersAddresses, ref pOutLen);

// if 111 error, use 
if (ret == 111) {
    ...
}
The recommended method of calling the GetAdaptersAddresses function is to pre-allocate a 15KB working buffer pointed to by the AdapterAddresses parameter. On typical computers, this dramatically reduces the chances that the GetAdaptersAddresses function returns ERROR_BUFFER_OVERFLOW, which would require calling GetAdaptersAddresses function multiple times.

http://msdn.microsoft.com/en-us/library/aa365915(VS.85).aspx[^]
Generali am not getting the exact ip address Pin
Gop'z14-Aug-07 3:19
MemberGop'z14-Aug-07 3:19 
GeneralRe: i am not getting the exact ip address Pin
Brian Riek14-Aug-07 9:33
MemberBrian Riek14-Aug-07 9:33 
GeneralRe: i am not getting the exact ip address Pin
Brian Riek10-Oct-07 15:05
MemberBrian Riek10-Oct-07 15:05 
GeneralEasier way Pin
Garth Watkins7-Mar-07 0:26
MemberGarth Watkins7-Mar-07 0:26 
GeneralRe: Easier way Pin
Brian Riek23-Apr-07 10:47
MemberBrian Riek23-Apr-07 10:47 
GeneralRe: Easier way Pin
Lex Li9-Jul-07 22:36
professionalLex Li9-Jul-07 22:36 
GeneralRe: Easier way Pin
Brian Riek19-Jul-07 7:18
MemberBrian Riek19-Jul-07 7:18 
GeneralThanx! this is Simple but useful article Pin
Asif Ashraf30-Dec-06 22:13
professionalAsif Ashraf30-Dec-06 22:13 
GeneralRe: Thanx! this is Simple but useful article Pin
Gop'z14-Aug-07 0:25
MemberGop'z14-Aug-07 0:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.