SideBar    Classes for Networking and Services
DOWNLOAD THE CODE:
Download the Code 3771.zip

You place the string Microsoft Windows Network in the lpProvider member of the NETRESOURCE structure to start Services Monitor's enumeration process. The WNetEnumResource function fills in the other structure values after it starts enumeration. The dwScope, dwType, and dwUsage members of the NETRESOURCE structure define the same information as the first three WNetOpenEnum parameters. The dwDisplayType member provides additional information about a resource's type; it reports whether network resources are domains, servers, or shares. The lpLocalName member contains the names of any remapped local devices. The lpRemoteName member contains the name of the network resource, and lpComment contains a description of the resource. The WNetOpenEnum function uses its fifth parameter to return an enumeration handle that the WNetEnumResource function uses in subsequent calls.

At B, Listing 1 calls the WNetEnumResource function to enumerate the network resources. The WNetEnumResource function takes four parameters. The first parameter is the handle that the WNetOpenEnum function returns. The second parameter identifies the number of entries the WNetEnumResource function needs to return per call. The third parameter is a buffer that contains the NETRESOURCE structure that the WNetEnumResource function returns. The fourth parameter specifies the size of the third parameter's buffer.

The NETRESOURCE structure that the WNetEnumResource function returns is a pointer. Listing 1's CopyMemory function copies the NETRESOURCE information from the buffer pointer into a structure so that Services Monitor can use the information in VB.

At C, Listing 1 uses the dwDisplayType member of the NETRESOURCE structure to determine whether the resource is a domain, server, or shared disk or printer. A VB Select statement then adds the structure information to the collection for that type of resource. If the NETRESOURCE structure contains domain information, the Select statement uses the structure information to create a new CDomain object, then passes the structure information as a parameter to the CDomain object's Init function. If the NETRESOURCE structure contains server information, the Select statement creates a CServer object and passes the structure information to the CServer class Init function. Services Monitor uses the CDomain and CServer objects to display resource information in the tree view. At D, Listing 1 uses the WNetCloseEnum function to close the enumeration.

The same NETRESOURCE structures that Services Monitor uses to represent domains can enumerate servers, the children of domains. After the WNetEnumResource function enumerates a resource, the third parameter in the calling list passes a pointer that identifies the NETRESOURCE structure to the WNetOpenEnum function. The pointer tells the WNetOpenEnum function to move to the next level of the hierarchy and open an enumeration for the resource's children. Services Monitor uses the same functions and structures to enumerate every level of the network resource hierarchy, so the WNetOpenEnum and WNetEnumResource routines must be common code that works for all the hierarchy's levels.

Listing Services
The NT SCM subsystem starts automatically when a system boots. The SCM reads information from a database in the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services Registry key about two types of services: installed services, such as the SQL Server or Workstation services, and driver services, such as 4mmDAT and Atdisk (kernel and file system drivers). This Registry key contains information about each service's security context, where its executable is, whether it starts automatically or manually, whether its failure is critical to the system, and the names of other services it depends on. Services Monitor monitors only installed services, but you can alter the code to also monitor driver services.

When a user expands a domain, Services Monitor retrieves a list of installed services from every server in the domain. Listing 2 shows the code that obtains the list of installed services for each remote server.

At A, Listing 2 uses the OpenSCManager API call to list a machine's services. The OpenSCManager function takes three parameters. The first parameter contains the server name that the WNetEnumResource function returns in Listing 1. The second parameter is a string that identifies the services database for Services Monitor to open. The value "ServicesActive" tells the OpenSCManager function to open the database of active services. The third parameter specifies the type of permissions Services Monitor needs to access the target machine's SCM database; the value SC_MANAGER_ENUMERATE_SERVICE defines the access rights that Services Monitor needs. When the OpenSCManager function completes, it returns a handle that the EnumServicesStatus function uses to create a buffer of ENUM_SERVICE_STATUS structures.

At B, Listing 2 shows the EnumServicesStatus function, which takes eight parameters. The first parameter is the handle the OpenSCManager function returns. The second parameter is a constant that specifies the type of services the EnumServicesStatus function needs to poll. The value SERVICE_WIN32 tells the function to list installed services rather than driver services. The third parameter further specifies the type of services the function returns. The value SERVICE_STATE_ALL tells EnumServicesStatus to list both active and inactive services.

The fourth parameter is the buffer in which EnumServicesStatus places an array of the ENUM_SERVICE_STATUS structures the function returns. The ENUM_SERVICE_STATUS structure is

Type ENUM_SERVICE_STATUS
lpServiceName
lpDisplayName
ServiceStatus
As Long
As Long
As SERVICE_STATUS
End Type
Prev. page     1 [2] 3     next page



You must log on before posting a comment.

If you don't have a username & password, please register now.

Reader Comments

Hi, would you also know a way to do the same but for tasks/processes (like winword.exe) on a network server?

Simon van Wijlen

Did you try this on XP SP2? When reading entries with EnumServicesStatusEx() on XP Prof SP2 I've found after the last bunch of entries the BytesReturned is 0 - but GetLastError() still returns ERROR_MORE_DATA. - djm

Anonymous User

 
 

ADS BY GOOGLE