Solved

DNS Resolving in MFC

Posted on 1998-01-09
10
634 Views
Last Modified: 2013-11-20
How do you resolve a DNS address in MFC?  Do you need to do it in API.  I need source code to support this theory.  Can any one give me any source code in either MFC or the API?
0
Comment
Question by:dbetz
  • 5
  • 5
10 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 1313735
// First, initialize the WINSOCK interface
WORD wVersionRequested;
WSADATA wsaData;
int err;
HOSTENT *hostent;

wVersionRequested = MAKEWORD( 2, 0 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
      /* Tell the user that we could not find a usable */
      /* WinSock DLL.                                  */    
      AfxMessageBox("ERROR: Failed to initialize WINSOCK", MB_OK);
      return;
}


// Do a DNS lookup of a hostname from it's IP
CString ip("192.168.0.10");
unsigned long ipaddr = inet_addr((LPCTSTR)ip);
hostent = gethostbyaddr((char *)&ipaddr, 4, AF_INET);
if(hostent != NULL){
      ip = CString(hostent->h_name);
}
0
 

Author Comment

by:dbetz
ID: 1313736
No, I need the MFC code to get the IP from a hostname.  Do you have any MFC code?
0
 
LVL 32

Expert Comment

by:jhance
ID: 1313737
The above code is in MFC but gives you the hostname from the IP address.  To do the other lookup, use the gethostbyname() function.  The code would be very similar but would have:

CString hostaddr;

hostent = gethostbyname(hostname);
hostaddr = CString(hostent->h_addr_list);


0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

Author Comment

by:dbetz
ID: 1313738
Can you put that in the entire code for me, and no that is not MFC, that is API code...But that is alright.
0
 
LVL 32

Expert Comment

by:jhance
ID: 1313739
// First, initialize the WINSOCK interface
WORD wVersionRequested;
WSADATA wsaData;
int err;
HOSTENT *hostent;

wVersionRequested = MAKEWORD( 2, 0 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
AfxMessageBox("ERROR: Failed to initialize WINSOCK", MB_OK);
return;
}


// Do a DNS lookup of an IP from it's hostname
CString hostnm("fred.bob.com");
hostent = gethostbyname((LPCTSTR)hostnm);
if(hostent != NULL){
hostnm = CString(hostent->h_addr_list);
}
0
 

Author Comment

by:dbetz
ID: 1313740
I get this error this this code, it is at this line...It looks as though it is trying to convert an array to a CString.

"hostnm = CString(hostent->h_addr_list);"

error C2665: 'CString::CString' : none of the 7 overloads can convert parameter 1 from type 'char ** ' (new behavior; please see help)
0
 
LVL 32

Expert Comment

by:jhance
ID: 1313741
Sorry, mental lapse occurred :-{

hostnm = CString(hostent->h_addr_list[0]);"
0
 

Author Comment

by:dbetz
ID: 1313742
That is to return an IP then?  All I get is garble with the host name: ÌGÈJwww.yahoo.com

And the same garble everytime...and it does not access the Internet anymore.  The IP->Host worked though.
0
 
LVL 32

Accepted Solution

by:
jhance earned 50 total points
ID: 1313743
OK, I stopped trying to do it from memory.  Following is a complete program that you can compile and run:

#include <stdio.h>
#include <winsock.h>

void main()
{
 // First, initialize the WINSOCK interface
 WORD wVersionRequested;
 WSADATA wsaData;
 int err;
 HOSTENT *hostent;
 IN_ADDR in_addr;

 wVersionRequested = MAKEWORD( 2, 0 );
 err = WSAStartup( wVersionRequested, &wsaData );
 if ( err != 0 ) {
 /* Tell the user that we could not find a usable */
 /* WinSock DLL. */
 printf("ERROR: Failed to initialize WINSOCK");
 return;
 }


 // Do a DNS lookup of an IP from it's hostname
 hostent = gethostbyname("www.yahoo.com");
 if(hostent != NULL){
  memcpy(&in_addr, hostent->h_addr_list[0], 4);
  printf("www.yahoo.com = %s\n", inet_ntoa(in_addr));
 }
 else{
  printf("Error, lookup failed\n");
 }
}
0
 

Author Comment

by:dbetz
ID: 1313744
Thank you, it works fine now.
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
repeatSeparator  java  challenge 13 57
Folder Comparison 12 54
Sed question 2 86
Updating statistics with error notification email in SQL server 4 96
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question