-
June 30th, 2003, 06:01 AM
#1
Junior Member
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
-
June 30th, 2003, 10:22 AM
#2
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
Oliver's Law:
Experience is something you don't get until just after you need it.
-
June 30th, 2003, 10:43 AM
#3
On a local machine use the GetComputerName api function to retrieve you local netbios name
-
June 30th, 2003, 10:59 AM
#4
Be careful. Your netbios name doesn't have to be the same as your hostname (at least on NT4 not sure about 2K/XP).
Oliver's Law:
Experience is something you don't get until just after you need it.
-
June 30th, 2003, 12:38 PM
#5
Junior Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|