Three Ways To Get Your System's Network MAC Address

By: mykle hoban

Abstract: This article describes three different ways to programatically get the MAC address of your system's network card (NIC).

Determining The MAC Address of Your System in Three Parts

I have searched endlessly through newsgroups and websites for a simple way to determine the MAC (network interface card) adress of my system. You would think that because of the overwhelming demand (especially in newsgroups) for an answer to this question, that there would be many examples available. This is not so. These examples are as a result of tireless searching and some actual learning on my part (imagine that!). Note: none of these examples involve parsing the output of ipconfig.exe /all <g>

Table Of Contents
Method 1 - Netbios API
Method 2 - Using the COM API
Method 3 Using SNMP
Example Projects

The purpose of this article is to give easy methods of determining your MAC address. I will explain how the code works and provide sample projects to illustrate it. I am assuming that you have a grasp of the following concepts:

  • Borland C++Builder
  • Simple network concepts
  • Some Win32 API

Method 1 - Using the Netbios API   [code]

This method of getting your computer's MAC address uses Microsoft's Netbios API. This is a set of commands that enables lower-level network support than provided by say Winsock. The disadvantage to using Netbios to determine your address is that you must have Netbios installed (not really a problem if you are on a Windows network and use file sharing). Otherwise this method is quick and accurate.

The Netbios API includes only one function, called simply Netbios. This function takes a network control block structure as its parameter, which tells it what it needs to do. The structure is defined as follows:

