Resolving Hostname :: MFC

Status
Not open for further replies.
Hi.

I am working on a simple program that resolves hostname. Here is the concept:

Enter hostname: www.google.com
Resolve IP: 216.239.33.101

The program has a dialog box for the user to enter the hostname such as (www.google.com or www.yahoo.com).
Here is the function that does it:

-----
void CDomainDNS::resolveHost(const CString &rHName)
{
WSAData wData;

if (WSAStartup(MAKEWORD(2,2), &wData) == SOCKET_ERROR)
{
CString error;
error.Format("Error initialization Winsock");
AfxMessageBox(error);

return;
}

hostent *host = 0;

host = gethostbyname(rHName);

if (host == 0)
mVeri = false;

else
{
mIP = inet_ntoa(*(reinterpret_cast<in_addr *>(host->h_addr)));

mVeri = true;
}
}
-----

The code above works perfect under Win32 *console*. However, it only outputs my *internal* ip in MFC application mode.

For example:

Enter hostname: www.google.com
Resolve IP: 192.168.0.1

Is there something I need to change or add when dealing with the code above in Win32 MFC environment? Maybe there is a better tool for resolve hostname via MFC?

Thanks,
Kuphryn
 

Delf

Seniorius Lurkius
5
One of my WinSock references encourages you to immediately copy the hostent data because the buffer may be reused by WinSock between the time you call gethostbyname() and the time you use the string.

Also, are you absolutely sure that the function's getting the proper hostname passed in? If you were passing an empty string, then gethostbyname() would likely try to resolve it in the local domain, which could well resolve to your local machine.
 
Status
Not open for further replies.