Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to Display all the IP Address in the Listbox using VC++

Posted on 2004-08-17
1
277 Views
Last Modified: 2013-11-20
Hi All,

    Iam doing socket programming in vc++ in that  I want to display all the IP Address in the domain using VC++ any body have idea for this.
Thanks in Advance
Regards
SureshPadmanabhan

0
Comment
Question by:SueshPadmanabhan
1 Comment
 
LVL 14

Accepted Solution

by:
wayside earned 50 total points
ID: 11823462
Use  NetQueryDisplayInformation() to enumerate the domain and gethostbyname() to get the ip address, inet_ntoa() to print it.

This code is derived from an MSDN sample:

#define UNICODE
#include <windows.h>
#include <stdio.h>
#include <lm.h>

void main( int argc, char *argv[ ] )
{
   WORD wVersionRequested;
   WSADATA wsaData;
   int err;
 
   wVersionRequested = MAKEWORD( 2, 2 );
 
   err = WSAStartup( wVersionRequested, &wsaData );
   
   PNET_DISPLAY_MACHINE pBuff, p;
   DWORD res, dwRec, i = 0;
   //
   // You can pass a NULL or empty string
   //  to retrieve the local information.
   //
   TCHAR szServer[255]=TEXT("");

   if(argc > 1)
      //
      // Check to see if a server name was passed;
      //  if so, convert it to Unicode.
      //
      MultiByteToWideChar(CP_ACP, 0, argv[1], -1, szServer, 255);

   do // begin do
   {
      //
      // Call the NetQueryDisplayInformation function;
      //   specify information level 2 (individual machine information).
      //
      res = NetQueryDisplayInformation(szServer, 2, i, 1000, MAX_PREFERRED_LENGTH, &dwRec, (PVOID *)&pBuff);
      //
      // If the call succeeds,
      //
      if((res==ERROR_SUCCESS) || (res==ERROR_MORE_DATA))
      {
         p = pBuff;
         for(;dwRec>0;dwRec--)
         {
            char name[256];
            sprintf(name, "%S", p->usri2_name); // convert from unicode
            name[strlen(name)-1] = 0; // get rid of trailing '$'
            struct hostent * he = gethostbyname(name);
            if (he) {
              struct sockaddr_in sa;
              memcpy(&(sa.sin_addr),he->h_addr,he->h_length);

              printf("Name: %s IP addr: %s\n",name,  inet_ntoa(sa.sin_addr));
            }
            else {
              printf("Name: %s IP addr: %s\n",name,  "NULL");
            }

 
            //
            // If there is more data, set the index.
            //
            i = p->usri2_next_index;
            p++;
         }
         //
         // Free the allocated memory.
         //
         NetApiBufferFree(pBuff);
      }
      else
         printf("Error: %u\n", res);
   //
   // Continue while there is more data.
   //
   } while (res==ERROR_MORE_DATA); // end do
   return;
}
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question