Retrieval of Computer Names and their IP Addresses | CodeGuru

Retrieval of Computer Names and their IP Addresses

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Jun 8, 2001
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

Downloads

Download source – 27 KB Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.