Solved

DNS Resolving in MFC

Posted on 1998-01-09
10
646 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.

828 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