< GetAdaptersInfo & GetAdaptersAddresses Examples | IP Helper Functions Main | Interface Table & SetIpForwardEntry Examples >
What do we have in this chapter 13 part 10?
|
Program Example Using GetInterfaceInfo() Function
The GetInterfaceInfo() function obtains the list of the network interface adapters with IPv4 enabled on the local system. Create a new empty Win32 console mode application and add the project/solution name.
Add the following code. |
// Description:
// The following example retrieves the list of network adapters
// with IPv4 enabled on the local system and prints various
// properties of the first network adapter
//
// Link to ws2_32.lib
#include <winsock2.h>
#include <ws2ipdef.h>
// Link to Iphlpapi.lib
#include <iphlpapi.h>
#include <stdio.h>
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
/* Note: could also use malloc() and free() */
int main(int argc, char **argv)
{
// Declare and initialize variables
PIP_INTERFACE_INFO pInfo;
ULONG ulOutBufLen = 0;
DWORD dwRetVal = 0;
int iReturn = 1, i;
// Make an initial call to GetInterfaceInfo to get
// the necessary size in the ulOutBufLen variable
dwRetVal = GetInterfaceInfo(NULL, &ulOutBufLen);
if (dwRetVal == ERROR_INSUFFICIENT_BUFFER)
{
printf("Not enough buffer! Re-allocating...\n");
pInfo = (IP_INTERFACE_INFO *) MALLOC(ulOutBufLen);
if (pInfo == NULL)
{
printf("Unable to allocate memory needed to call GetInterfaceInfo()!\n");
return 1;
}
else
printf("Memory needed to call GetInterfaceInfo() has been allocated!\n");
}
else
printf("GetInterfaceInfo() should be fine!\n");
// Make a second call to GetInterfaceInfo to get
// the actual data we need
dwRetVal = GetInterfaceInfo(pInfo, &ulOutBufLen);
if (dwRetVal == NO_ERROR)
{
printf("GetInterfaceInfo() is OK!\n");
printf("Number of Adapters: %ld\n\n", pInfo->NumAdapters);
for (i = 0; i < (int) pInfo->NumAdapters; i++)
{
printf("Adapter Index[%d]: %ld\n", i, pInfo->Adapter[i].Index);
printf("Adapter Name[%d]: %ws\n\n", i, pInfo->Adapter[i].Name);
}
iReturn = 0;
}
else if (dwRetVal == ERROR_NO_DATA)
{
printf("There are no network adapters with IPv4 enabled on the local system!\n");
iReturn = 0;
}
else
{
printf("GetInterfaceInfo() failed with error code %d\n", dwRetVal);
iReturn = 1;
}
FREE(pInfo);
return (iReturn);
}
Build and run the project.
The GetIfEntry() function retrieves information for the specified interface on the local computer. The GetIfTable() function retrieves the MIB-II interface table. Create a new empty Win32 console mode application and add the project/solution name.
Add the following source code.
// Description:
// The following example retrieves the entries from the interface
// table and prints some of the information available for that entry
//
// Link to ws2_32.lib
#include <winsock2.h>
#include <ws2tcpip.h>
// Link to Iphlpapi.lib
#include <iphlpapi.h>
#include <stdio.h>
#include <stdlib.h>
// Note: could also use malloc() and free()
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
int main()
{
// Declare and initialize variables
DWORD dwSize = 0;
DWORD dwRetVal = 0;
int i, j;
// variables used for GetIfTable and GetIfEntry
MIB_IFTABLE *pIfTable;
MIB_IFROW *pIfRow;
// Allocate memory for our pointers.
pIfTable = (MIB_IFTABLE *) MALLOC(sizeof (MIB_IFTABLE));
if (pIfTable == NULL)
{
printf("Error allocating memory needed to call GetIfTable()!\n");
exit (1);
}
else
printf("Memory needed to call GetIfTable() has been allocated!\n");
// Before calling GetIfEntry, we call GetIfTable to make
// sure there are entries to get and retrieve the interface index.
// Make an initial call to GetIfTable to get the necessary size into dwSize
dwSize = sizeof (MIB_IFTABLE);
if (GetIfTable(pIfTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER)
{
printf("Not enough memory! Re-allocating...\n");
FREE(pIfTable);
pIfTable = (MIB_IFTABLE *) MALLOC(dwSize);
if (pIfTable == NULL)
{
printf("Error allocating memory!\n");
exit (1);
}
else
printf("(Dummy) Memory allocated successfully!\n");
}
else
printf("GetIfTable() should be fine!\n");
// Make a second call to GetIfTable to get the actual data we want.
if ((dwRetVal = GetIfTable(pIfTable, &dwSize, 0)) == NO_ERROR)
{
if (pIfTable->dwNumEntries > 0)
{
pIfRow = (MIB_IFROW *) MALLOC(sizeof (MIB_IFROW));
if (pIfRow == NULL)
{
printf("Error allocating memory!\n");
if (pIfTable != NULL)
{
FREE(pIfTable);
pIfTable = NULL;
}
exit (1);
}
else
printf("Memory allocated successfully for 2nd call!\n");
printf("\n\tNumber of entries: %ld\n\n", pIfTable->dwNumEntries);
for (i = 0; i < (int) pIfTable->dwNumEntries; i++)
{
pIfRow->dwIndex = pIfTable->table[i].dwIndex;
if ((dwRetVal = GetIfEntry(pIfRow)) == NO_ERROR)
{
printf("\tIndex:\t %ld\n", pIfRow->dwIndex);
printf("\tInterfaceName[%d]:\t ", i);
if (pIfRow->wszName != NULL)
printf("%ws", pIfRow->wszName);
printf("\n");
printf("\tDescription[%d]:\t ", i);
for (j = 0; j < (int) pIfRow->dwDescrLen; j++)
printf("%c", pIfRow->bDescr[j]);
printf("\n");
printf("\tType[%d]:\t ", i);
switch (pIfRow->dwType)
{
case IF_TYPE_OTHER:
printf("Other\n");
break;
case IF_TYPE_ETHERNET_CSMACD:
printf("Ethernet\n");
break;
case IF_TYPE_ISO88025_TOKENRING:
printf("Token Ring\n");
break;
case IF_TYPE_PPP:
printf("PPP\n");
break;
case IF_TYPE_SOFTWARE_LOOPBACK:
printf("Software Lookback\n");
break;
case IF_TYPE_ATM:
printf("ATM\n");
break;
case IF_TYPE_IEEE80211:
printf("IEEE 802.11 Wireless\n");
break;
case IF_TYPE_TUNNEL:
printf("Tunnel type encapsulation\n");
break;
case IF_TYPE_IEEE1394:
printf("IEEE 1394 Firewire\n");
break;
default:
printf("Unknown type %ld\n", pIfRow->dwType);
break;
}
printf("\tMtu[%d]:\t\t %ld\n", i, pIfRow->dwMtu);
printf("\tSpeed[%d]:\t %ld\n", i, pIfRow->dwSpeed);
printf("\tPhysical Addr:\t ");
if (pIfRow->dwPhysAddrLen == 0)
printf("\n");
for (j = 0; j < (int) pIfRow->dwPhysAddrLen; j++)
{
if (j == (pIfRow->dwPhysAddrLen - 1))
printf("%.2X\n", (int) pIfRow->bPhysAddr[j]);
else
printf("%.2X-", (int) pIfRow->bPhysAddr[j]);
}
printf("\tAdmin Status[%d]:\t %ld\n", i, pIfRow->dwAdminStatus);
printf("\tOper Status[%d]:\t ", i);
switch (pIfRow->dwOperStatus)
{
case IF_OPER_STATUS_NON_OPERATIONAL:
printf("Non Operational\n");
break;
case IF_OPER_STATUS_UNREACHABLE:
printf("Unreasonable\n");
break;
case IF_OPER_STATUS_DISCONNECTED:
printf("Disconnected\n");
break;
case IF_OPER_STATUS_CONNECTING:
printf("Connecting\n");
break;
case IF_OPER_STATUS_CONNECTED:
printf("Connected\n");
break;
case IF_OPER_STATUS_OPERATIONAL:
printf("Operational\n");
break;
default:
printf("Unknown status %ld\n", pIfRow->dwAdminStatus);
break;
}
printf("\n");
}
else {
printf("GetIfEntry failed for index %d with error: %ld\n", i, dwRetVal);
// Here you can use FormatMessage to find out why it failed
}
}
} else {
printf("\tGetIfTable failed with error: %ld\n", dwRetVal);
}
}
exit (0);
}
Build and run the project.
-----------------------------------------------
< GetAdaptersInfo & GetAdaptersAddresses Examples | IP Helper Functions Main | Interface Table & SetIpForwardEntry Examples >