To enumerate a network resource, an application passes a pointer to a NETRESOURCE structure to the WNetOpenEnum function. WNetOpenEnum creates a handle to the resource described by the NETRESOURCE structure. The application then passes this handle to the WNetEnumResource function. WNetEnumResource returns information about the resource that is identified by the handle, in the form of an array of NETRESOURCE structures. When the handle is no longer needed, it should be closed by calling the WNetCloseEnum function.
An application can continue enumerating any container resource described in the array of NETRESOURCE structures retrieved by WNetEnumResource. If the dwUsage member of the NETRESOURCE structure is RESOURCEUSAGE_CONTAINER, the application can pass a pointer to that structure to WNetOpenEnum. If dwUsage is RESOURCEUSAGE_CONNECTABLE, the application can use the structure in a call to the WNetAddConnection2 function.
The following code fragment is a function that enumerates all the resources on a network. An application would call this function for the first time specifying NULL for the pointer to the NETRESOURCE function; when the WNetOpenEnum function uses a NULL pointer in this case, it retrieves a handle to the logical root of the network. Whenever a NETRESOURCE structure retrieved by WNetEnumResource is RESOURCEUSAGE_CONTAINER, the function calls itself, using a pointer to that structure in the call to WNetOpenEnum.
BOOL FAR PASCAL EnumerateFunc(hwnd, hdc, lpnr)
HWND hwnd;
HDC hdc;
LPNETRESOURCE lpnr;
{
DWORD dwResult, dwResultEnum;
HANDLE hEnum;
DWORD cbBuffer = 16384; /* 16K is a reasonable buffer size for NT */
DWORD cEntries = 0xFFFFFFFF; /* enumerate all possible entries */
LPNETRESOURCE lpnrLocal; /* pointer to enumerated structures */
DWORD i;
dwResult = WNetOpenEnum(RESOURCE_GLOBALNET,
RESOURCETYPE_ANY,
0, /* enumerate all resources */
lpnr, /* NULL first time this function is called */
&hEnum); /* handle to resource */
if (dwResult != NO_ERROR) {
/* Locally defined error handler. */
ErrorHandler(hwnd, dwResult, "WNetOpenEnum");
return FALSE;
}
do {
/* Allocate memory for NETRESOURCE structures. */
lpnrLocal = (LPNETRESOURCE) GlobalAlloc(GPTR, cbBuffer);
dwResultEnum = WNetEnumResource(hEnum, /* resource handle */
&cEntries, /* defined locally as 0xFFFFFFFF */
lpnrLocal, /* LPNETRESOURCE */
&cbBuffer); /* buffer size */
if (dwResultEnum == NO_ERROR) {
for(i = 0; i < cEntries; i++) {
/*
* Locally defined function for displaying contents of
* NETRESOURCE structures.
*/
DisplayStruct(hdc, &lpnrLocal[i]);
/*
* Call function recursively if this NETRESOURCE is a
* container.
*/
if(RESOURCEUSAGE_CONTAINER ==
(lpnrLocal[i].dwUsage & RESOURCEUSAGE_CONTAINER))
if(!EnumerateFunc(hwnd, hdc, &lpnrLocal[i]))
TextOut(hdc, 10, 10,
"EnumerateFunc returned FALSE.", 29);
}
GlobalFree((HGLOBAL) lpnrLocal);
}
else if (dwResultEnum != ERROR_NO_MORE_ITEMS) {
ErrorHandler(hwnd, dwResultEnum, "WNetEnumResource");
break;
}
}
while(dwResultEnum != ERROR_NO_MORE_ITEMS);
GlobalFree((HGLOBAL) lpnrLocal);
dwResult = WNetCloseEnum(hEnum);
if(dwResult != NO_ERROR) {
ErrorHandler(hwnd, dwResult, "WNetCloseEnum");
return FALSE;
}
return TRUE;
}