|
-
February 13th, 2006, 06:56 PM
#1
Get Names and IP's on local network.
I am attempting to get a list of names and ip addresses of all devices connected to my local network. Well actually I'm looking for just one ipaddress whose name is known. The network is using DHCP for most devices.
The code below will return the name and IP of my computer (the one on which this program is running), but I cannot figure out how to get the other devices. The network is linked together with a number of 5 port workgroup switches. My computer and the device I'm looking for are on the same switch, if that makes a difference.
I relatively new to C++ and networking. This code has been assembled from Micro$ and Google searches and, to be honest, I don't fully understand it - especially.... BufferSize/sizeof(NETRESOURCE).
Any help would be appreciated.
Code:
// function run in 'main'
void Library::Get_IPs() {
struct hostent *host;
char szHostName[200];
DWORD dwScope = RESOURCE_GLOBALNET; //RESOURCE_CONTEXT;
DWORD dwUsage = RESOURCEUSAGE_CONTAINER;
DWORD dwType = RESOURCETYPE_ANY;
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;
for (int i=0; i<BufferSize/sizeof(NETRESOURCE); i++, NetResource++) {
if (NetResource->dwUsage == dwUsage && NetResource->dwType == dwType) {
gethostname(szHostName, strlen(szHostName));
host = gethostbyname(NetResource->lpRemoteName);
printf("My Host name : %s\n", host->h_name );
printf("My Host addr : %s\n", inet_ntoa( *( in_addr *)host->h_addr) );
}
}
delete Buffer;
WNetCloseEnum( hEnum );
}
WSACleanup();
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|