< GetIpAddrTable & AddIPAddress Examples | IP Helper Functions Main | GetInterfaceInfo, GetIfEntry & GetIfTable Examples >
What do we have in this chapter 13 part 9?
Program Example Using GetAdaptersInfo() Function
Program Example Using GetAdaptersAddresses() Function
|
Program Example Using GetAdaptersInfo() Function
The GetAdaptersInfo() function retrieves adapter information for the local computer. On Windows XP and later: Use the GetAdaptersAddresses() function instead of GetAdaptersInfo().This example retrieves the adapter information and prints various properties of each adapter. Create a new empty Win32 console mode application and add the project/solution name.
Add the source code. |
// Description: This example retrieves the adapter information and prints various properties of each adapter
//
// Link to ws2_32.lib
#include <winsock2.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(int argc, char **argv)
{
// Declare and initialize variables
//
// It is possible for an adapter to have multiple
// IPv4 addresses, gateways, and secondary WINS servers assigned to the adapter
//
// Note that this sample code only prints out the
// first entry for the IP address/mask, and gateway, and
// the primary and secondary WINS server for each adapter
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = 0;
UINT i;
// variables used to print DHCP time info
struct tm newtime;
char buffer[32];
errno_t error;
ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(sizeof (IP_ADAPTER_INFO));
if (pAdapterInfo == NULL)
{
printf("Error allocating memory needed to call GetAdaptersinfo()\n");
return 1;
}
else
printf("Memory allocation for GetAdaptersinfo() call is OK!\n");
// Make an initial call to GetAdaptersInfo to get
// the necessary size into the ulOutBufLen variable
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW)
{
printf("Not enough buffer! Re-allocating...\n");
FREE(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(ulOutBufLen);
if (pAdapterInfo == NULL)
{
printf("Error allocating memory needed to call GetAdaptersinfo()\n");
return 1;
}
else
printf("Memory allocation for GetAdaptersinfo() 2nd call is OK!\n");
}
else
printf("Buffer for GetAdaptersInfo() is OK!\n");
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR)
{
pAdapter = pAdapterInfo;
while (pAdapter)
{
printf("\tComboIndex: \t%d\n", pAdapter->ComboIndex);
printf("\tAdapter Name: \t%s\n", pAdapter->AdapterName);
printf("\tAdapter Desc: \t%s\n", pAdapter->Description);
printf("\tAdapter Addr: \t");
for (i = 0; i < pAdapter->AddressLength; i++)
{
if (i == (pAdapter->AddressLength - 1))
printf("%.2X\n", (int) pAdapter->Address[i]);
else
printf("%.2X-", (int) pAdapter->Address[i]);
}
printf("\tIndex: \t%d\n", pAdapter->Index);
printf("\tType: \t");
switch (pAdapter->Type)
{
case MIB_IF_TYPE_OTHER:
printf("Other\n");
break;
case MIB_IF_TYPE_ETHERNET:
printf("Ethernet\n");
break;
case MIB_IF_TYPE_TOKENRING:
printf("Token Ring\n");
break;
case MIB_IF_TYPE_FDDI:
printf("FDDI\n");
break;
case MIB_IF_TYPE_PPP:
printf("PPP\n");
break;
case MIB_IF_TYPE_LOOPBACK:
printf("Lookback\n");
break;
case MIB_IF_TYPE_SLIP:
printf("Slip\n");
break;
default:
printf("Unknown type %ld\n", pAdapter->Type);
break;
}
printf("\tIP Address: \t%s\n", pAdapter->IpAddressList.IpAddress.String);
printf("\tIP Mask: \t%s\n", pAdapter->IpAddressList.IpMask.String);
printf("\tGateway: \t%s\n", pAdapter->GatewayList.IpAddress.String);
printf("\t***\n");
if (pAdapter->DhcpEnabled)
{
printf("\tDHCP Enabled: Yes\n");
printf("\t DHCP Server: \t%s\n", pAdapter->DhcpServer.IpAddress.String);
printf("\t Lease Obtained: ");
// Display local time
error = _localtime32_s(&newtime, (__time32_t *)&pAdapter->LeaseObtained);
if (error)
printf("Invalid Argument to _localtime32_s\n");
else
{
// Convert to an ASCII representation
error = asctime_s(buffer, 32, &newtime);
if (error)
printf("Invalid Argument to asctime_s\n");
else
// asctime_s returns the string terminated by \n\0
printf("%s", buffer);
}
printf("\t Lease Expires: ");
error = _localtime32_s(&newtime, (__time32_t *)&pAdapter->LeaseExpires);
if (error)
printf("Invalid Argument to _localtime32_s\n");
else {
// Convert to an ASCII representation
error = asctime_s(buffer, 32, &newtime);
if (error)
printf("Invalid Argument to asctime_s\n");
else
// asctime_s returns the string terminated by \n\0
printf("%s", buffer);
}
}
else
printf("\tDHCP Enabled: No\n");
if (pAdapter->HaveWins)
{
printf("\tHave Wins: Yes\n");
printf("\t Primary Wins Server: %s\n", pAdapter->PrimaryWinsServer.IpAddress.String);
printf("\t Secondary Wins Server: %s\n", pAdapter->SecondaryWinsServer.IpAddress.String);
}
else
printf("\tHave Wins: No\n");
pAdapter = pAdapter->Next;
printf("\n");
}
}
else{
printf("GetAdaptersInfo failed with error: %d\n", dwRetVal);
}
if (pAdapterInfo)
FREE(pAdapterInfo);
return 0;
}
Build and run the project.
The GetAdaptersAddresses() function retrieves the addresses associated with the adapters on the local computer. Create a new empty Win32 console mode application and add the project/solution name.
Add the following source code.
// Description:
// This example retrieves the IP_ADAPTER_ADDRESSES structure
// for the adapters associated with the system and prints some
// members for each adapter interface.
//
// Link to ws2_32.lib
#include <winsock2.h>
// Link to Iphlpapi.lib
#include <iphlpapi.h>
#include <stdio.h>
// Note: could also use malloc() and free()
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
int main(int argc, char **argv)
{
// Declare and initialize variables
DWORD dwSize = 0;
DWORD dwRetVal = 0;
int i = 0;
ULONG flags, outBufLen = 0, family;
LPVOID lpMsgBuf;
PIP_ADAPTER_ADDRESSES pAddresses;
PIP_ADAPTER_ADDRESSES pCurrAddresses;
PIP_ADAPTER_UNICAST_ADDRESS pUnicast;
PIP_ADAPTER_ANYCAST_ADDRESS pAnycast;
PIP_ADAPTER_MULTICAST_ADDRESS pMulticast;
IP_ADAPTER_DNS_SERVER_ADDRESS *pDnServer = NULL;
IP_ADAPTER_PREFIX *pPrefix = NULL;
// Set the flags to pass to GetAdaptersAddresses
flags = GAA_FLAG_INCLUDE_PREFIX;
// default to unspecified address family (both)
family = AF_UNSPEC;
lpMsgBuf = NULL;
pAddresses = NULL;
pCurrAddresses = NULL;
pUnicast = NULL;
pAnycast = NULL;
pMulticast = NULL;
if (argc != 2)
{
printf(" Usage: GetAdaptersAddresses family\n");
printf(" GetAdaptersAddresses 4 (for IPv4)\n");
printf(" GetAdaptersAddresses 6 (for IPv6)\n");
printf(" GetAdaptersAddresses A (for both IPv4 and IPv6)\n");
exit(1);
}
if (atoi(argv[1]) == 4)
family = AF_INET;
else if (atoi(argv[1]) == 6)
family = AF_INET6;
outBufLen = sizeof (IP_ADAPTER_ADDRESSES);
pAddresses = (IP_ADAPTER_ADDRESSES *) MALLOC(outBufLen);
if (pAddresses == NULL)
{
printf("Memory allocation failed for IP_ADAPTER_ADDRESSES struct!\n");
exit(1);
}
else
printf("Memory allocation for IP_ADAPTER_ADDRESSES struct is OK!\n");
// Make an initial call to GetAdaptersAddresses to get the
// size needed into the outBufLen variable
if (GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen) == ERROR_BUFFER_OVERFLOW)
{
printf("Not enough buffer! Re-allocating...\n");
FREE(pAddresses);
pAddresses = (IP_ADAPTER_ADDRESSES *) MALLOC(outBufLen);
}
else
printf("Buffer allocation is OK!\n");
if (pAddresses == NULL)
{
printf("Memory allocation failed for IP_ADAPTER_ADDRESSES struct!\n");
exit(1);
}
else
printf("Memory allocation for IP_ADAPTER_ADDRESSES struct is OK!\n");
// Make a second call to GetAdapters Addresses to get the actual data we want
printf("Memory allocated for GetAdapterAddresses = %d bytes\n", outBufLen);
printf("Calling GetAdaptersAddresses function with family = ");
if (family == AF_INET)
printf("AF_INET\n");
if (family == AF_INET6)
printf("AF_INET6\n");
if (family == AF_UNSPEC)
printf("AF_UNSPEC\n\n");
dwRetVal = GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen);
if (dwRetVal == NO_ERROR) {
// If successful, output some information from the data we received
pCurrAddresses = pAddresses;
while (pCurrAddresses)
{
printf("\tLength of the IP_ADAPTER_ADDRESS struct: %ld\n", pCurrAddresses->Length);
printf("\tIfIndex (IPv4 interface): %u\n", pCurrAddresses->IfIndex);
printf("\tAdapter name: %s\n", pCurrAddresses->AdapterName);
pUnicast = pCurrAddresses->FirstUnicastAddress;
if (pUnicast != NULL)
{
for (i = 0; pUnicast != NULL; i++)
pUnicast = pUnicast->Next;
printf("\tNumber of Unicast Addresses: %d\n", i);
}
else
printf("\tNo Unicast Addresses\n");
pAnycast = pCurrAddresses->FirstAnycastAddress;
if (pAnycast)
{
for (i = 0; pAnycast != NULL; i++)
pAnycast = pAnycast->Next;
printf("\tNumber of Anycast Addresses: %d\n", i);
}
else
printf("\tNo Anycast Addresses\n");
pMulticast = pCurrAddresses->FirstMulticastAddress;
if (pMulticast)
{
for (i = 0; pMulticast != NULL; i++)
pMulticast = pMulticast->Next;
printf("\tNumber of Multicast Addresses: %d\n", i);
}
else
printf("\tNo Multicast Addresses\n");
pDnServer = pCurrAddresses->FirstDnsServerAddress;
if (pDnServer)
{
for (i = 0; pDnServer != NULL; i++)
pDnServer = pDnServer->Next;
printf("\tNumber of DNS Server Addresses: %d\n", i);
}
else
printf("\tNo DNS Server Addresses\n");
printf("\tDNS Suffix: %wS\n", pCurrAddresses->DnsSuffix);
printf("\tDescription: %wS\n", pCurrAddresses->Description);
printf("\tFriendly name: %wS\n", pCurrAddresses->FriendlyName);
if (pCurrAddresses->PhysicalAddressLength != 0)
{
printf("\tPhysical address: ");
for (i = 0; i < (int) pCurrAddresses->PhysicalAddressLength; i++)
{
if (i == (pCurrAddresses->PhysicalAddressLength - 1))
printf("%.2X\n", (int) pCurrAddresses->PhysicalAddress[i]);
else
printf("%.2X-", (int) pCurrAddresses->PhysicalAddress[i]);
}
}
printf("\tFlags: %ld\n", pCurrAddresses->Flags);
printf("\tMtu: %lu\n", pCurrAddresses->Mtu);
printf("\tIfType: %ld\n", pCurrAddresses->IfType);
printf("\tOperStatus: %ld\n", pCurrAddresses->OperStatus);
printf("\tIpv6IfIndex (IPv6 interface): %u\n", pCurrAddresses->Ipv6IfIndex);
printf("\tZoneIndices (hex): ");
for (i = 0; i < 16; i++)
printf("%lx ", pCurrAddresses->ZoneIndices[i]);
printf("\n");
pPrefix = pCurrAddresses->FirstPrefix;
if (pPrefix)
{
for (i = 0; pPrefix != NULL; i++)
pPrefix = pPrefix->Next;
printf("\tNumber of IP Adapter Prefix entries: %d\n", i);
} else
printf("\tNumber of IP Adapter Prefix entries: 0\n");
printf("\n");
pCurrAddresses = pCurrAddresses->Next;
}
}
else
{
printf("Call to GetAdaptersAddresses failed with error: %d\n", dwRetVal);
if (dwRetVal == ERROR_NO_DATA)
printf("\tNo addresses were found for the requested parameters\n");
else
{
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("\tError: %s", lpMsgBuf);
LocalFree(lpMsgBuf);
FREE(pAddresses);
exit(1);
}
}
}
FREE(pAddresses);
return 0;
}
Build and run the project.
-----------------------------------------------------
< GetIpAddrTable & AddIPAddress Examples | IP Helper Functions Main | GetInterfaceInfo, GetIfEntry & GetIfTable Examples >