Getting interface information

In this page, a way to obtain network interface information using IPHLPAPI is shown.

Sample code

List of network interfaces can be obtained using GetInterfaceInfo(). The following sample code shows how to use GetInterfaceInfo().


#include <stdio.h>

#include <winsock2.h>
#include <iphlpapi.h>

int
main()
{
 int i;
 PIP_INTERFACE_INFO pInfo = NULL;
 ULONG ulOutBufLen = 0;
 DWORD dwRetVal = 0;

 /* Get ulOutBufLen size */
 if (GetInterfaceInfo(NULL, &ulOutBufLen)
       == ERROR_INSUFFICIENT_BUFFER) {
   pInfo = (IP_INTERFACE_INFO *) malloc (ulOutBufLen);
 }

 /* Get actual data. */
 dwRetVal = GetInterfaceInfo(pInfo, &ulOutBufLen);

 if (dwRetVal == NO_ERROR ) {
   printf("Number of Adapters: %ld\n\n", pInfo->NumAdapters);

   for (i=0; i<pInfo->NumAdapters; i++) {
     printf("Adapter Name: %ws\n", pInfo->Adapter[i].Name);
     printf("Adapter Index: %ld\n", pInfo->Adapter[i].Index);
     printf("\n");
   }
 } else {
   printf("GetInterfaceInfo failed.\n");
   LPVOID lpMsgBuf;
		
   if (FormatMessage(
           FORMAT_MESSAGE_ALLOCATE_BUFFER | 
           FORMAT_MESSAGE_FROM_SYSTEM | 
           FORMAT_MESSAGE_IGNORE_INSERTS,
           NULL,
           dwRetVal,
           MAKELANGID(LANG_NEUTRAL,
                      SUBLANG_DEFAULT), /* Default language */
           (LPTSTR) &lpMsgBuf,
           0,
           NULL ))  {
     printf("Error: %s", lpMsgBuf);
   }

   LocalFree( lpMsgBuf );
 }

 return 0;
}

Sample code output

The sample will output an message like the following.


C:> a.exe
Number of Adapters: 2

Adapter Name: \DEVICE\TCPIP_{22EXXC3D-F01D-4231-AE59-C23EXXXX6C66}
Adapter Index: 2

Adapter Name: \DEVICE\TCPIP_{4E7XX61B-836C-49AB-8B02-775BXXXX7352}
Adapter Index: 3

Structure used by GetInterfaceInfo()

The following structure is IP_INTERFACE_INFO that is used by GetInterfaceInfo().


typedef struct _IP_ADAPTER_INDEX_MAP {
  ULONG Index;
  WCHAR Name[MAX_ADAPTER_NAME];
} IP_ADAPTER_INDEX_MAP, *PIP_ADAPTER_INDEX_MAP;

typedef struct _IP_INTERFACE_INFO {
  LONG NumAdapters;
  IP_ADAPTER_INDEX_MAP Adapter[1];
} IP_INTERFACE_INFO, *PIP_INTERFACE_INFO;

Getting list of network interfaces (2)

The value obtained as "Adapter Name" is different from the value that is shown in the "ipconfig" command. The "Adapter Name" string is not human friendly. The following sample shows how to get a human friendly adapter name information.

Sample code

The interface description can be obtained using GetIfEntry(). The following sample shows how to use GetIfEntry().


#include <stdio.h>

#include <winsock2.h>
#include <iphlpapi.h>

int
main()
{
 DWORD i;
 PMIB_IFTABLE ifTable;
 MIB_IFROW MibIfRow;
 DWORD dwSize = 0;
 DWORD dwRetVal = 0;

 /*
  * GetIfTable() is used before using GetIfEntry().
  * This is to obtain number of network interfaces.
  */

 /* Get size required for GetIfTable() */
 if (GetIfTable(NULL, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) {
   ifTable = (MIB_IFTABLE *) malloc (dwSize);
 }

 /* Get actual data using GetIfTable() */
 if ((dwRetVal = GetIfTable(ifTable, &dwSize, 0)) == NO_ERROR) {
   if (ifTable->dwNumEntries > 0) {
     printf("Number of Adapters: %ld\n\n", ifTable->dwNumEntries);
     for (i=1; i<=ifTable->dwNumEntries; i++) {
       MibIfRow.dwIndex = i;
       if ((dwRetVal = GetIfEntry(&MibIfRow)) == NO_ERROR) {
         printf("Description: %s\n", MibIfRow.bDescr);
       }
     }
   }
 } else {
   printf("GetInterfaceInfo failed.\n");
   LPVOID lpMsgBuf;
		
   if (FormatMessage( 
           FORMAT_MESSAGE_ALLOCATE_BUFFER | 
           FORMAT_MESSAGE_FROM_SYSTEM | 
           FORMAT_MESSAGE_IGNORE_INSERTS,
           NULL,
           dwRetVal,
           MAKELANGID(LANG_NEUTRAL,
                      SUBLANG_DEFAULT), // Default language
           (LPTSTR) &lpMsgBuf,
           0,
           NULL ))  {
     printf("Error: %s", lpMsgBuf);
   }

   LocalFree( lpMsgBuf );
 }

 return 0;
}

Sample code output

The sample will output an message like the following.


C:> a.exe
Number of Adapters: 3

Description: MS TCP Loopback Interface
Description: Intel (R) PRO/100 VE Network Connection
Description: 3Com Fast Ethernet Network Connection

Structure used by GetIfTable()

The following structure are MIB_IFTABLE and MIB_IFROW that is used by GetIfTable().


typedef struct _MIB_IFROW {
  WCHAR wszName[MAX_INTERFACE_NAME_LEN];
  DWORD dwIndex;
  DWORD dwType;
  DWORD dwMtu;
  DWORD dwSpeed;
  DWORD dwPhysAddrLen;
  BYTE bPhysAddr[MAXLEN_PHYSADDR];
  DWORD dwAdminStatus;
  DWORD dwOperStatus;
  DWORD dwLastChange;
  DWORD dwInOctets;
  DWORD dwInUcastPkts;
  DWORD dwInNUcastPkts;
  DWORD dwInDiscards;
  DWORD dwInErrors;
  DWORD dwInUnknownProtos;
  DWORD dwOutOctets;
  DWORD dwOutUcastPkts;
  DWORD dwOutNUcastPkts;
  DWORD dwOutDiscards;
  DWORD dwOutErrors;
  DWORD dwOutQLen;
  DWORD dwDescrLen;
  BYTE bDescr[MAXLEN_IFDESCR];
} MIB_IFROW, *PMIB_IFROW;

typedef struct _MIB_IFTABLE {
  DWORD dwNumEntries;
  MIB_IFROW table[ANY_SIZE];
} MIB_IFTABLE, *PMIB_IFTABLE;

  

Copyright (C) GeekPage.JP. All rights reserved.