-
Visual C++ Problem-1
Hi guys,
Last week me and my friend tried to make a simple project about how to detect our local Address in Visual C++ .
void CAboutDlg::OnDetect()
{
WORD Version;
WSADATA wsaData;
char name[255];
CString ip;
CWaitCursor wait;
PHOSTENT hostinfo;
Version = MAKEWORD( 2, 2 );
if(WSAStartup(Version,&wsaData)==0)
{
if(gethostname ( name, sizeof (name)) ==0)
{
if(hostinfo = gethostbyname (name)) != NULL)
{
ip=inet_ntoa (*(struct in_addr
*)*hostinfo>h_addr_list);
m_cIPAddress.SetWindowText(ip);
m_cName.SetWindowText(name);
m_cIPAddress.EnableWindow(TRUE);
m_cName.EnableWindow(TRUE);
}
}
WSACleanup( );
}
}
when i try to run the programm it doesn't show the Computer name .. it only show the IP Address.
Can anyone help us ?
-Mike
-
This is probably because you cannot reverse resolve the hostname through DNS.
Gethostbyname uses DNS to find the hostname. Try editting the hosts file and add your hostname and ip address to it. Or setup an DNS server with forward and reverse entries.
You can find the hosts file on NT4/2K/XP at %windir%\system32\drivers\etc.
Another option to get the hostname on a windows machine is by reading the following registrykey:
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\tcpip\parameters\hostname
-
On a local machine use the GetComputerName api function to retrieve you local netbios name
-
Be careful. Your netbios name doesn't have to be the same as your hostname (at least on NT4 not sure about 2K/XP).
-
i guess u are correct sirdice .. that helps me alot .
hehehehe .. no wonder this is a good community for discuss some problem .. thansk sirdice .. i preciate that alot.