Retrieval of Computer Names and their IP Addresses

Environment: [eg VC6 SP4, NT4 SP3, winCE 2.0]

You can use this code to retrieve the host information in a network and also get the IP address of each pc. Its something similar to what you see in the Network Neighbourhood list. All luck.

1) Include winsock2.h

2) In the Menu, go to Project-->Settings and in the Link tab, you can see a text box named Object/Library Modules. In that, add ws2_32.lib mpr.lib to the existing entries there. Those 2 libraries have to be added for this code snippet to compile without any linker errors.

CString strTemp;	
struct hostent *host;
struct in_addr *ptr;	// To retrieve the IP Address

DWORD dwScope = RESOURCE_CONTEXT;
NETRESOURCE *NetResource = NULL;
HANDLE hEnum;
WNetOpenEnum( dwScope, NULL, NULL, NULL, &hEnum );

WSADATA wsaData;
WSAStartup(MAKEWORD(1,1),&wsaData);

if ( hEnum )
{
  DWORD Count = 0xFFFFFFFF;
  DWORD BufferSize = 2048;
  LPVOID Buffer = new char[2048];
  WNetEnumResource( hEnum, &Count, Buffer, &BufferSize );
  NetResource = (NETRESOURCE*)Buffer;

  char szHostName[200];

  for ( unsigned int i = 0; i < BufferSize/sizeof(NETRESOURCE); 
                                          i++, NetResource++ )
  {
    if ( NetResource->dwUsage == RESOURCEUSAGE_CONTAINER && 
                  NetResource->dwType == RESOURCETYPE_ANY )
    {
      if ( NetResource->lpRemoteName )
      {
         CString strFullName = NetResource->lpRemoteName;
         if ( 0 == strFullName.Left(2).Compare("\\\\") )
           strFullName = strFullName.Right(strFullName.GetLength()-2);
         gethostname( szHostName, strlen( szHostName ) );
         host = gethostbyname(strFullName);
         if(host == NULL) continue; 
         ptr = (struct in_addr *) host->h_addr_list[0];

         // Eg. 211.40.35.76 split up like this.
         int a = ptr->S_un.S_un_b.s_b1;  // 211
         int b = ptr->S_un.S_un_b.s_b2;  // 40
         int c = ptr->S_un.S_un_b.s_b3;  // 35
         int d = ptr->S_un.S_un_b.s_b4;  // 76

         strTemp.Format("%s -->  %d.%d.%d.%d",strFullName,a,b,c,d);
         AfxMessageBox(strTemp);
      }
    }
  }
  delete Buffer;
  WNetCloseEnum( hEnum );	
}

WSACleanup();

// End of Code

Downloads

Download source - 27 KB Kb


Comments

  • DANGEROUS Memory Reference in the code

    Posted by RogerGarrett on 05/04/2005 10:26am

    The code, as shown, contains a potentially dangerous memory reference. The following addresses the problem by clearing the szHostName buffer, using an explicit buffer size instead of the strlen() function, and by checking the ptr for NULL before using it.
    
    #define BUFFER_SIZE 200
    
    char szHostName[BUFFER_SIZE];
    
             CString strFullName = NetResource->lpRemoteName;
             memset(szHostName, '\0', BUFFER_SIZE);
             gethostname( szHostName, BUFFER_SIZE);
             host = gethostbyname(strFullName);
             if(host == NULL) continue;
             ptr = (struct in_addr *) host->h_addr_list[0];
             ASSERT(NULL != ptr);
             if (NULL != ptr)
             {
                 // Eg. 211.40.35.76 split up like this.
                 int a = ptr->S_un.S_un_b.s_b1;  // 211
                 int b = ptr->S_un.S_un_b.s_b2;  // 40
                 int c = ptr->S_un.S_un_b.s_b3;  // 35
                 int d = ptr->S_un.S_un_b.s_b4;  // 76

    Reply
  • Do It in a Dinamic way

    Posted by Legacy on 12/12/2003 12:00am

    Originally posted by: Jose Luis Briones

         Many of you have problems with the size of the buffer. You can�t find all the Pcs of the total net and the solution you provide is to make bigger the size of the buffer. Ther is one more razonable metod to do this. You can do the same in a dinamic way like this:
    

    DWORD BufferSize = sizeof(NETRESOURCE);
    DWORD dwResult;
    LPVOID Buffer = new char[BufferSize];
    do
    {
    DWORD Count = 0xFFFFFFFF;
    dwResult=WNetEnumResource( hEnum, &Count, Buffer, &BufferSize );
    NetResource = (NETRESOURCE*)Buffer;
    ///Do here what you want with the netresource
    }while(dwResult!=ERROR_NO_MORE_ITEMS);

    By this way you must eliminate the for.

    Reply
  • How can I get all ip address in windows 98 ?

    Posted by Legacy on 05/14/2003 12:00am

    Originally posted by: HYEONI

    this programm doesn't works with windows 98
    How can I get all ip address in windows 98 ?

    Reply
  • good start

    Posted by Legacy on 02/06/2003 12:00am

    Originally posted by: ajit.v

    i am very much interested in network programming using VC++.it was a great start 4 me.all the best

    Reply
  • Detecting PCs in Network

    Posted by Legacy on 01/06/2003 12:00am

    Originally posted by: Suresh Shirgave

    I tried this code sample on both windows 2000 & windows 98. But it doesn't works with windows 98. Please tell me what are modifications needed in this program to run under windows 98.


    Reply
  • Retrieval of Computer Names and their IP Addresses

    Posted by Legacy on 12/26/2002 12:00am

    Originally posted by: palanivelu

    I am not able to get the computer names in win'95/98 machine but  i am able to get the computer name in windows2000? pls reply me
    

    Reply
  • I don't see any computer...

    Posted by Legacy on 12/15/2002 12:00am

    Originally posted by: ssinalol

    I have Win98 machine, I don't see any computer in the window that i see in my network neighbourhood. Why is it not working. Please tell me how to change the code ?

    Reply
  • It would be nice to know how it works the other way round

    Posted by Legacy on 12/05/2002 12:00am

    Originally posted by: Thilo

    You are working on the NetBios Host name.
    
    What if you have the ip-address first.
    How can you get that information.
    Ok i know the function 'gethostbyaddr', but
    what if the computer, you wanna retrieve the hostname from,
    is shut down??
    If anybody knows a answer or how to talk with a dns server
    it would be great if you write me a mail :-).

    Ciao Thilo
    5.12.2002 (if the date is older then one year,
    you don't need to write me then ;) )

    Reply
  • error in compiling in WinCE 3.0

    Posted by Legacy on 08/23/2002 12:00am

    Originally posted by: Peter

    i am using Embedded WinCE 3.0

    i have problem in compiling
    i will be grateful if anyone can help me in solving it.
    The error is shown below :

    fatal error C1083: Cannot open include file: 'Winsock2.h': No such file or directory

    error C2065: 'Enable3dControls' : undeclared identifier
    Error executing shcl.exe.


    Reply
  • Why no IP address shown ?

    Posted by Legacy on 08/22/2002 12:00am

    Originally posted by: John Anson

    I have use visual C++ to run this program. After the files have been executed, there is no IP address shown after clicking the "Get computer name and IP address". Why is this so ?

    John Anson

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

  • A global data storage provider whose business is booming needed a best-in-class data center to serve as the backbone of its technical operations going forward—and it needed it delivered within a year.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds