Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / MFC
Article

CDNS 1.0 - An MFC DNS class

Rate me:
Please Sign up or sign in to vote.
3.83/5 (20 votes)
22 May 2005CPOL 64.8K   1.7K   28   11
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generaldns lookup Pin
Dave_Nunes16-Jan-06 7:29
Dave_Nunes16-Jan-06 7:29 
GeneralVery good Pin
foobar4724-May-05 21:25
foobar4724-May-05 21:25 
GeneralDNS Records Pin
Anatoly24-May-05 11:58
Anatoly24-May-05 11:58 
GeneralRe: DNS Records Pin
ThatsAlok24-May-05 20:12
ThatsAlok24-May-05 20:12 
GeneralTo those who vote 1 without reading or looking at the article or code Pin
.rich.w22-May-05 10:33
.rich.w22-May-05 10:33 
GeneralRe: To those who vote 1 without reading or looking at the article or code Pin
Stlan22-May-05 21:23
Stlan22-May-05 21:23 
GeneralRe: To those who vote 1 without reading or looking at the article or code Pin
ThatsAlok22-May-05 22:28
ThatsAlok22-May-05 22:28 
GeneralRe: To those who vote 1 without reading or looking at the article or code Pin
.rich.w23-May-05 4:02
.rich.w23-May-05 4:02 
GeneralRe: To those who vote 1 without reading or looking at the article or code Pin
ThatsAlok23-May-05 17:40
ThatsAlok23-May-05 17:40 
GeneralRe: To those who vote 1 without reading or looking at the article or code Pin
.rich.w24-May-05 4:08
.rich.w24-May-05 4:08 
GeneralRe: To those who vote 1 without reading or looking at the article or code Pin
Geert van Horrik2-Jun-05 23:43
Geert van Horrik2-Jun-05 23:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.