Solved

DNS Resolving in MFC

Posted on 1998-01-09
10
623 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now