Click here to Skip to main content
Click here to Skip to main content

Enumeration of Computer Names and their IP address

, 28 Jun 2002
Rate this:
Please Sign up or sign in to vote.
Retrieves the Computer names in a network and their corresponding IP addresses


This code snippet allows you to retrieve the names of the computers in the network and their associated IP addresses. 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 Neighborhood list. All luck.

Things to do

  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.

Program listing

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 Count = 0xFFFFFFFF;
    DWORD BufferSize = 2048;
    LPVOID Buffer = new char[2048];
    WNetEnumResource( hEnum, &Count, 
        Buffer, &BufferSize );
    NetResource = (NETRESOURCE*)Buffer;

    char szHostName[200];
    unsigned int i;

    for ( i = 0; 
        i < BufferSize/sizeof(NETRESOURCE); 
        i++, NetResource++ )
        if ( NetResource->dwUsage == 
            NetResource->dwType == 
            RESOURCETYPE_ANY )
            if ( NetResource->lpRemoteName )
                CString strFullName = 
                if ( 0 == 
                    strFullName.Left(2).Compare("\\\\") )   
                    strFullName = 

                gethostname( szHostName, 
                    strlen( szHostName ) );
                host = gethostbyname(strFullName);

                if(host == NULL) continue; 
                ptr = (struct in_addr *) 

                // 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",

    delete Buffer;
    WNetCloseEnum( hEnum ); 



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

India India
No Biography provided

Comments and Discussions

GeneralMy vote of 5 PinmemberPuneet Kumar Bhatia15-Mar-11 23:18 
Generalcomments PinmemberRiceking9-Mar-05 7:45 
Questionhow??? PinsussAnonymous28-Oct-04 11:34 
AnswerRe: how??? Pinmember1of34-Mar-05 7:46 
AnswerRe: how??? PinmvpDavidCrow20-Mar-07 9:00 
GeneralGeting information Pinmemberone_eddie4-Aug-04 1:57 
GeneralCan't find all computer! PinsussAnonymous10-Feb-04 4:26 
GeneralRe: Can't find all computer! PinmemberChuggy8826-Sep-08 1:04 
Generalerror... Pinmemberfyh2119-Oct-03 23:28 
GeneralENUMERATING ALL DEVICES... Pinmemberborini31-Aug-03 0:52 
QuestionCan't get info on 'Workgroup' nodes? Pinmemberob63515-Apr-03 11:27 
Generalwsock vs wsock2 PinmemberNullStream29-Jun-02 2:37 
Generalvery nice althrough have some leaks Pinmembersidewalk29-Jun-02 2:25 
Generalmultithreading Pinmembermeteor19-Jun-02 20:15 
GeneralRe: multithreading PinsussPeter Hendrix23-Jul-02 4:41 
QuestionHow to get all the Subnet Mask.... PinmemberPhan Tien Vu3-Jun-02 2:26 
Questionhow to enum current login user PinmemberSuresh M28-Feb-02 20:41 
GeneralI Cant Browse the Computers' names PinmemberHrh24-Feb-02 4:34 
QuestionHow to hadle CString? PinmemberDMD1-Jul-01 2:44 
AnswerRe: How to hadle CString? PinmemberVGirish1-Jul-01 16:09 
GeneralMemory leak PinmemberAnonymous1-Jun-01 3:21 
GeneralTo Retrieve MAC Address PinmemberMaria Jothi28-May-01 4:10 
GeneralRe: To Retrieve MAC Address PinmemberVGirish5-Jun-01 19:05 
GeneralRe: To Retrieve MAC Address PinmemberAnonymous29-Aug-01 20:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150327.1 | Last Updated 29 Jun 2002
Article Copyright 2001 by VGirish
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid