Click here to Skip to main content
15,867,488 members
Articles / Desktop Programming / MFC
Article

Enumeration of Computer Names and their IP address

Rate me:
Please Sign up or sign in to vote.
3.64/5 (18 votes)
28 Jun 2002 160.4K   56   25
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


Written By
Founder
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThank You for nice & helpful Article Pin
Mizanur Rahman11-May-16 8:10
Mizanur Rahman11-May-16 8:10 
GeneralMy vote of 5 Pin
Puneet Kumar Bhatia15-Mar-11 22:18
Puneet Kumar Bhatia15-Mar-11 22:18 
Generalcomments Pin
Riceking9-Mar-05 6:45
Riceking9-Mar-05 6:45 
Questionhow??? Pin
Anonymous28-Oct-04 10:34
Anonymous28-Oct-04 10:34 
AnswerRe: how??? Pin
1of34-Mar-05 6:46
1of34-Mar-05 6:46 
AnswerRe: how??? Pin
David Crow20-Mar-07 8:00
David Crow20-Mar-07 8:00 
GeneralGeting information Pin
one_eddie4-Aug-04 0:57
one_eddie4-Aug-04 0:57 
GeneralCan't find all computer! Pin
Anonymous10-Feb-04 3:26
Anonymous10-Feb-04 3:26 
I run your program in my class and It doesn't show all computer in my class local network!Confused | :confused:
GeneralRe: Can't find all computer! Pin
Chuggy8826-Sep-08 0:04
Chuggy8826-Sep-08 0:04 
Generalerror... Pin
fyh2119-Oct-03 22:28
fyh2119-Oct-03 22:28 
GeneralENUMERATING ALL DEVICES... Pin
borini30-Aug-03 23:52
borini30-Aug-03 23:52 
QuestionCan't get info on 'Workgroup' nodes? Pin
ob63515-Apr-03 10:27
ob63515-Apr-03 10:27 
Generalwsock vs wsock2 Pin
NullStream29-Jun-02 1:37
NullStream29-Jun-02 1:37 
Generalvery nice althrough have some leaks Pin
sidewalk29-Jun-02 1:25
sidewalk29-Jun-02 1:25 
Generalmultithreading Pin
meteor19-Jun-02 19:15
meteor19-Jun-02 19:15 
GeneralRe: multithreading Pin
Peter Hendrix23-Jul-02 3:41
sussPeter Hendrix23-Jul-02 3:41 
QuestionHow to get all the Subnet Mask.... Pin
3-Jun-02 1:26
suss3-Jun-02 1:26 
Questionhow to enum current login user Pin
28-Feb-02 19:41
suss28-Feb-02 19:41 
GeneralI Cant Browse the Computers' names Pin
Hrh24-Feb-02 3:34
Hrh24-Feb-02 3:34 
QuestionHow to hadle CString? Pin
1-Jul-01 1:44
suss1-Jul-01 1:44 
AnswerRe: How to hadle CString? Pin
VGirish1-Jul-01 15:09
VGirish1-Jul-01 15:09 
GeneralMemory leak Pin
1-Jun-01 2:21
suss1-Jun-01 2:21 
GeneralTo Retrieve MAC Address Pin
Maria Jothi28-May-01 3:10
Maria Jothi28-May-01 3:10 
GeneralRe: To Retrieve MAC Address Pin
VGirish5-Jun-01 18:05
VGirish5-Jun-01 18:05 
GeneralRe: To Retrieve MAC Address Pin
29-Aug-01 19:53
suss29-Aug-01 19:53 

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

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