Obtain local machine IP address

This is a discussion on Obtain local machine IP address within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I need to obtain the local ip address of a PC e.g. 192.168.0.100 I have tried using gethostbyname() function, ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    35

    Question Obtain local machine IP address

    Hi,

    I need to obtain the local ip address of a PC
    e.g.

    192.168.0.100

    I have tried using gethostbyname() function, but can only get the name, not the ip address.


    struct hostent *name;
    name = gethostbyname("localhost");
    m_ip.SetWindowText(name->h_name);


    Please help!

    Compile VC++ Platform win xp pro

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    the HOSTENT stucture dose contrain the IP address(es)
    Code:
    struct hostent {
        char FAR *       h_name;
        char FAR * FAR * h_aliases;
        short            h_addrtype;
        short            h_length;
        char FAR * FAR * h_addr_list;
    };
    it's not in text format so you will need to use inet_ntoa to convert the address(es) to dotted format.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    35
    Can you give me a code sample of how to do this?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    To ge the first address.
    Code:
    struct in_addr Address;
    struct hostent *name; 
    name = gethostbyname("localhost");
    memcpy(&Address, *name->h_addr_list, 4);
    // and put it somewhere
    m_ip.SetWindowText(inet_ntoa(Address));

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    35

    Gateway

    Also I need to get the local pc's gateway address

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does this do (Windows API)?
    By EVOEx in forum Windows Programming
    Replies: 4
    Last Post: 12-19-2008, 10:48 AM
  2. Internal IP address program
    By John87412897 in forum Networking/Device Communication
    Replies: 4
    Last Post: 08-19-2008, 05:57 AM
  3. Replies: 2
    Last Post: 07-08-2008, 04:45 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  5. MSN Vital Information
    By iain in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 09-22-2001, 09:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21