Home   Index   About
Ultimate Pack


Custom Search
Enumerating Network Resources

To enumerate a network container resource, your application should pass the address of 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, which returns information about the resource in the form of an array of NETRESOURCE structures. When the handle is no longer needed, the application can close it by calling the WNetCloseEnum function.

Your 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 the address of that structure to WNetOpenEnum to open the container and continue the enumeration. If dwUsage is RESOURCEUSAGE_CONNECTABLE, the application can pass the structure's address to the WNetAddConnection2 function.

The following example illustrates an application-defined function (EnumerateFunc) that enumerates all the resources on a network. When calling this function, specify NULL for the pointer to the NETRESOURCE structure. When WNetOpenEnum receives the NULL pointer, it retrieves a handle to the root of the network. Whenever a NETRESOURCE structure retrieved by WNetEnumResource is RESOURCEUSAGE_CONTAINER, the EnumerateFunc function calls itself and uses a pointer to that structure in its call to WNetOpenEnum.

BOOL WINAPI EnumerateFunc(HWND hwnd,

HDC hdc,

LPNETRESOURCE lpnr)

{

DWORD dwResult, dwResultEnum;

HANDLE hEnum;

DWORD cbBuffer = 16384; // 16K is a good size

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) {

// An application-defined error handler is demonstrated in the

// section titled "Retrieving Network Errors."

NetErrorHandler(hwnd, dwResult, (LPSTR)"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++)

{

// Following is an application-defined function for

// displaying contents of NETRESOURCE structures.

DisplayStruct(hdc, &lpnrLocal[i]);

//

// If this NETRESOURCE is a container, call the function

// recursively.

if(RESOURCEUSAGE_CONTAINER ==

(lpnrLocal[i].dwUsage & RESOURCEUSAGE_CONTAINER))

if(!EnumerateFunc(hwnd, hdc, &lpnrLocal[i]))

TextOut(hdc, 10, 10,

"EnumerateFunc returned FALSE.", 29);

}

}

else if (dwResultEnum != ERROR_NO_MORE_ITEMS) {

NetErrorHandler(hwnd, dwResultEnum, (LPSTR)"WNetEnumResource");

break;

}

}

while(dwResultEnum != ERROR_NO_MORE_ITEMS);

GlobalFree((HGLOBAL) lpnrLocal);

dwResult = WNetCloseEnum(hEnum);

if(dwResult != NO_ERROR) {

NetErrorHandler(hwnd, dwResult, (LPSTR)"WNetCloseEnum");

return FALSE;

}

return TRUE;

}


Last news from Greatis Software

Nostalgia .Net     Nostalgia .Net     .Net is powerful, but not all-powerful, so sometimes we need to use Win32 API for our .Net applications. It's simple enough with Platform Invoke if you have Win32 skill, but we do not always have time to dig the ancient documentation, declare the special types that are compatible with Win32, find the values of the Win32's constants and so on. Nostalgia .Net offers several simple-to-use classes, and components that will allow you to forget about the headache of Win32 and just use the power of Win32 in your application the same way as you use the native. Net classes.  More »

Recommended software for developers

Ultimate Pack for Delphi and C++ Builder     Ultimate Pack     Component pack for Delphi and C++ Builder that contains runtime form designer, runtime object inspector, print suite and much more for the very special price.  More »

Form Designer .Net     Form Designer .Net     Unique runtime form design solution that allows to edit any form in .Net WinForms application at runtime with full source codes for only 300 euro!  More »

Print Suite .Net     Print Suite .Net     Print Suite .Net is a set of components for easy printing texts, images and grids from your WinForms applications. Full C# source codes are available  More »

Gradient Controls .Net     Gradient Controls .Net     Gradient Controls .Net offers controls with gradient background feature. Labels, panels and so on... Full C# source codes are available  More »

iGrid     Greatis iGrid     iGrid plots drawing grid right over your desktop, so you can use it everywhere, with any drawing application without any special plugins for different graphic editors.  More »


All the contacts and projects

Dmitry Vasiliev (just.dmitry)

Related Links

Software for Visual Studio .NET developers
Software for Delphi and C++ Builder developers
Software for Visual Basic 6 developers
Delphi Tips&Tricks
MegaDetailed.NET

More Online Helps

Win32 Programmer's Reference
Win32 Multimedia Programmer's Reference
OLE Programmer's Reference
Microsoft Windows Pen API Programmer's Reference
Microsoft Windows Sockets 2 Reference
Microsoft Windows Telephony API (TAPI) Programmer's Reference
Unix Manual Pages

Free Tech Secrets ;) Copyright © 2008-2012 Free Tech Secrets ;) greatis just4fun network just4fun