Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DNS Resolving in MFC

Posted on 1998-01-09
10
Medium Priority
?
678 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 200 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

773 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