IntroductionQ: What is a Windows Service ?
A: A Windows service is an EXE specially designed to communicate with the SCM of
Win NT/2000.The SCM maintains a database of installed services and driver services
and provides a unified and secure means of controlling them.The SCM is started at
system boot and it is a remote procedure call (RPC) server.
A service application conforms to the interface rules of the SCM. It can be started
automatically at system boot, by a user through the Services control panel applet,
or by an application that uses the service functions.Services can execute even
when no user is logged on to the system. A driver service conforms to the device
driver protocols. It is similar to a service application, but it does not interact
with the SCM. Here, we use the term service to refer to a service application.
Q: What is SCM and what exactly does it do?
A: The term SCM is an acryonym for Service Control Manager. The SCM maintains a
database of installed services and driver services, and provides a unified and
secure means of controlling them. The database includes information on how each
service or driver service should be started. It also enables system administrators
to customize security requirements for each service thus controlling access to the
service.
Q: Where can i possibly use the SCM ?
A: There are 3 types of programs that use the SCM. They are :-
a) Service program : A program that provides executable code for one or more
services.Service programs use functions that connect to the SCM and send status
information to the SCM.
b) Service configuration program : A program that queries or modifies the
services database. Service configuration programs use functions that open the
database, install or delete services in the database, and query or modify the
configuration and security parameters for installed services. Service
configuration programs manage both services and driver services.
c) Service control program : A program that starts and controls services and
driver services. Service control programs use functions that send requests to the
SCM,which carries out the request.
Q: What does this article concenterate on ?
A: In our case, we are going to write a Service Control Program. We are going
to write a manager that can Start,Stop,Pause,Install or UnInstall a service.
All this with the help of a simple UI.
Implementation
This Tutorial describes how to write a Service Control Program using MFC. The
source code and a sample application using the source can be found at the bottom
of this article.
The ServiceHandler class
The ServiceHandler class is the base class containing the complete
functionality for the Service Handling functions. Most of the routines
have gone into it and they've been wrapped up neatly into a small set
of just 7 Function calls to handle the entire functionality. An additional
set of 2 data structures are also exposed in order to retrieve the
list of Services and the individual details of each Service. Lets go
through them one by one.
At its root, there is a structure called ServiceInformation
which is defined as follows.
struct ServiceInformation
{
char DisplayName[128];
char ServiceName[256];
char ServiceState[32];
DWORD dwProcessID;
char ServiceDescription[512];
}ServiceInfo;
This structure holds the details about each service found.
As there might be many services on your machine, a vector array is
created to hold the list of structures. This is defined as follows:
// Array to hold the ServiceInformation Structures. i.e. it contains the
// details of all services that were enumerated by the EnumServices function.
vector ServiceInfoVectArray;
Functions Exposed in the ServiceHandler class
The functions are split into two categories. One is for enumerating
the list of services on your machine and the other category is to manipulate
a service.
Enumeration Category
The Enumeration category contains only one function called EnumServices.
It enumerates the list of all services and stores them in a vector array called
the ServiceInfoVectArray. The return value indicates the number of
services found.
An example for its usage would be as follows :-
// Assuming that you have created a member variable for the
// CServiceHandler class and named it ServiceHandler
ServiceHandler.EnumServices();
ServiceHandler.ServiceInfo = ServiceHandler.ServiceInfoVectArray.at(nIndex);
CString strServiceName(_T(""));
for(int nIndex = 0; nIndex < ServiceHandler.ServiceInfoVectArray.size(); nIndex++)
{
strServiceName = ServiceHandler.ServiceInfo.ServiceName;
AfxMessageBox(strServiceName);
}
Manipulation Category
It consists of 6 functions that allow you to manipulate the Services.
They are as follows :
1) StartService, 2) StopService, 3) PauseService,
4) ContinueService 5) InstallService and 6) DeleteService.
Lets see them one by one. The function names are self-explanatory and
we need not delve much into them. We shall see the prototypes, the action and
the return values of each one.
The functions StartService, StopService, PauseService, ContinueService
and DeleteService all have the same prototype such as :
1) BOOL <FunctionName> (LPCTSTR szDisplayName);
The functions manipulate a service using the display name of the service
as a parameter.If you are planning to use a service manager to manipulate a
service, you can use the display name enumerated by the EnumServices function.
It returns TRUE if succeeded and FALSE if failed. It also informs the
user with a message box if anything failed.
Assuming that you have created a member variable for the CServiceHandler
class in your header file and named it as ServiceHandler,
An Example for the usage of any one of those functions would be as follows :-
char szCurSelService[MAX_PATH];
strcpy(szCurSelService,"MyOwnService");
ServiceHandler.StartService(szCurSelService);
The only exception is the InstallService which has a prototype like this.
BOOL InstallService(LPCTSTR szBinaryPath,LPCTSTR szServiceName,LPCTSTR szDisplayName);
The parameters are the Fully Qualified Path to the Executable, The Service Name
and the Service Display name. It returns TRUE if the service is installed successfully
or FALSE if it fails.
Downloads
Source Code (4 kb)
Demo Application (26 kb)
Links
A good article at codeproject.com
Creating a Windows NT/Windows 2000 Service
Simple Service Manager from codeguru.com
WinNT/2000 Service Controller from codeguru.com
Conclusion :
THATS ALL FOLKS. The end of a simple tutorial for using the SCM. Play around with the
Services, but be careful that you dont delete important ones :) All Luck.
|