A New Method to Get MAC/NIC Statistical Information

Environment: Compiled on Visual Studio 6 & Windows XP Pro

Introduction:

In Khalid Shaikh's article, "Three ways to get your MAC address" (2002/04/15), he described three ways to retrieve a MAC address.

The first method is to use the relation between UUID and MAC to retrieve MAC. However, due to security reasons, UUID no longer contains an explicit MAC string. Thus, this method will not work anymore.

The second method is to use NetBIOS, of which Khalid Shaikh has described clearly the pros and cons.

The third method is to use the Windows SDK, which is very easy to use. Also along with MAC information, other configuration information can be retrieved in the same function call.

Due to project requirement, I need to find out a easy/neat way to not only get the configuration information of an interface, but also the dynamic statistic information: how many packages received/sent, how many packages dropped, and so forth. The methods described there cannot provide such information.

After doing some research, I found a good way to collect this information; the by-product is the MAC address.

Code

The code segment is quite straightforward. I only demonstrate how to retrieve MAC here. Check the reference for details of the MIB_IFROW structure if you need to collect the statistic information of NIC.

bool GetMacAddr()
{
  // Retrieve network interface information
  //

  DWORD sizeReq = 0 ;
  PMIB_IFTABLE pInfo = NULL;
  // Get size information
  ::GetIfTable(NULL, &sizeReq, FALSE) ;

  // Allocate required memory
  pInfo = (PMIB_IFTABLE) new BYTE [sizeReq] ;
  memset (pInfo, 0, sizeReq) ;

  DWORD sizeToUse = sizeReq ;

  // Retrieve network interface information
  bool result = ( ::GetIfTable( (PMIB_IFTABLE)pInfo,
       sizeToUse, FALSE) == NO_ERROR );
  if( !result )
  {
    delete (PMIB_IFTABLE)pInfo;
    pInfo = NULL;
    printf(
      "Couldn't retrieve network interface information!");
    return false;
  }

  // Print all interface information
  for( unsigned int index = 0; index <
    ((PMIB_IFTABLE)pInfo)->dwNumEntries; index ++ )
  {
    // Get interface description
    MIB_IFROW& details = ((PMIB_IFTABLE)pInfo)->table[index];
    printf("Interface %d: %s\n", index, (LPCTSTR) details.bDescr );

    // Is this an Ethernet interface?
    if( (details.dwPhysAddrLen == 6)
      && (details.dwType == MIB_IF_TYPE_ETHERNET) )
    {

      // Ethernet MAC address
      printf("     Ethernet: ");
    }
    else    // Other interface
    {
      printf("     Others: ");
    }

    // Format physical address
    char macStr[30];
    for (DWORD j = 0 ; j < details.dwPhysAddrLen ; ++j)
    {
      sprintf( &macStr[j*3], "%02X-", details.bPhysAddr[j] );
    }
    macStr[j*3-1] = '\0';

    // Print out physical address
    printf( "%s\n", &macStr[0] );
  }

  return true;
}

If you have any questions, don't hesitate to e-mail me.

Downloads

Download demo project and source (GetMAC) - 4 Kb
Note: This method uses Windows SDK, Thus, it requires that the Windows SDK be installed in order for it to be compiled.


Comments

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: October 23, 2014 Despite the current "virtualize everything" mentality, there are advantages to utilizing physical hardware for certain tasks. This is especially true for backups. In many cases, it is clearly in an organization's best interest to make use of physical, purpose-built backup appliances rather than relying on virtual backup software (VBA - Virtual Backup Appliances). Join us for this webcast to learn why physical appliances are preferable to virtual backup appliances, …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds