To the main page...The list of my products...Some texts...Sample applications, tips, tricks...If you need support...
 

 
Self-starting configurable service
Step 3. Self starting/self stopping

Well, now the thing I promised in the forum - the self-starting service.

The idea: let's check the command line and if the /start switch is there then simply execute a service starting code. In other case let's check it for the /stop switch. Of course if the /start or /stop switches are found then application should be stopped after execution of starting/stopping code. So the application code should be modified as shown below:



 
 

 

 
 
 

 

 
 
 

 
 

 
 

 

 
 



var ServiceName: String;
begin
    Application.Initialize;

    if SvComFindParameter('start', ServiceName) then
    begin
        StartService(ServiceName);
        exit;
    end;

    if SvComFindParameter('stop', ServiceName) then
    begin
        StopService(ServiceName);
        exit;
    end;

    if SvComFindCommand('ReInstall') then
        raise Exception.Create('/REINSTALL switch is not supported');

    if SvComFindCommand('Install') then
        CreateNewServices
    else
        CreateOldServices;

    Application.Run;
end.

Note that the service name is not checked in this code. So our application can be used instead of "net start" command. To complete it the StartService and StopService methods should be implemented. These methods are standard and the similar code exists on many Delphi sites over the net.

Well, the StartService looks like:






 
 
 
 
 

 

 

 
 
 

 


 

 

procedure StartService(ServiceName: String);
var
    schService,
    schSCManager: DWORD;
    p: PChar;
begin
    p:=nil;
    schSCManager:= SvOpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
    if schSCManager = 0 then RaiseLastWin32Error;
    try

            schService:=SvOpenService(schSCManager,
                PChar(ServiceName),SERVICE_ALL_ACCESS);
            if schService = 0 then RaiseLastWin32Error;

        try
            if not SvStartService(schService,0,p) then 
                RaiseLastWin32Error;
        finally
            SvCloseServiceHandle(schService);
        end;
    finally
        SvCloseServiceHandle(schSCManager);
    end;
end;

and the StopService method is almost the same:






 
 
 
 
 

 

 

 
 
 

 


 

 

procedure StopService(ServiceName: String);
var
    schService,
    schSCManager: DWORD;
    p: PChar;
begin
    p:=nil;
    schSCManager:= SvOpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
    if schSCManager = 0 then RaiseLastWin32Error;
    try

            schService:=SvOpenService(schSCManager,
                PChar(ServiceName),SERVICE_ALL_ACCESS);
            if schService = 0 then RaiseLastWin32Error;

        try
            if not SvControlService(schService,SERVICE_CONTROL_STOP,SS) then 
                RaiseLastWin32Error;
        finally
            SvCloseServiceHandle(schService);
        end;
    finally
        SvCloseServiceHandle(schSCManager);
    end;
end;

Well, the second part of our example is finished and can be tested. Try to execute our example with the following switches in the command line:

SvComEx22.exe /start:Ex22SvcA

SvComEx22.exe /stop:Ex22SvcA

You will see that service starts and stops as required. There is no need in external controller like net or svcomsc commands. Note that our example still require the SvComSvc.dll on Win9x/ME systems to be executed. If you are a register user you can simply attach the sources of this dll to our example and remove this last external dll. As a result you will have completely independent, self starting, configurable and multi-service application.

On the next step we shall solve one more task: communication with service using Windows messages.

The source code of this step is available here (zip, 3.1kb).

<< | Index | Step 1 | Step 2 | Step 3 | Step 4 | Step 5 | >>
Add your comment | Read comments


 
© 1998-2001 Alexey Dynnikov
My ICQ # is 18267212