Desaware Home
Products    Purchase    Publishing    Articles   Support    Company    Contact    
Articles
.NET
COM

 

 

bluebar
Contact Desaware and order today

bluebar
Sign up for Desaware's Newsletter for the latest news and tech tips.

Control Windows Services Across your Network

by Stjepan Pejic
Copyright 2002 by Desaware Inc. All Rights Reserved.

One of the major new features in version 2.0 of the COM edition of the NT Service Toolkit is a service control object that allows you to access and control services. This article demonstrates how the library can be used to control services on remote systems as well.

Once Visual Basic programmers became familiar with services through our NT Service Toolkit, they started asking us for information about doing more with those services. They needed to perform tasks such as determining which other services were installed, which were active. They wanted to know what features their own service could enable, and wanted to be able to start and stop services from other programs. There are a series of Windows API functions dealing with the Service Control Manager, which is the system object in charge of controlling services on a machine. However, there is no Visual Basic documentation on how to use these functions outside of a chapter in Dan Appleman's "Win32 API Puzzle Book and Tutorial for Visual Basic Programmers" which covered the most basic functions. Even with documentation, the parameters of these functions were often difficult to use in Visual Basic.

To address this problem, Desaware has created a library that comes with the NT Service Toolkit version 2.0 that lightly wraps the API functions, giving you all their capability without having to deal with the hassles of directly declaring and calling the functions themselves. You can list services and retrieve or set the state and configuration for each. The sample program demonstrating the library is a fully featured service control program. With it you can view the information for every service on the machine and modify it at will.

But the Service Control Manager library is not limited to only controlling services on the local machine. You can also control services on other machines on the network. The only difference is that the machine name (with a preceding pair of backslashes) be passed as the first parameter of the InitializeSCManager method of the dwServiceManager object. Here is a sample program that shows how this is done:

' Service and computer information is hard coded to 
' demonstrate exactly what is passed to each function.
' This button is pressed to start a service.
Private Sub Command1_Click()
Dim manager As New dwServiceManager ' Control Manager object
Dim service As dwServiceObject ' represents a single service
Dim status As dwServiceStatus ' holds status information
Dim retval As Long
' The first parameter is the machine name of the computer 
' which contains the services you want to manage.
retval = manager.InitializeSCManager("\\testmachine", "", _
SC_MANAGER_CONNECT)
Set service = manager.OpenService("SCardSvr", "Smart Card", _
SERVICE_START Or SERVICE_STOP Or SERVICE_QUERY_STATUS)
' First we need to make sure the service is stopped before
' starting it.
Set status = service.QueryServiceStatus()
If (status.CurrentState <> SERVICE_STOPPED) then
  Exit Sub
End If
' This starts a service.
service.StartService
' This demonstrates how to wait until the service has
' actually started.
Do
  Set status = svc.QueryServiceStatus()
  Debug.Print status.CurrentState
Loop While (status.CurrentState <> SERVICE_RUNNING)
End Sub

' This button is pressed to stop a service.
' Service and computer information is hard coded to 
' demonstrate exactly what is passed to each function.
Private Sub Command2_Click()
Dim manager As New dwServiceManager ' Control Manager object
Dim service As dwServiceObject ' represents a single service
Dim status As dwServiceStatus ' holds status information
Dim retval As Long
' The first parameter is the machine name of the computer 
' which contains the services you want to manage.
retval = manager.InitializeSCManager("\\testmachine", "", _
SC_MANAGER_CONNECT)
Set service = manager.OpenService("SCardSvr", "Smart Card", _
SERVICE_START Or SERVICE_STOP Or SERVICE_QUERY_STATUS)
' First we need to make sure the service is started before
' trying to stop it.
Set status = service.QueryServiceStatus()
If (status.CurrentState <> SERVICE_RUNNING) then
  Exit Sub
End If
' This stops the service.
svc.ControlService SERVICE_CONTROL_STOP
' Wait until the service has actually stopped.
Do
  Set status = svc.QueryServiceStatus()
  Debug.Print status.CurrentState
Loop While (status.CurrentState <> SERVICE_STOPPED)
End Sub

In an actual program, you will probably want to call the InitializeSCManager and OpenService methods separately, as they take a few seconds when accessing a computer across a network. You should also include error checking, as you may encounter any number of network or security issues. Note that you may not have permission to perform all the actions on a network machine that you can on a local machine. The reduce the chance of security related errors, use only the flags associated with the functions you plan to call, and do not use wide-ranging ones like SC_MANAGER_ALL_ACCESS.

 

For notification when new articles are available, sign up for Desaware's Newsletter.

articles
Related Products:
 
Products    Purchase    Articles    Support    Company    Contact
Copyright© 2012 Desaware, Inc. All Rights Reserved.    Privacy Policy