typedef struct _NCB { 
    UCHAR  ncb_command; 
    UCHAR  ncb_retcode; 
    UCHAR  ncb_lsn; 
    UCHAR  ncb_num; 
    PUCHAR ncb_buffer; 
    WORD   ncb_length; 
    UCHAR  ncb_callname[NCBNAMSZ]; 
    UCHAR  ncb_name[NCBNAMSZ]; 
    UCHAR  ncb_rto; 
    UCHAR  ncb_sto; 
    void (CALLBACK *ncb_post) (struct _NCB *); 
    UCHAR  ncb_lana_num; 
    UCHAR  ncb_cmd_cplt; 
#ifdef _WIN64
    UCHAR  ncb_reserve[18];
    UCHAR  ncb_reserve[10]; 
    HANDLE ncb_event; 

Focus especially on the ncb_command member. This is the member that tells Netbios what to do. We will use 3 commands in determining the MAC address. They are defined on MSDN as follows:

NCBENUM Windows NT/2000: Enumerates LAN adapter (LANA) numbers. When this code is specified, the ncb_buffer member points to a buffer to be filled with a LANA_ENUM structure.

NCBENUM is not a standard NetBIOS 3.0 command.
NCBRESET Resets a LAN adapter. An adapter must be reset before it can accept any other NCB command that specifies the same number in the ncb_lana_num member.
NCBASTAT Retrieves the status of either a local or remote adapter. When this code is specified, the ncb_buffer member points to a buffer to be filled with an ADAPTER_STATUS structure, followed by an array of NAME_BUFFER structures.

Here are the steps to getting the MAC address(es) of your system:

  • Enumerate the adaptors
  • Reset each adaptor to get proper information from it
  • Query the adaptor to get the MAC address and put it in standard colon-separated format
The code below provides a simple illustration of these concepts. For more information on Netbios functions, refer to the Microsoft help files or MSDN.

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>

using namespace std;
#define bzero(thing,sz) memset(thing,0,sz)

bool GetAdapterInfo(int adapter_num, string &mac_addr)
    // Reset the LAN adapter so that we can begin querying it
  NCB Ncb;
  memset(&Ncb, 0, sizeof(Ncb));
  Ncb.ncb_command = NCBRESET;
  Ncb.ncb_lana_num = adapter_num;
  if (Netbios(&Ncb) != NRC_GOODRET) {
    mac_addr = "bad (NCBRESET): ";
    mac_addr += string(Ncb.ncb_retcode);
    return false;

  // Prepare to get the adapter status block
  Ncb.ncb_command = NCBASTAT;
  Ncb.ncb_lana_num = adapter_num;
  strcpy((char *) Ncb.ncb_callname, "*");
  struct ASTAT
    NAME_BUFFER NameBuff[30];
  } Adapter;
  Ncb.ncb_buffer = (unsigned char *)&Adapter;
  Ncb.ncb_length = sizeof(Adapter);

  // Get the adapter's info and, if this works, return it in standard,
  // colon-delimited form.
  if (Netbios(&Ncb) == 0)
    char acMAC[18];
    sprintf(acMAC, "%02X:%02X:%02X:%02X:%02X:%02X",
            int (Adapter.adapt.adapter_address[0]),
            int (Adapter.adapt.adapter_address[1]),
            int (Adapter.adapt.adapter_address[2]),
            int (Adapter.adapt.adapter_address[3]),
            int (Adapter.adapt.adapter_address[4]),
            int (Adapter.adapt.adapter_address[5]));
    mac_addr = acMAC;
    return true;
    mac_addr = "bad (NCBASTAT): ";
    mac_addr += string(Ncb.ncb_retcode);
    return false;

int main()
  // Get adapter list
  LANA_ENUM AdapterList;
  NCB Ncb;
  memset(&Ncb, 0, sizeof(NCB));
  Ncb.ncb_command = NCBENUM;
  Ncb.ncb_buffer = (unsigned char *)&AdapterList;
  Ncb.ncb_length = sizeof(AdapterList);

  // Get all of the local ethernet addresses
  string mac_addr;
  for (int i = 0; i < AdapterList.length - 1; ++i)
    if (GetAdapterInfo(AdapterList.lana[i], mac_addr))
      cout << "Adapter " << int (AdapterList.lana[i]) <<
              "'s MAC is " << mac_addr << endl;
      cerr << "Failed to get MAC address! Do you" << endl;
      cerr << "have the NetBIOS protocol installed?" << endl;

  return 0;


Method 2 - COM GUID API   [code]

This method uses the COM API to create a GUID (globably unique identifier) and derive the MAC address therefrom. GUIDs are used commonly to identify COM components and other objects in the system. They are computed from the MAC address (among other things) and ostensibly contain that address within them. I say ostensibly because that is not at all certain. I provide this method mostly as an example of what not to do. You may end up with you MAC address this way, but chances are you'll just end up with some random hex values.

This is pretty simple and doesn't require much explanation. We create a GUID with CoCreateGuid and dump the last six bytes into a string. These should be the MAC address, but as I said, there's no way to be certain.

#include <windows.h>
#include <iostream>
#include <conio.h>

using namespace std;

int main()
    cout << "MAC address is: ";

    // Ask COM to create a UUID for us.  If this machine has an Ethernet
    // adapter, the last six bytes of the UUID (bytes 2-7 inclusive in
    // the Data4 element) should be the MAC address of the local
    // Ethernet adapter.
    GUID uuid;
    // Spit the address out
    char mac_addr[18];
    cout << mac_addr << endl;
    return 0;

Method 3 - Using the SNMP Extension API   [code]

The third method I will talk about is using the SNMP (Simple Network Management Protocol) extension in Windows to get your system's address. The protocol in my experience is anything but simple, but the code should read pretty straight-forwardly. Basically the steps are the same as with Netbios:

  • Get a list of adapters
  • Query each adapter for type and MAC address
  • Save the adapters that are actual NICs
I don't personally know much about SNMP, but as I said before, the code is pretty clear. For more information, refer to the following URLs:

SNMP Functions
SNMP Variable Types and Request PDU Types
SNMP Structures

#include <snmp.h>
#include <conio.h>
#include <stdio.h>

typedef bool(WINAPI * pSnmpExtensionInit) (
        IN DWORD dwTimeZeroReference,
        OUT HANDLE * hPollForTrapEvent,
        OUT AsnObjectIdentifier * supportedView);

typedef bool(WINAPI * pSnmpExtensionTrap) (
        OUT AsnObjectIdentifier * enterprise,
        OUT AsnInteger * genericTrap,
        OUT AsnInteger * specificTrap,
        OUT AsnTimeticks * timeStamp,
        OUT RFC1157VarBindList * variableBindings);

typedef bool(WINAPI * pSnmpExtensionQuery) (
        IN BYTE requestType,
        IN OUT RFC1157VarBindList * variableBindings,
        OUT AsnInteger * errorStatus,
        OUT AsnInteger * errorIndex);

typedef bool(WINAPI * pSnmpExtensionInitEx) (
        OUT AsnObjectIdentifier * supportedView);

void main()
  HINSTANCE m_hInst;
  pSnmpExtensionInit m_Init;
  pSnmpExtensionInitEx m_InitEx;
  pSnmpExtensionQuery m_Query;
  pSnmpExtensionTrap m_Trap;
  HANDLE PollForTrapEvent;
  AsnObjectIdentifier SupportedView;
  UINT OID_ifEntryType[] = {1, 3, 6, 1, 2, 1, 2, 2, 1, 3};
  UINT OID_ifEntryNum[] = {1, 3, 6, 1, 2, 1, 2, 1};
  UINT OID_ipMACEntAddr[] = {1, 3, 6, 1, 2, 1, 2, 2, 1, 6};
  AsnObjectIdentifier MIB_ifMACEntAddr =
    { sizeof(OID_ipMACEntAddr)  sizeof(UINT), OID_ipMACEntAddr };
  AsnObjectIdentifier MIB_ifEntryType =
    {sizeof(OID_ifEntryType)  sizeof(UINT), OID_ifEntryType};
  AsnObjectIdentifier MIB_ifEntryNum =
    {sizeof(OID_ifEntryNum)  sizeof(UINT), OID_ifEntryNum};
  RFC1157VarBindList varBindList;
  RFC1157VarBind varBind[2];
  AsnInteger errorStatus;
  AsnInteger errorIndex;
  AsnObjectIdentifier MIB_NULL = {0, 0};
  int ret;
  int dtmp;
  int i = 0, j = 0;
  bool found = false;
  char TempEthernet[13];
  m_Init = NULL;
  m_InitEx = NULL;
  m_Query = NULL;
  m_Trap = NULL;

  /* Load the SNMP dll and get the addresses of the functions
     necessary */
  m_hInst = LoadLibrary("inetmib1.dll");
    m_hInst = NULL;
  m_Init =
    (pSnmpExtensionInit) GetProcAddress(m_hInst, "SnmpExtensionInit");
  m_InitEx =
    (pSnmpExtensionInitEx) GetProcAddress(m_hInst,
  m_Query =
    (pSnmpExtensionQuery) GetProcAddress(m_hInst,
  m_Trap =
    (pSnmpExtensionTrap) GetProcAddress(m_hInst, "SnmpExtensionTrap");
  m_Init(GetTickCount(), &PollForTrapEvent, &SupportedView);

  /* Initialize the variable list to be retrieved by m_Query */
  varBindList.list = varBind;
  varBind[0].name = MIB_NULL;
  varBind[1].name = MIB_NULL;

  /* Copy in the OID to find the number of entries in the
     Inteface table */
  varBindList.len = 1;        /* Only retrieving one item */
  SNMP_oidcpy(&varBind[0].name, &MIB_ifEntryNum);
  ret =
    m_Query(ASN_RFC1157_GETNEXTREQUEST, &varBindList, &errorStatus,
  printf("# of adapters in this system : %in",
  varBindList.len = 2;

  /* Copy in the OID of ifType, the type of interface */
  SNMP_oidcpy(&varBind[0].name, &MIB_ifEntryType);

  /* Copy in the OID of ifPhysAddress, the address */
  SNMP_oidcpy(&varBind[1].name, &MIB_ifMACEntAddr);


    /* Submit the query.  Responses will be loaded into varBindList.
       We can expect this call to succeed a # of times corresponding
       to the # of adapters reported to be in the system */
    ret =
      m_Query(ASN_RFC1157_GETNEXTREQUEST, &varBindList, &errorStatus,
    if (!ret)
      ret = 1;
        /* Confirm that the proper type has been returned */
      ret =
          SNMP_oidncmp(&varBind[0].name, &MIB_ifEntryType,
                       MIB_ifEntryType.idLength); if (!ret) {
    dtmp = varBind[0].value.asnValue.number;
    printf("Interface #%i type : %in", j, dtmp);

    /* Type 6 describes ethernet interfaces */
    if (dtmp == 6)

      /* Confirm that we have an address here */
      ret =
          SNMP_oidncmp(&varBind[1].name, &MIB_ifMACEntAddr,
      if ((!ret) && (varBind[1] != NULL))
        if((varBind[1][0] == 0x44)
          && (varBind[1][1] == 0x45)
          && (varBind[1][2] == 0x53)
          && (varBind[1][3] == 0x54)
          && (varBind[1][4] == 0x00))
          /* Ignore all dial-up networking adapters */
          printf("Interface #%i is a DUN adaptern", j);
        if ((varBind[1][0] == 0x00)
            && (varBind[1][1] == 0x00)
            && (varBind[1][2] == 0x00)
            && (varBind[1][3] == 0x00)
            && (varBind[1][4] == 0x00)
            && (varBind[1][5] == 0x00))
          /* Ignore NULL addresses returned by other network
             interfaces */
          printf("Interface #%i is a NULL addressn", j);
        sprintf(TempEthernet, "%02x%02x%02x%02x%02x%02x",
        printf("MAC Address of interface #%i: %sn", j,
  } while (!ret);         /* Stop only on an error.  An error will occur
                             when we go exhaust the list of interfaces to
                             be examined */

  /* Free the bindings */

Back to top

Server Response from: ETNASC01