Network Enumeration(2)

This class introduces minor modifications to a Network enumeration article by Joerg Koenig.

Great! However, I wanted to use it locally within a function. The problem is that if you create a CNetSearch object within a function, you don't have access to it within a callback function called when a network resource is hit (the object itself is used to get properties of a network resource). Also, it is not possible to use one callback function in several situations (for example, search for network resources on application startup or updating network resources while application is running). Of course, these problems can always be solved using the original implementations.

To solve these problems, I introduced minor modifications (marked in bold):

Step 1:

This step contains all the modifications (done in netsearch.h include file).

template <class T>
class CNetSearch : public CNetwork {
    typedef BOOL (T::*NsFnc)(CNetSearch*, NETRESOURCE &, DWORD);
    T *m_pTheObject;
    NsFnc m_NsFnc;
    DWORD Data;
    public:
        CNetSearch() : m_pTheObject(0), m_NsFnc(0) {}
        void Create(T * pObj, NsFnc fnc, DWORD data) {
            ASSERT(pObj != 0);
            ASSERT(fnc != 0);
            m_pTheObject = pObj;
            m_NsFnc = fnc;
            Data = data;
        }
    protected: // overridables
        virtual BOOL OnHitResource(NETRESOURCE & rNetRC) {
            return (m_pTheObject->*m_NsFnc)(this,rNetRC,Data);
        }
    virtual BOOL NetError(DWORD dwErrorCode, LPCTSTR lpszFunction) {
        switch(dwErrorCode) {
            case ERROR_BAD_NETPATH:
            case ERROR_NO_NETWORK:
                // minor errors: continue enumeration
                return TRUE;
        }
        // serious error: break enumeration
        return CNetwork::NetError(dwErrorCode, lpszFunction);
    }
};
That's all.

Step 2:

To use this class now, all you have to do (let's assume that you are doing it locally from a function) is following:
 

// Update argument is FALSE on initial serch and TRUE when updating
void CMyDocument::scanNetworkMachines(BOOL update)
{
    CNetSearch<CMyDocument> netWalker;
    netWalker.Create(this,onNetResourceHit,(DWORD)update);
    netWalker.Enumerate();
}
BOOL CMyDocument::onNetResourceHit(CNetSearch<CMyDocument> *net, NETRESOURCE& res, DWORD data)
{
    int ndx;
    if (net->IsServer(res)) {
        CString str = net->GetRemoteName(res);
        for (int ndx = 0; str[ndx] == '\\' ; ++ndx) ;
        if (ndx)
            str = str.Mid(ndx);
        str.MakeUpper();
        if (data == 0) {
            // This is an initial scan !!!
            // For example, create TComputer object and add it to the list
        } else if (data == 1) {
            // This is an update !!!
            // For example, search for a TComputer object in a list and if not found,
            // create a new one and add it to the list.
        }
    }
    return TRUE;    // continue enumeration 
}
 

Last updated: 17 May 1998.



Comments

  • can this enumerate the network hierrachy

    Posted by Legacy on 07/02/2002 12:00am

    Originally posted by: TARundeep

    i want to have hierarchy of netwrok !! domains workgroup etc.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • CentreCorp is a fully integrated and diversified property management and real estate service company, specializing in the "shopping center" segment, and is one of the premier retail service providers in North America. Company executives travel a great deal, carrying a number of traveling laptops with critical current business data, and no easy way to back up to the network outside the office. Read this case study to learn how CentreCorp implemented a suite of business continuity services that included …

Most Popular Programming Stories

More for Developers

RSS Feeds