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

The ENUM_SERVICE_STATUS structure's members identify the service's name, the name Services Monitor will display for the service, and a nested structure called SERVICE_STATUS that contains information about the service's current status.

The fifth EnumServicesStatus parameter defines the size in bytes of the fourth parameter's buffer. The sixth parameter returns the number of bytes the buffer needs to hold the array of ENUM_SERVICE_STATUS structures. Services Monitor allocates a buffer size of 16KB, which is larger than necessary in most cases. The seventh parameter contains the number of services the EnumServicesStatus function returns. The eighth parameter tells Services Monitor how far down the target SCM database's list of services to start enumerating services. This value needs to be 0.

When the EnumServicesStatus function call completes, it returns a new value in the eighth parameter. If the function places a 0 in the eighth parameter, the buffer is large enough to enumerate all the target machine's services. If the function returns a non-zero value in the eighth parameter, the call failed, and you need to check the value of the VB Err object's LastDLLError property. If your computer's last DLL error is ERROR_MORE_DATA, the EnumServicesStatus buffer is too small, and the eighth parameter's value identifies the entry in the SCM database's services list where Services Monitor needs to resume enumeration when the buffer has space for the remaining data. When Services Monitor encounters the ERROR_MORE_DATA error, it increases the size of the buffer and tries the EnumServicesStatus call again. Services Monitor continues to try the EnumServicesStatus function call until the buffer is large enough and the call completes successfully or the call returns an error other than ERROR_MORE_DATA. Any other LastDLLError entry indicates that a more serious error prevents Services Monitor from enumerating services; if it encounters an error other than ERROR_MORE_DATA, Services Monitor exits.

At C, Listing 2 processes the array of ENUM_SERVICE_STATUS structures that the EnumServicesStatus function returns and creates a CService object for each service the EnumServicesStatus function adds to the colServices collection. As Services Monitor processes the arrays, it uses the Tree View control to display each server's list of services and each service's status. At D, Listing 2 uses the CloseServiceHandle function to close the handle to the SCM database.

Updating Services' Status
When the Services Monitor's polling timer expires, the application queries the status of each selected service using the QueryServiceStatus API. Listing 3, page 193, shows the code that queries services' status.

At A, Listing 3 uses the OpenSCManager function to connect to the SCM database. The value SC_MANAGER_CONNECT in OpenSCManager's third parameter tells the function to open a connection.

At B, the OpenService function uses the handle that the OpenSCManager function returns to acquire a handle to a service. The OpenService function takes three parameters. The first parameter is the handle to the SCM database that the OpenSCManager function returns. The second parameter is the service's short name. The third parameter is a constant that specifies the permission level the SCM operation needs. The value SERVICE_QUERY_STATUS gives the OpenService function permission to return the service's status.

At C, Listing 3 uses the QueryServiceStatus function to retrieve the service's current status. The QueryServiceStatus function takes two parameters. The first parameter is the service handle that the OpenService function returns, and the second parameter is a structure that contains the service's current status. At D, Listing 3 uses the CloseServiceHandle function to close the handles to the SCM database and the service.

Services Monitor's Functionality
This article uses the WNet and SCM APIs to build a Services Monitor application. You can immediately put this utility to work on your network or customize the code to fit your needs. Services Monitor provides a display and alarm when a service fails, but you can modify the code to perform other actions, such as send email or attempt to restart the failed service.

NT 5.0 will include another method for querying the status of services on networked systems: Active Directory Services. Nevertheless, Services Monitor offers service monitoring functionality, and the WNet and SCM APIs will continue to support NT users for the foreseeable future.

End of Article

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