Retrieval of Computer Names and their IP Addresses
Posted
by V.Girish V.Girish
on June 8th, 2001
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

Comments
DANGEROUS Memory Reference in the code
Posted by RogerGarrett on 05/04/2005 10:26amThe 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; // 76ReplyDo It in a Dinamic way
Posted by Legacy on 12/12/2003 12:00amOriginally posted by: Jose Luis Briones
ReplyHow can I get all ip address in windows 98 ?
Posted by Legacy on 05/14/2003 12:00amOriginally posted by: HYEONI
this programm doesn't works with windows 98
ReplyHow can I get all ip address in windows 98 ?
good start
Posted by Legacy on 02/06/2003 12:00amOriginally posted by: ajit.v
i am very much interested in network programming using VC++.it was a great start 4 me.all the best
ReplyDetecting PCs in Network
Posted by Legacy on 01/06/2003 12:00amOriginally 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.
ReplyRetrieval of Computer Names and their IP Addresses
Posted by Legacy on 12/26/2002 12:00amOriginally posted by: palanivelu
ReplyI don't see any computer...
Posted by Legacy on 12/15/2002 12:00amOriginally 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 ?
ReplyIt would be nice to know how it works the other way round
Posted by Legacy on 12/05/2002 12:00amOriginally posted by: Thilo
Replyerror in compiling in WinCE 3.0
Posted by Legacy on 08/23/2002 12:00amOriginally 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.
ReplyWhy no IP address shown ?
Posted by Legacy on 08/22/2002 12:00amOriginally 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
ReplyLoading, Please Wait ...