Free Web Hosting | free host | Free Web Space | BlueHost Review

WST 4 - Windows NT Services
Home Up Interests Downloads (New) My Resume Links Feedback

 

Interests
Downloads (New)
My Resume
Links
WST
Feedback

Hi All,

Today we will have a look at Windows NT Services (Win32 Services), what they are, how they work and how to develop them.

A Windows Service is a special kind of application, more complex than a normal Win32 application but also more powerful and privileged than it. We can look at some of the differences between these two to understand them. A normal application must have at least ONE thread to run. A windows service requires at least TWO threads to run. A normal application runs in the current user account (it has all the privileges that the currently logged on user has, and nothing more). A service however, runs in the local system account, hence it has more privileges.

That is also the reason why an application is terminated when the user who launched it, logs out, however, a service starts running before a user logs in, and keeps running even when a user has logged out. Apart from these, services are not supported on 9x/ME platforms.

Services are run by a "Service Control Manager" (SCM, pronounced scum). This is the OS component which is responsible for starting/stopping the services, installing/uninstalling the services etc.

A service must have at least two threads and there needs to be some synchronization among them, and this makes the service development, a bit more complex. One of the service thread is the main service thread, just like any other application. It's usually called, service main and implements the major portion of the service functionality. The other thread is a command handler, which performs different actions on the service based upon requests from SCM (such as stopping the service etc.) Hence there needs to be some way of communication between service handler thread, and service main thread, so that when SCM wants to stop the service, the handler thread, can some way communicate this to the main thread.

Since the services run in local system account, they are more privileged. But they have some restrictions also, such as a service can usually not interact with the current user's desktop. Hence, a service cannot display GUI elements (windows, dialogs, message boxes etc) to the user. This can however, be enabled from SCM. You can view different services from the services snap in (execute services.msc from command prompt or run). You can see all the installed services and their properties from here. You can start/stop/pause a service set it's start options (automatic, manual ) etc.

As in any other case, MS provides API's for managing services too. Hence there are two sets of API's, one for SCM operations and another for services itself. You can explore about SCM API's in MSDN. Take a look at OpenSCManage() and it's group of API's.

Now for developing a service, you just require three special functions. Everything else, is the normal Win32 application stuff (WinMain etc.). These API's are StartServiceCtrlDispatcher(), RegisterServiceCtrlHandler, and SetServiceStatus(). StartServiceCtrlDispatcher() is the API' which starts the service main thread. You usually call it from the WinMain of your Win32 application. This takes a table of service name and service main entries.

A single win 32 application can have multiple services. So this table should have one entry for each of the service.

This function does not return unless all the services in the executable have stopped. You can look at this function in MSDN. RegisterServiceCtrlHandler() function registers and starts the so called service handler (or control handler) thread which is called by SCM for different service operations (start/stop service etc.). SetServiceStatus() is the API which let's the SCM know the current status of the service. The service calls this at different stages of it's life cycle to keep the SCM updated with it's status. You can refer to MSDN for more details on these API's.

Once your service is developed, you need to register it with SCM. CreateService() api does this for you. For starting/stopping the services there are StarService() and ControlService() api's.

There are service related samples in Platform SDK, which you can refer for more information about services.

There are some short cut methods too, for developing service, for example there is a service active X control which you can use, similarly, ATL provides a simple way for developing a COM service. Although these methods speed up the development, they incur a lot of overheads and don't give you a finer control over the development of services. Hence for system side development, these methods should be avoided.

We have book in our library titled "Developing Server Side Applications", this has a complete chapter on service development with system side perspective. You can refer to this too.

Exercise:

1) Explore different service and SCM related API's

2) Write a simple Windows GUI application, mimicking the functionality of Windows services snap in.

3) Write a basic service which starts/stops and performs some useful operation.

4) Find out how services can be allowed to interact with user desktop, and how services can be run

in different user accounts rather than local system account.

This should be sufficient for this week, we will try to cover some other topic next week. If you have any questions, please do ask.

Thanks.

-Farooque

 

 

HyperCounter
Bpath Counter