how to get IP address of computer given its name

  • Thread starter Thread starter codymanix
  • Start date Start date
C

codymanix

how can I get the IP address of a computer when I only know its computername
in a windows network?
 
how can I get the IP address of a computer when I only know its computername
in a windows network?

Here's an example doctored from Knowledge Base article 183988 "HOWTO:
Retrieve the IP Address of the Remote PPP Peer":

DWORD GetIpAddress(char *hostname)
{
DWORD dwIP = 0;

TCHAR msg[128];
HOSTENT *lpHost=NULL;
struct sockaddr_in dest;

lpHost = gethostbyname(hostname);
if (lpHost == NULL)
{
wsprintf(msg, _T("gethostbyname failed: %d"),
WSAGetLastError());
MessageBox(NULL, msg, NULL, MB_OK);
}
else
{
for(int i=0; lpHost->h_addr_list != NULL ;i++)
{
memcpy(&(dest.sin_addr),
lpHost->h_addr_list,
lpHost->h_length);
dwIP = ntohl( dest.sin_addr.S_un.S_addr );
}

}

return dwIP;
}

Dave
 
Back
Top