How to get the MAC address of your network adapter.

24 05 2008


MAC address stands for Media Access Control address which is 6 bytes( 48 bits ) long. MAC address is the unique address which is used to identify network interface hardware. So how to get the MAC address of your network adapter?


You can use the function – GetAdaptersInfo(). The network adapter information will be populated and filled back in IP_ADAPTER_INFO structure. In that structure, you can get the network adapter name, MAC address and a couple of other information. See the sample code snippet.

#include "Iphlpapi.h"
...
// Get the buffer length required for IP_ADAPTER_INFO.
ULONG BufferLength = 0;
BYTE* pBuffer = 0;
if( ERROR_BUFFER_OVERFLOW == GetAdaptersInfo( 0, &BufferLength ))
{
    // Now the BufferLength contain the required buffer length.
    // Allocate necessary buffer.
    pBuffer = new BYTE[ BufferLength ];
}
else
{
    // Error occurred. handle it accordingly.
}

// Get the Adapter Information.
PIP_ADAPTER_INFO pAdapterInfo =
      reinterpret_cast<PIP_ADAPTER_INFO>(pBuffer);
GetAdaptersInfo( pAdapterInfo, &BufferLength );

// Iterate the network adapters and print their MAC address.
while( pAdapterInfo )
{
    // Assuming pAdapterInfo->AddressLength is 6.
    CString csMacAddress;
    csMacAddress.Format(_T("%02x:%02x:%02x:%02x:%02x:%02x"),
                        pAdapterInfo->Address[0],
                        pAdapterInfo->Address[1],
                        pAdapterInfo->Address[2],
                        pAdapterInfo->Address[3],
                        pAdapterInfo->Address[4],
                        pAdapterInfo->Address[5]);

    cout << "Adapter Name :" << pAdapterInfo->AdapterName << " "
         << "MAC :" << (LPCTSTR) csMacAddress << endl;

    // Get next adapter info.
    pAdapterInfo = pAdapterInfo->Next;
}

// deallocate the buffer.
delete[] pBuffer;


Don’t forget to add Iphlpapi.lib to project settings.


Targeted Audience – Beginners.

Advertisements

Actions

Information

6 responses

20 09 2008
Ananth

Hello,
From where can I get Iphlpapi.h and Iphlpapi.lib? I am using VC++ 6.0.

Regards
Ananth

19 10 2008
Sachin

Ananth, you need to download the Microsoft SDKs which is not included in the Visual C++ 6, that contains new libraries, headers for VC++ 6.

4 03 2009
prasanna

Hello,

Sometimes, this mac address is returning morethan one mac id. Then how should i find in them which is the active macid

Regards
Prasanna

4 03 2009
Jijo.Raj

Hello Prasanna,

Check this link for getting the active IP address(es) of your machine – http://bobmoore.mvps.org/Win32/w32tip19.htm.

And IP_ADAPTER_INFO.CurrentIpAddress contains the active IP of the adapter. So loop through all adapter info and get the active adapter and there by you can get the mac address of active adapter.

Well, keep in mind that, a PC can have multiple adapter and multiple IP addresses configured at same time. That does mean that there can be multiple active MAC addresses. Hope it helps you.

Regards,
Jijo.

26 03 2009
Just Another Developer

In the 15 years of C++ development, I’ve never seen the NIC Name show up under AdapterName property. It’s always shown up under Description.

26 03 2009
Just Another Developer

BTW, your params for calling GetAdapterInfo() is backwards. Maybe you should test your code before posting?

DWORD GetAdaptersInfo(
__out PIP_ADAPTER_INFO pAdapterInfo,
__inout PULONG pOutBufLen
);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s




%d bloggers like this: