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

 
Self-starting configurable service
Step 2. Saving configuration data into registry

The code below needs some comments. The first is the changes of CreateNewService method. As you see all data from the ini file are duplicated and stored in registry. Due to command line check (SvComFindCommand calls in the program body) the CreateNewService method is called on service installation only. After the service s installed the ini file can be modified or even deleted and it will not damage our service configuration.

Now when the configuration data are stored in the registry it is this data should be used when the service executes. So if the SvComFindCommand do not detects the /install switch in the command line then it means that the configuration data should be retriever from registry not from the ini file. The CreateOldServices call does it.






















 
 

 
 
 

 
 

 

 

 

 
 

 


 


 
 


 
 

 

 
 






 
 
 
 

 

 

 

 

 
 

 
 


 
 

 
 

 
 

 

 
 

program SvComEx22;

uses
    WinSvc,
    SvCom_WinSvc,
    SvCom_NTService,
    Windows,
    IniFiles,
    Registry,
    SysUtils,
    svcExample22 in 'svcExample22.pas' {svcServiceBase: TNtService};

{$R *.RES}

const RegKey='Software\SvCom\Example22';

procedure CreateNewServices;
var Ini: TIniFile;
    i, Count: Integer;
    Svc: TNTService;
    R: TRegistry;
begin
    Ini:=TIniFile.Create(ChangeFileExt(ParamStr(0), '.INI'));

    R:=TRegistry.Create;
    R.RootKey:=HKEY_LOCAL_MACHINE;
    R.OpenKey(RegKey,True);

    Count:=Ini.ReadInteger('Options','Count',1);
    R.WriteInteger('Count',Count);

    for i:=1 to Count do
    begin
        Svc:=TsvcServiceBase.Create(Application);

        Svc.Name:=Ini.ReadString('Options','Name'+IntToStr(i),'');

        if Svc.Name = '' then
            raise Exception.CreateFmt('Service name '+
                      'missed for the service %d',[i]);
        R.WriteString('Name'+IntToStr(i),Svc.Name);


        Svc.DisplayName:=Ini.ReadString('Options',
                                 'DisplayName'+IntToStr(i),'');

        if Svc.DisplayName = '' then
            raise Exception.CreateFmt('Service display name'+
                ' missed for the service %d',[i]);

        R.WriteString('DisplayName'+IntToStr(i),Svc.DisplayName);
    end;

    R.Free;

    Ini.Free;
end;


procedure CreateOldServices;
var R: TRegistry;
    i, Count: Integer;
    Svc: TNTService;
begin
    R:=TRegistry.Create;
    R.RootKey:=HKEY_LOCAL_MACHINE;
    R.OpenKey(RegKey,True);

    Count:=R.ReadInteger('Count');

    for i:=1 to Count do
    begin
        Svc:=TsvcServiceBase.Create(Application);

        Svc.Name:=R.ReadString('Name'+IntToStr(i));

        Svc.DisplayName:=R.ReadString('DisplayName'+IntToStr(i));
    end;

    R.Free;
end;

var ServiceName: String;
begin
    Application.Initialize;

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

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

    Application.Run;
end.

And one more thing: our application must not support the /reinstall switch. This switch assumes that uninstallation of existing service and its installation will be done in the Application.Run call. But we can't change the number of services and their names when this call is done so it makes impossible to modify the number of services or their names with /reinstall switch. So we simply disable this switch.

Well, now our example is ready for tests. You can change the number of services, modify their names and display names, you can install, uninstall, run and stop them. Of course you can add any other configuration parameter to your service. Create your own configurable service, it is easy, isn't it?

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

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


 
© 1998-2001 Alexey Dynnikov
My ICQ # is 18267212