Click here to Skip to main content
Licence 
First Posted 21 May 2001
Views 119,760
Bookmarked 51 times

Enumeration of Computer Names and their IP address

By | 28 Jun 2002 | Article
Retrieves the Computer names in a network and their corresponding IP addresses

Introduction

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 

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];
    unsigned int i;

    for ( 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();

License

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

VGirish

Founder

India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberPuneet Kumar Bhatia22:18 15 Mar '11  
Generalcomments PinmemberRiceking6:45 9 Mar '05  
Questionhow??? PinsussAnonymous10:34 28 Oct '04  
AnswerRe: how??? Pinmember1of36:46 4 Mar '05  
AnswerRe: how??? PinmvpDavidCrow8:00 20 Mar '07  
GeneralGeting information Pinmemberone_eddie0:57 4 Aug '04  
GeneralCan't find all computer! PinsussAnonymous3:26 10 Feb '04  
GeneralRe: Can't find all computer! PinmemberChuggy880:04 26 Sep '08  
Generalerror... Pinmemberfyh2122:28 19 Oct '03  
GeneralENUMERATING ALL DEVICES... Pinmemberborini23:52 30 Aug '03  
QuestionCan't get info on 'Workgroup' nodes? Pinmemberob63510:27 15 Apr '03  
Generalwsock vs wsock2 PinmemberNullStream1:37 29 Jun '02  
Generalvery nice althrough have some leaks Pinmembersidewalk1:25 29 Jun '02  
Generalmultithreading Pinmembermeteor19:15 19 Jun '02  
GeneralRe: multithreading PinsussPeter Hendrix3:41 23 Jul '02  
QuestionHow to get all the Subnet Mask.... PinmemberPhan Tien Vu1:26 3 Jun '02  
Questionhow to enum current login user PinmemberSuresh M19:41 28 Feb '02  
GeneralI Cant Browse the Computers' names PinmemberHrh3:34 24 Feb '02  
QuestionHow to hadle CString? PinmemberDMD1:44 1 Jul '01  
AnswerRe: How to hadle CString? PinmemberVGirish15:09 1 Jul '01  
GeneralMemory leak PinmemberAnonymous2:21 1 Jun '01  
GeneralTo Retrieve MAC Address PinmemberMaria Jothi3:10 28 May '01  
GeneralRe: To Retrieve MAC Address PinmemberVGirish18:05 5 Jun '01  
GeneralRe: To Retrieve MAC Address PinmemberAnonymous19:53 29 Aug '01  

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.

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