The Assembly Programming Master Book

Now, it is time to consider the service structure and describe several API functions used for controlling services.

From the operating system's point of view, the service has the standard structure of the executable module. However, it starts in a way different from the standard one and has the following three distinct parts :

Well, that's all that you need to know about the service structure. Now, it is necessary to describe the API functions used for working with services.

The StartServiceCtrlDispatcher function was already briefly described. In the next section, an example illustrating its use will be provided.

The RegisterServiceCtrlHandler function registers the command handler procedure. It returns the procedure handle. The function receives the following parameters:

The SetServiceStatus function sets the service status. It accepts the following parameters:

The preceding functions are required functions, which must be present in the service program. In the simplest case, the command handler will contain only the SetServiceStatus function, using which the service status will be determined depending on the received command. The structure of all components of a service will be presented in Listing 21.1.

However, the previously described API functions are not sufficient to make the service operate. To achieve this, the service must already be registered in the services database, which is Stored under the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services key of the system registry. Only after that can the service start. Often, a separate program is used for registering the service in the services database. This approach is the one chosen for the program presented in Listing 21.1. However, most services are organized so that the operations carried out over the service depend on the command-line parameters.

Consider how the service is registered in the system registry. To achieve this, it is first necessary to open the services database, which is a part of system registry.

The OpenSCManager function opens the services database. It accepts several parameters.

If the function completes successfully, it returns the database handle. If the database has been opened successfully, then, after carrying out all required operations, it must be closed. For this purpose, use the closeServiceHandle function whose only parameter is the service database handle.

To register the service in the service database, use the CreateService API function. This function has the following 13 parameters:

Now, consider the procedures required to start the service programmatically. Note that the service must previously be registered using the CreateService function. The order of operations is as follows:

  1. Open the services database using the OpenSCManager function.

  2. Open the service using the OpenService function.

  3. Start the service using the StartService function.

  4. Close the services database.

Now, consider some functions not described yet. The OpenService function accepts three parameters:

The StartService , like the previous function, also accepts three parameters. The first parameter is the handle returned by the OpenService function. The second and third parameters are parameters passed to the service. Usually, these values are assumed to be zero, which means that this feature is not used.

Finally, it is necessary to consider another important aspect of service programminghow to programmatically delete the service from the system registry. To achieve this, it is necessary to carry out the following operations.

  1. Open the services database.

  2. Open the required service.

  3. If this service is started, it must be stopped before deletion. This can be achieved using the Controlservice function. If the service is not running, nothing bad will happen.

  4. Delete the service from the registry using the DeleteService function.

  5. Close the services database.

Now, it is time to describe functions that have not been covered yet The Controlservice function has three parameters:

The DeleteService function accepts only one parameterthe handle to the previously opened service.

Категории