枚举网络资源

#include <windows.h>
#include <Winnetwk.h>
#include <stdio.h>
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "Mpr.lib")


BOOL GetIpAddress(char *hostname)
{
HOSTENT *lpHost=NULL;
struct sockaddr_in   dest;
lpHost = gethostbyname(hostname);
if (lpHost == NULL)
{
printf("gethostbyname failed: %d", WSAGetLastError());
}
else
{
for(int i=0; lpHost->h_addr_list[i] != NULL ;i++)
{
            memcpy(&(dest.sin_addr), lpHost->h_addr_list[i],
lpHost->h_length);
            printf("IP address is: %s",
inet_ntoa(dest.sin_addr));
}

}
return TRUE;
}
BOOL WINAPI EnumerateFunc (LPNETRESOURCE lpnr)
{
HANDLE hEnum;
DWORD dwIndex, dwResult, dwBufferSize = 16384, dwNumEntries = 0xFFFFFFFF;  // Enumerate all possible entries.
LPNETRESOURCE lpnrLocal;          // Pointer to enumerated structures.

dwResult = WNetOpenEnum (
RESOURCE_GLOBALNET, // All resources on the network
RESOURCETYPE_ANY,   // All resources 
0,                  // Enumerate all resources.
lpnr,               // The container to enumerate
&hEnum);            // Handle to resource
if (dwResult != ERROR_SUCCESS) 
{
WNetGetLastError(NULL,NULL,1024,NULL,1024);
return FALSE;
}

// Allocate memory for NETRESOURCE structures.
if (!(lpnrLocal = (LPNETRESOURCE) LocalAlloc (LPTR, dwBufferSize)))
printf("Not enough memory");

do 
{
dwResult = WNetEnumResource (
hEnum,              // Resource handle
&dwNumEntries,      // Number of entries
lpnrLocal,          // LPNETRESOURCE
&dwBufferSize);     // Buffer size

if (dwResult == ERROR_SUCCESS) 
{
for (dwIndex = 0; dwIndex < dwNumEntries; dwIndex++)
{
printf("\nRemote Name:%s Comment: %s", lpnrLocal[dwIndex].lpRemoteName,
lpnrLocal[dwIndex].lpComment);
GetIpAddress(lpnrLocal[dwIndex].lpRemoteName+2);

}
}
else if (dwResult != ERROR_NO_MORE_ITEMS) 
break;

while(dwResult != ERROR_NO_MORE_ITEMS);
LocalFree (lpnrLocal);
WNetCloseEnum (hEnum);
return TRUE;

#include <iostream.h>
int main(int argc, char* argv[])
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err == 0 ) 
{
NETRESOURCE nr;
nr.dwScope = RESOURCE_GLOBALNET;
nr.dwType = RESOURCETYPE_DISK;  
nr.dwUsage = RESOURCEUSAGE_CONTAINER;
nr.lpLocalName = TEXT("");
nr.lpRemoteName = NULL;//"123-U04JMH6H8AC";//NULL;//"www.csdn.net";//TEXT("ROBOTICS");//这里改为你的域名
nr.lpComment = TEXT("");
nr.lpProvider = TEXT("");
EnumerateFunc (&nr); 
}
int i;
cin>>i;
return 0;
}

你可能感兴趣的:(枚举网络资源)