Solved

DNS Resolving in MFC

Posted on 1998-01-09
10
609 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
Comment Utility
// 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
// 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:dbetz
Comment Utility
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
Comment Utility
Sorry, mental lapse occurred :-{

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

Author Comment

by:dbetz
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you, it works fine now.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
lucky13 challenge 11 111
has77  challenge 9 67
java ^ examples 8 57
Execute multiple curl cmds with sleep and send output to file 10 80
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

10 Experts available now in Live!

Get 1:1 Help Now