Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CDNS 1.0 - An MFC DNS class

0.00/5 (No votes)
22 May 2005 1  
An MFC implementation of a DNS class, it can retrieve multiple IPs and hostnames.

Sample Image - CDNS.PNG

Introduction

I needed to use DNS lookups in a little Internet app I was using, and couldn't find a manageable solution. So I created one.

What are DNS lookups and why would you use one?

DNS stands for Domain Name Server/Service. It is the method which all browsers use to resolve hostnames (www.codeproject.com, for example) into IP addresses they can connect to.

How does this code work?

In my class, I have used a few Winsock functions to do the lookups, mainly GetHostByName to fill the hostent structure with information. The basic process is:

  • WSAStartup to initialize.
  • GetHostByName to fill the hostent structure.
  • Check if any information was retrieved.
  • If so, loop through the returned IPs and hostnames and add them to an array.
  • WSACleanup to end the process.

Using the class

The main three functions that will be used are:

  • SetHostname
  • DoDNSLookup
  • GetIPAt

An example of retrieving a hostname's primary IP is:

// Define a CDNS object

CDNS dnsObj;

// Set the hostname

dnsObj.SetHostname("www.google.be");

// Do the DNS lookup

BOOL doLookup = dnsObj.DoDNSLookup;

if (doLookup)
{
    // Retrieve the IP

    CString thisIP = dnsObj.GetIPAt(0);
}

Of course, the class retrieves multiple IPs, so you'll need to do a loop to get them all. Here's an example:

// Define a CDNS object

CDNS dnsObj;

// Set the hostname

dnsObj.SetHostname("www.google.be");

// Do the DNS lookup

BOOL doLookup = dnsObj.DoDNSLookup;

if (doLookup)
{
    for (int i = 0; i <= dnsObj.GetNumberOfIP(); i++)
    {
        CString thisIP = dnsObj.GetIPAt(i);
        // Do something with the IP

    }
}

History

  • 1.0
    • 22nd May, 2005: First public release.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here