CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2002
    Location
    Seattle
    Posts
    4

    method to get IP address of the host

    hi all,

    I am new to VC++ programming . Is there any way to get
    information about the localhost (i.e.,its IP address , hostname) ?
    In general , how network related issues are handled in VC++?
    Do help this novice user.

    Ashwini

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    The following example will get up to ten assigned IP addresses...
    Code:
    #include <winsock2.h>
    
    // Add 'ws2_32.lib' to your linker options
    
    WSADATA WSAData;
    
    // Initialize winsock dll
    if(::WSAStartup(MAKEWORD(1, 0), &WSAData))
      // Error handling
    
    // Get local host name
    char szHostName[128] = "";
    
    if(::gethostname(szHostName, sizeof(szHostName)))
      // Error handling -> call 'WSAGetLastError()'
    
    // Get local IP addresses
    struct sockaddr_in SocketAddress;
    struct hostent     *pHost        = 0;
    
    pHost = ::gethostbyname(szHostName);
    if(!pHost)
      // Error handling -> call 'WSAGetLastError()'
    
    char aszIPAddresses[10][16] // maximum of ten IP addresses
    
    for(int iCnt = 0; ((pHost->h_addr_list[iCnt]) && (iCnt < 10)); ++iCnt)
    {
      memcpy(&SocketAddress.sin_addr, pHost->h_addr_list[iCnt], pHost->h_length);
      strcpy(aszIPAddresses[iCnt], inet_ntoa(SocketAddress.sin_addr));
    }
    
    // Cleanup
    WSACleanup();
    Last edited by Andreas Masur; December 9th, 2002 at 02:00 AM.

  3. #3
    Join Date
    Nov 2002
    Location
    Israel
    Posts
    182
    In the IpHlpApi there are two not so difficult functions -GetAdaptersInfo, GetNetworkParams.
    Good luck

  4. #4
    Join Date
    Dec 2002
    Posts
    20

    Using Netbios to obtain an IP Address

    You can use the NetBios API to obtain the IP Address...



    /**
    *************************************************
    * Attempt to obtain a non-zero MAC (Media Access
    * Control) from the * network cards by issuing
    * net bios commands.
    * We need to link to Netapi32.lib for this.
    *
    * @methodName McbGetMACAddress
    *
    * @param char uMAC[6]
    *
    * @return bool
    *
    * @exception none
    *
    * @author Martyn C Brown
    *
    * @changeHistory
    * 5th October 2001 - (V1.0) Creation (MCB)
    *************************************************
    */

    bool McbGetMACAddress(unsigned char uMAC[6])
    {
    bool bResult = false;

    typedef struct tagMcbAdaptor
    {
    ADAPTER_STATUS adaptStatus;
    NAME_BUFFER szName[30];
    } McbAdaptor;

    McbAdaptor adapter;
    unsigned char * lpMAC =
    adapter.adaptStatus.adapter_address;


    /*
    *********************************************
    * Prepare network control block for
    * enumeration command
    *********************************************
    */

    NCB ncb;
    LANA_ENUM lenum;

    ::memset(&ncb, 0, sizeof(ncb));
    ncb.ncb_command = NCBENUM;
    ncb.ncb_buffer = (UCHAR *)&lenum;
    ncb.ncb_length = sizeof(lenum);

    /*
    *********************************************
    * Call netbios to enumerate LAN adapters
    * (LANA).
    *********************************************
    */

    UCHAR uRetCode = Netbios(&ncb);

    /*
    *********************************************
    * Enmerate LAN adapters until we find a non
    * zero MAC address.
    *********************************************
    */

    for(int i=0; i<lenum.length && !bResult; i++)
    {
    /*
    *****************************************
    * Prepare network control block for LAN
    * adapter reset. An adapter must be
    * reset before it can accept any other
    * NCB command that specifies the same
    * number in the ncb_lana_num member.
    *****************************************
    */

    ::memset(&ncb, 0, sizeof(ncb));
    ncb.ncb_command = NCBRESET;
    ncb.ncb_lana_num = lenum.lana[i];

    /*
    *****************************************
    * Call netbios with the reset command
    *****************************************
    */

    uRetCode = Netbios(&ncb);

    /*
    *****************************************
    * Prepare network control block to
    * retrieve the LAN adapter status.
    *****************************************
    */

    ::memset(&ncb, 0, sizeof (ncb) );
    ncb.ncb_command = NCBASTAT;
    ncb.ncb_lana_num = lenum.lana[i];

    ::strcpy((char*)ncb.ncb_callname,
    "* ");

    ncb.ncb_buffer = (unsigned char*)
    &adapter;
    ncb.ncb_length = sizeof(adapter);

    ::memset(lpMAC, 0, 6);

    /*
    *****************************************
    * Call netbios to obtain MAC address
    *****************************************
    */

    uRetCode = Netbios(&ncb);

    /*
    *****************************************
    * If the MAC address is non-zero
    *****************************************
    */

    if (::memcmp(lpMAC, "\x0\x0\x0\x0\x0\x0",
    6) != 0)
    {
    /*
    *************************************
    * Take a copy and set the result to
    * indicate success.
    *************************************
    */

    ::memcpy(uMAC, lpMAC, 6);
    bResult = true;
    }
    }

    return bResult;

    }/* McbGetMACAddress */


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured