When a workstation is switched on in our Internet cafe we need to have a way of ensuring that the client application, which communicates with the server, is also started. There are two ways of doing this: manually and automatically. You will probably want the application to load automatically. Windows services will let us accomplish this task. Keep reading to find out what they are and how to use them.
If you choose to load the application manually, a staff member in our Internet café must go to each workstation, switch it on and then start the client application. This is fine if the Internet café has only a couple of workstations, however if it has more than, say, twenty workstations, this can be a bit overwhelming.
If the application is loaded automatically, then it starts by itself when a workstation is switched on. It just means that the only thing a staff member will need to do is switch on the workstations, and the client application will then be loaded as part of the startup process. This is a small improvement, but nevertheless, it is an improvement.
This is where the Windows service comes in. In the following sections we will discuss what a Windows service is and then create one for our client application.
The Windows service
What is a Windows service? It is basically an application that runs in the background. By this I mean that the application cannot be seen on the desktop while it is carrying out its functions. At any given time there are more than five processes running on your computer. You can check this by pressing cntrl+alt+del, which will bring up the task manager (on Windows) and then selecting the Processes tab. You will see a great many services running.
So how do we create one? Delphi makes it very easy to create such a service. Select the menu items File|New| Other, select "Service Application" and click OK. You now have the framework for a service application which includes a TService class. It should look something like this:
function TService1.GetServiceController: TServiceController;
Result := ServiceController;
The TService class is where we will write our code. It also has a number of properties which you can see in the Object Inspector. Among these are (in order of appearance):
Displayname - The DisplayName is used for displaying when you use the Control Panel, Administration, Services applet.
Interactive – This property simply checks to see if the service is going to interact with a user. Most services don’t, so you can safely set this to false.
Servicestartname - You can specify an account name in this property and a password in the Password property to specify which account the service should log on as.
That requires that you know at design time the account name and password. Most of us won't know this. Just leave ServiceStartName and Password empty. You can always enter an account name and password in the service properties after the service has been installed.
A service runs as a specific user. This means that a service has the same access permissions to different things, such as folders, as the account under which the service runs. Many services run as "Local System" unless you specify a specific username when installing the service. For many things, "Local System" is sufficient; however, if the service needs to have access to things such as a folder on a network drive/share (which are normally user/password protected) you may need to specify an account which has been granted access to the network share.
Name - It's important to choose a good name, because when installing the service this name is automatically used to create a key in the registry under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services. Giving your service a name that is already registered in the registry will cause a conflict in the registry, so make sure that the name is unique.