Visual C++ Problem-1
Results 1 to 5 of 5

Thread: Visual C++ Problem-1

  1. #1
    Junior Member
    Join Date
    Jun 2003
    Posts
    8

    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

  2. #2
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,401
    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.

  3. #3
    Senior Member
    Join Date
    Jun 2003
    Posts
    188
    On a local machine use the GetComputerName api function to retrieve you local netbios name

  4. #4
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,401
    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.

  5. #5
    Junior Member
    Join Date
    Jun 2003
    Posts
    8
    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
  •