Getting DNS information

GetNetworkParams() lets you get the local hostname and the DNS that is used by the currrent computer. In this page, the usage of GetNetworkParams() is shown.

Sample code

The following sample code shows how to use GetNetworkParams().


#include <stdio.h>

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

int
main()
{
 FIXED_INFO *FixedInfo = NULL;
 ULONG    ulOutBufLen = 0;
 DWORD    dwRetVal;
 IP_ADDR_STRING *pIPAddr;
   
 if(GetNetworkParams(NULL, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
   FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, ulOutBufLen);
 } else {
   return 1;
 }

 if (dwRetVal = GetNetworkParams(FixedInfo, &ulOutBufLen)) {
   printf("GetNetworkParams() failed: %08x\n", dwRetVal);
   return 1;
 } else {
   printf("Host Name: %s\n", FixedInfo->HostName);
   printf("Domain Name: %s\n", FixedInfo->DomainName);

   printf("DNS Servers:\n" );
   printf("\t%s\n", FixedInfo->DnsServerList.IpAddress.String);
      
   pIPAddr = FixedInfo->DnsServerList.Next;
   while (pIPAddr ) {
     printf("\t%s\n", pIPAddr->IpAddress.String);
     pIPAddr = pIPAddr->Next;
   }
 }

 return 0;
}

Sample code output

The sample will output an message like the following.


C:> a.exe
Host Name: GeekPC
Domain Name:
DNS Server:
      192.168.0.1

Structure used by GetNetworkParams()

The following structure is FIXED_INFO that is used by GetNetworkParams().


typedef struct {
  char HostName[MAX_HOSTNAME_LEN + 4];
  char DomainName[MAX_DOMAIN_NAME_LEN + 4];
  PIP_ADDR_STRING CurrentDnsServer;
  IP_ADDR_STRING DnsServerList;
  UINT NodeType;
  char ScopeId[MAX_SCOPE_ID_LEN + 4];
  UINT EnableRouting;
  UINT EnableProxy;
  UINT EnableDns;
} FIXED_INFO, *PFIXED_INFO;

HostName Host name of the local computer.
DomainName The domain that the local computer is in.
CurrentDnsServer Not used. It seems that DnsServerList is used instead.
DnsServerList List of DNS servers used by the local computer. It is a linked list.
NodeType Node type of the local computer. Following 4 types are available, BROADCAST_NODETYPE, PEER_TO_PEER_NODETYPE, MIXED_NODETYPE, HYBRID_NODETYPE.
ScopeId DHCP scope name.
EnableRouting Shows if routing is enabled. i.e. Shows if the local computer is a router.
EnableProxy Shows if the local computer serves an ARP Proxy service.
EnableDns Shows if the local computer serves as a DNS server.
  

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