Virtual Developer Workshop: Containerized Development with Docker

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

WNetOpenEnum( dwScope, NULL, NULL, NULL, &hEnum );

WSADATA wsaData;

if ( hEnum )
  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. 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);
  delete Buffer;
  WNetCloseEnum( hEnum );	


// End of Code


Download source - 27 KB Kb


  • DANGEROUS Memory Reference in the code

    Posted by RogerGarrett on 05/04/2005 05:26pm

    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. 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

  • Do It in a Dinamic way

    Posted by Legacy on 12/12/2003 08: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];
    DWORD Count = 0xFFFFFFFF;
    dwResult=WNetEnumResource( hEnum, &Count, Buffer, &BufferSize );
    NetResource = (NETRESOURCE*)Buffer;
    ///Do here what you want with the netresource

    By this way you must eliminate the for.

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

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

    Originally posted by: HYEONI

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

  • good start

    Posted by Legacy on 02/06/2003 08: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

  • Detecting PCs in Network

    Posted by Legacy on 01/06/2003 08: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.

  • Retrieval of Computer Names and their IP Addresses

    Posted by Legacy on 12/26/2002 08: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

  • I don't see any computer...

    Posted by Legacy on 12/15/2002 08: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 ?

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

    Posted by Legacy on 12/05/2002 08: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 ;) )

  • error in compiling in WinCE 3.0

    Posted by Legacy on 08/23/2002 07: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.

  • Why no IP address shown ?

    Posted by Legacy on 08/22/2002 07: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

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

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

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date