IP Enumeration | CodeGuru

IP Enumeration

. Environment: VC4, VC5, VC6 I think this is the quickest way to enumerate the available IP addresses on your machine. I’ve gotten this question a lot. This is fairly simple but haven’t seen it anywhere. IP Enumeration // Function GetIPs // in : CStringArray // out : CStringArray + count int GetIPs( CStringArray &arrIPS […]

Written By
CodeGuru Staff
CodeGuru Staff
Dec 24, 2003
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

.

Environment: VC4, VC5, VC6

I think this is the quickest way to enumerate the available IP addresses on your machine. I’ve gotten this question a lot. This is fairly simple but haven’t seen it anywhere.

IP Enumeration

// Function GetIPs
// in : CStringArray
// out : CStringArray + count
int GetIPs( CStringArray &arrIPS )
{
    BOOL bRet = TRUE;
    char name[255];
    int i=0;
    PHOSTENT hostinfo;
  //first, clear the list
    arrIPS.RemoveAll();
  //get the host name
  if( gethostname ( name, sizeof(name)) == 0)
  {
    // get the host info
    if((hostinfo = gethostbyname(name)) != NULL)
    {
      // now, loop until the address list = null
      while( hostinfo->h_addr_list[i] != NULL )
      {
        //get the IP address
        char *ip_addr = inet_ntoa (*(struct in_addr *)
                        hostinfo->h_addr_list[i]);
        //add it to the array
        ip_addr );
        //increment the counter
        i++;
      }
    }
  }
  //return the array count
  return arrIPS.GetSize();
}
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.