Home Search Contact us About us
Title Network resource enumeration
Summary A simple function to enumerate the machines and resources on a network. It recursively expands the network exposing all reachable items.
Contributor John McTainsh
Published 8-Nov-2000
Last updated 8-Nov-2000
Page rating   100% for 2 votes Useless Brilliant

Description.

It is often necessary for a user to browse the network to identify a machine or shared resource. The following function allows the network resources to be enumerated and expanded like branches on a tree.

How to use it.

You MUST add Mpr.lib to the build librarys otherwise you will get a link error. To do this select Project->Settings to open the Project settings dialog. Select the Link tab and from the drop down box select the General Catagory. In the Object/Link modules edit type in mpr.lib

The following function starts an enumeration of the newtork starting at the root. If you have a pointer to a NETRESOURCE it can be used in place of NULL to expand that branch of the tree. In this example EnumNetwork calls itself (a bad idea) to expand the branches. Run the application in DEBUG to view the output in the trace log.

    EnumNetwork( NULL, _T(" - ") );

The Enumerate function.

This is the actual enumeration function. It first opens the network resource then enumerates it. Each enumeration is displayed in the TRACE then used to re-enter this function to expand itself.

// ***************************************************************************
//DESCRIPTION:
//        Enumerate the given network resource. 
//PARAMS:
//        pNetResource    NULL to enumerate root or a pointer
//                        to a NETRESOURCE to enumerate.
//        sIndent         Used only for display of data it allows
//                        re-enternat calls to be pushed further across page.
//RETURN:
//        true if the resource has sub items.
//CREATED:
//        8-11-2000, 8:20:31 by john@mctainsh.com
// ***************************************************************************
bool EnumNetwork( NETRESOURCE *const pNetResource, CString sIndent )
{
    //TRACE( _T("EnumNetwork( %p )\n"), pNetResource );

    //
    //Setup
    //
    DWORD dwResult;      
    HANDLE hEnum;      
    DWORD cbBuffer = 16*1024;
    DWORD cEntries = 0xFFFFFFFF;      
    LPNETRESOURCE lpnrDrv;      
    DWORD i;
    dwResult = WNetOpenEnum(
                pNetResource ?  RESOURCE_GLOBALNET  : RESOURCE_CONTEXT,
                RESOURCETYPE_ANY,
                0,                               
                pNetResource,
                &hEnum );   
    
    //Was the read sucessfull
    if (dwResult != NO_ERROR)      
    {
        TRACE( _T("%s *** ERROR %d - Cannot enumerate network\n"), 
            sIndent, dwResult );
        return false;
    } 
    
    //
    //Get items until no more remain.
    //
    do      
    {
        lpnrDrv = (LPNETRESOURCE) GlobalAlloc( GPTR, cbBuffer );
        dwResult = WNetEnumResource( hEnum, &cEntries, lpnrDrv, &cbBuffer );
        if (dwResult == NO_ERROR)         
        {
            //Scann through the results
            for( i = 0; i < cEntries; i++ )            
            {
                CString sNameRemote = lpnrDrv[i].lpRemoteName;
                if( sNameRemote.IsEmpty() )
                {
                    sNameRemote = lpnrDrv[i].lpComment;
                }

                //Save the resource information
                TRACE( _T("%s %s\n"), sIndent, sNameRemote );
                    
                //Here is where we delve deeper into the tree by
                //calling our self with current NETRESOURCE.
                //We don't bother with shares as they  are branch ends.
                //and can be enumerated with CFileFind.
                if( lpnrDrv[i].dwDisplayType != RESOURCEDISPLAYTYPE_SHARE )
                    EnumNetwork( &(lpnrDrv[i]), sIndent + _T("  ") );
            }
        }         
        GlobalFree( (HGLOBAL) lpnrDrv );      
        if( dwResult != ERROR_NO_MORE_ITEMS && dwResult != NO_ERROR )
        {            
            TRACE( _T("%s *** ERROR %d - Cannot complete network enumeration\n"),
                sIndent, dwResult );
            break;         
        }
    }
    while( dwResult != ERROR_NO_MORE_ITEMS );      

    //
    //Let go and go home
    //
    WNetCloseEnum(hEnum);   
}
Comments Date
Home Search Contact us About us