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

Make sure your network's vital services are functioning properly

Developers build 32-bit applications for Windows NT networks on an infrastructure of NT services. Each service provides functionality at a system level or at a level that is available to other computers on the network. Examples of services that NT Server runs include the Computer Browser, Windows Internet Naming Service (WINS), remote procedure call (RPC), FTP, and SQL Server services.

Applications from Microsoft and third-party vendors rely on NT services to function properly. When an NT service stops running, its failure affects all the applications and users that depend on the service, reducing the users' capacity to work. Therefore, the sooner a developer or administrator learns about a service's failure, the better off everyone is.

You can interactively view and manage your local system's services using the Control Panel Services applet, but the applet doesn't let you view remote systems' services. Server Manager (which comes with NT Server and is available in all versions of Microsoft Windows NT Server Resource Kit) solves the remote management problem, but neither program produces warnings when a service fails.

To receive warnings when services on remote systems fail, you need the Services Monitor program that I build in this article. Services Monitor lists the domains and computers within your LAN or WAN, lets you choose services you want to monitor on any NT servers on the network, and alerts you when selected services fail.

Using Services Monitor
When you start Services Monitor, the program builds a tree-view display that contains an expandable icon for each domain and server in your network, as Screen 1, page 188, shows. If your network is large or widely distributed across a WAN, the tree-building process can take 30 minutes or more, depending on your network speed and the size of your WAN. If you expand the icon for a domain, Services Monitor builds a list of services for each server in that domain. You can alter Services Monitor to build the list of services at the same time you build the lists of domains and servers, but querying every server about its services increases your startup time considerably--an unnecessary inconvenience if you want to monitor services on only a few systems on the network.

Services Monitor uses Visual Basic's (VB's) Tree View control to list the domains, systems, and services on your network. You expand a domain icon to view the systems in the domain and expand a system icon to view the services that the system runs. Services Monitor displays a check box next to services that are running. You double-click a service's check box to instruct Services Monitor to monitor that service. NT services that aren't running are not available for monitoring. Services Monitor displays a red No Service circular icon next to each stopped service.

Services Monitor periodically polls the status of the services you select for monitoring. You use the Polling Interval text box to set the polling interval; polling intervals can range from 10 seconds to 60 seconds.

Services Monitor also lets you select the action that you want NT to perform when a monitored service stops. Services Monitor lets you set the program to produce a beep or play a .wav file on the machine where Services Monitor is running.

After you select monitoring options, click Start Polling to start Services Monitor. The program places an icon in the right corner of your taskbar and begins periodically polling services. You can right-click the Services Monitor icon to view the status of monitored services. The Show Service Statuses window, which Screen 2 shows, will appear. The window displays a stoplight icon next to each monitored service. When a service is active, its stoplight icon shows a green light; when a service stops, its stoplight icon shows a red light.

Inside Services Monitor
Services Monitor's core functionality rests on two types of Win32 APIs. The program uses the Windows Networking (WNet) APIs in mpr.dll to list the networked domains and servers. Then it uses the Windows Service Control Manager (SCM) APIs from advapi32.dll to poll the status of services running on local and remote NT systems. To make these APIs easy to reuse in other applications, I encapsulated them in VB classes. (For more information about the networking and service classes that Services Monitor uses, see the sidebar "Classes for Networking and Services.")

Retrieving Domain and Server Names
When Services Monitor starts, it uses the WNetOpenEnum API to open a network enumeration, then uses the WNetEnumResource API to traverse the enumeration. Services Monitor uses the com-bination of the WNetOpenEnum and WNetEnumResource functions to reveal a domain's hierarchy of resources.

When you start Services Monitor, the WNetOpenEnum function opens the network provider level, and the WNetEnumResource function lists the network's providers, such as Microsoft Windows Network, Novell NetWare, or Banyan Vines. At the next level, Services Monitor uses the WNetOpenEnum and WNetEnumResource functions to reveal the domains in the Microsoft Windows Network. The enumeration level beneath the domains lists each of the domains' networked computers that is running a server service and sharing resources on the network. This list of each domain's servers can include machines running NT Workstation, NT Server, Windows for Workgroups (WFW), Windows 95, and LAN Manager servers, but Services Monitor can monitor services only on the NT systems.

Listing 1, page 190, shows sample code that uses the WNetOpenEnum and WNetEnumResource functions. At callout A, Listing 1 contains the WNetOpenEnum function, which takes five parameters. The first parameter sets the scope of the enumeration. The value RESOURCE_GLOBALNET tells the WNetEnumResource function to display all the resources on the network. The second parameter specifies the type of network resources the WNetEnumResource function needs to list. The value RESOURCETYPE_ANY tells the function to include disk and printing resources. The third parameter further specifies the type of resource the WNetEnumResource function needs to return. The value 0 tells the function to return local and networked resources. The fourth parameter is the most critical. It is a structure that sets the level of the enumeration (i.e., network provider, domain, or server). The NETRESOURCE structure Services Monitor uses in parameter four is

Type NETRESOURCE
dwScope
dwType
dwDisplayType
dwUsage
lpLocalName
lpRemoteName
lpComment
lpProvider
As Long
As Long
As Long
As Long
As String
As String
As String
As String
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