Home > Windows API > How to retrieve network parameters?

How to retrieve network parameters?

IpHlpApi provides a pretty useful function called GetNetworkParams. Helps in retrieving hostname, domain name, and dns server info.

This is how we use this API. Code snippet taken from MSDN, modified a bit…

#include "iphlpapi.h"
#pragma comment( lib, "iphlpapi.lib" ) 

void PrintNetworkParams()
{
   FIXED_INFO*       pFixedInfo  = 0;
   ULONG             ulOutBufLen = 0;
   DWORD             dwRetVal    = 0;
   IP_ADDR_STRING*   pIPAddr     = 0;

   if( ERROR_BUFFER_OVERFLOW == GetNetworkParams( pFixedInfo, &ulOutBufLen ) )
   {
      pFixedInfo = new FIXED_INFO[ulOutBufLen];
   }
   else
   {
      ASSERT( FALSE );
   }

   if( dwRetVal = GetNetworkParams( pFixedInfo, &ulOutBufLen ))
   {
      printf( "Call to GetNetworkParams failed. Return Value: %08x\n", dwRetVal );
   }
   else
   {
      printf( "Host Name: %s\n", pFixedInfo -> HostName );
      printf( "Domain Name: %s\n", pFixedInfo -> DomainName );

      printf( "DNS Servers:\n" );
      printf( "\t%s\n", pFixedInfo -> DnsServerList.IpAddress.String );

      pIPAddr = pFixedInfo ->DnsServerList.Next;
      while ( pIPAddr )
      {
         printf( "\t%s\n", pIPAddr ->IpAddress.String );
         pIPAddr = pIPAddr ->Next;
      }// End while
   }// End if 

   if( pFixedInfo )
   {
      delete [] pFixedInfo;
   }
}// End PrintNetworkParams
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: