How To Get the MAC Address for an Ethernet Adapter

Article translations Article translations
Article ID: 118623 - View products that this article applies to.
This article was previously published under Q118623
Expand all | Collapse all

On This Page

SUMMARY

This article demonstrates how to get the Media Access Control (MAC) address for an ethernet adapter programmatically by using NetBIOS, if your card is bound to NetBIOS.

MORE INFORMATION

To get the Media Access Control (MAC) address for an ethernet adapter programmatically, use the Netbios() NCBASTAT command and provide a "*" as the name in the NCB.ncb_CallName field. This is demonstrated in the sample code below.

For computers with multiple network adapters you need to enumerate the LANA numbers and perform the NCBASTAT command on each. Even when you have a single network adapter, it is a good idea to enumerate valid LANA numbers first and perform the NCBASTAT on one of the valid LANA numbers. It is considered bad programming to hardcode the LANA number to 0.

In addition, other hardware and software may be assigned their own MAC addresses. For example, a modem can have a MAC address. Also, a RAS client or server can install "dummy" network adapters that correspond to a dialup or serial connection. Normally, these MAC addresses are randomly generated. If an adapter status is called on a LANA that corresponds to one of these adapters when no connection is present, Netbios returns error 0x34 (NRC_ENVNOTDEF) even if a reset was previously performed.

With the NetBEUI and IPX transports, the same information can be obtained at a command prompt by using:
   net config workstation
				
The ID given is the MAC address.

The following code enumerates all LANA numbers, performs a reset (NCBREST), and an adapter status (NCBASTAT).

Sample Code

   #include <windows.h>
   #include <wincon.h>
   #include <stdlib.h>
   #include <stdio.h>
   #include <time.h>

   typedef struct _ASTAT_
   {

      ADAPTER_STATUS adapt;
      NAME_BUFFER    NameBuff [30];

   }ASTAT, * PASTAT;

   ASTAT Adapter;

   void main (void)
   {
      NCB Ncb;
      UCHAR uRetCode;
      char NetName[50];
      LANA_ENUM   lenum;
      int      i;

      memset( &Ncb, 0, sizeof(Ncb) );
      Ncb.ncb_command = NCBENUM;
      Ncb.ncb_buffer = (UCHAR *)&lenum;
      Ncb.ncb_length = sizeof(lenum);
      uRetCode = Netbios( &Ncb );
      printf( "The NCBENUM return code is: 0x%x \n", uRetCode );

      for(i=0; i < lenum.length ;i++)
      {
          memset( &Ncb, 0, sizeof(Ncb) );
          Ncb.ncb_command = NCBRESET;
          Ncb.ncb_lana_num = lenum.lana[i];

          uRetCode = Netbios( &Ncb );
          printf( "The NCBRESET on LANA %d return code is: 0x%x \n",
                  lenum.lana[i], uRetCode );

          memset( &Ncb, 0, sizeof (Ncb) );
          Ncb.ncb_command = NCBASTAT;
          Ncb.ncb_lana_num = lenum.lana[i];

          strcpy( Ncb.ncb_callname,  "*               " );
          Ncb.ncb_buffer = (char *) &Adapter;
          Ncb.ncb_length = sizeof(Adapter);

          uRetCode = Netbios( &Ncb );
          printf( "The NCBASTAT on LANA %d return code is: 0x%x \n",
                  lenum.lana[i], uRetCode );
          if ( uRetCode == 0 )
          {
             printf( "The Ethernet Number on LANA %d is:
                     %02x%02x%02x%02x%02x%02x\n",
            lenum.lana[i],
                  Adapter.adapt.adapter_address[0],
                  Adapter.adapt.adapter_address[1],
                  Adapter.adapt.adapter_address[2],
                  Adapter.adapt.adapter_address[3],
                  Adapter.adapt.adapter_address[4],
                  Adapter.adapt.adapter_address[5] );
          }
       }

   }
				

Properties

Article ID: 118623 - Last Review: July 8, 2005 - Revision: 2.5
APPLIES TO
  • Microsoft Platform Software Development Kit-January 2000 Edition, when used with:
    • Microsoft Windows NT Advanced Server 3.1
    • Microsoft Windows NT Server 3.5
    • Microsoft Windows NT Server 3.51
    • Microsoft Windows NT Server 4.0 Standard Edition
    • Microsoft Windows NT Workstation 3.1
    • Microsoft Windows NT Workstation 3.5
    • Microsoft Windows NT Workstation 3.51
    • Microsoft Windows NT Workstation 4.0 Developer Edition
    • the operating system: Microsoft Windows 2000
    • Microsoft Windows 98 Standard Edition
    • Microsoft Windows 95
    • Microsoft Windows 98 Standard Edition
Keywords: 
kbapi kbhowto kbnetbios kbnetwork KB118623

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com