Hi Friends
Hi Friends,
I have created window service using delphi below are its code
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,registry;
type
TService2 = class(TService)
procedure ServiceAfterInstall(Sender: TService);
procedure ServiceCreate(Sender: TObject);
procedure ServiceBeforeUninstall(Sender: TService);
procedure ServiceBeforeInstall(Sender: TService);
procedure ServiceShutdown(Sender: TService);
procedure ServiceStop(Sender: TService; var Stopped: Boolean);
procedure ServiceStart(Sender: TService; var Started: Boolean);
private
{ Private declarations }
public
function GetServiceController: TServiceController; override;
{ Public declarations }
end;
var
Service2: TService2;
implementation
{$R *.DFM}
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
Service2.Controller(CtrlCode);
end;
function TService2.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TService2.ServiceStart(Sender: TService;
var Started: Boolean);
begin
Started:=True;
end;
procedure TService2.ServiceStop(Sender: TService;
var Stopped: Boolean);
begin
Stopped:=True;
end;
procedure TService2.ServiceShutdown(Sender: TService);
begin
//
end;
procedure TService2.ServiceBeforeInstall(Sender: TService);
begin
// DisplayName:='AAA Test Service';
// Name:='Ax_Test_Service';
end;
procedure TService2.ServiceBeforeUninstall(Sender: TService);
begin
//
end;
procedure TService2.ServiceCreate(Sender: TObject);
var
myfile:TextFile;
begin
AssignFile(myFile, 'C:\Test.txt');
Rewrite(myfile);
CloseFile(myfile);
//DisplayName:='AAA Test Service';
// Name:='Ax_Test_Service';
end;
procedure TService2.ServiceAfterInstall(Sender: TService);
var
Reg: TRegistry;
myfile:TextFile;
begin
AssignFile(myFile, 'C:\Test.txt');
Rewrite(myfile);
Reg := TRegistry.Create(KEY_READ or KEY_WRITE);
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
if Reg.OpenKey('\system\currentcontrolset\services\Service2', false) then
begin
Reg.WriteString('Description', 'Test description');
Reg.WriteString('DisplayName', 'Test Display name');
Reg.CloseKey;
end;
finally
Reg.Free;
CloseFile(myfile);
end;
end;
end.
I have crated all its event afterinstall,create etc but none one is called at installation time and afterinstall is not called.
i am installing service like /install in CMD.
Could any one help to get out of this? actually i am trying to set my name and description while service installed.
I have created window service using delphi below are its code
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,registry;
type
TService2 = class(TService)
procedure ServiceAfterInstall(Sender: TService);
procedure ServiceCreate(Sender: TObject);
procedure ServiceBeforeUninstall(Sender: TService);
procedure ServiceBeforeInstall(Sender: TService);
procedure ServiceShutdown(Sender: TService);
procedure ServiceStop(Sender: TService; var Stopped: Boolean);
procedure ServiceStart(Sender: TService; var Started: Boolean);
private
{ Private declarations }
public
function GetServiceController: TServiceController; override;
{ Public declarations }
end;
var
Service2: TService2;
implementation
{$R *.DFM}
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
Service2.Controller(CtrlCode);
end;
function TService2.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TService2.ServiceStart(Sender: TService;
var Started: Boolean);
begin
Started:=True;
end;
procedure TService2.ServiceStop(Sender: TService;
var Stopped: Boolean);
begin
Stopped:=True;
end;
procedure TService2.ServiceShutdown(Sender: TService);
begin
//
end;
procedure TService2.ServiceBeforeInstall(Sender: TService);
begin
// DisplayName:='AAA Test Service';
// Name:='Ax_Test_Service';
end;
procedure TService2.ServiceBeforeUninstall(Sender: TService);
begin
//
end;
procedure TService2.ServiceCreate(Sender: TObject);
var
myfile:TextFile;
begin
AssignFile(myFile, 'C:\Test.txt');
Rewrite(myfile);
CloseFile(myfile);
//DisplayName:='AAA Test Service';
// Name:='Ax_Test_Service';
end;
procedure TService2.ServiceAfterInstall(Sender: TService);
var
Reg: TRegistry;
myfile:TextFile;
begin
AssignFile(myFile, 'C:\Test.txt');
Rewrite(myfile);
Reg := TRegistry.Create(KEY_READ or KEY_WRITE);
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
if Reg.OpenKey('\system\currentcontrolset\services\Service2', false) then
begin
Reg.WriteString('Description', 'Test description');
Reg.WriteString('DisplayName', 'Test Display name');
Reg.CloseKey;
end;
finally
Reg.Free;
CloseFile(myfile);
end;
end;
end.
I have crated all its event afterinstall,create etc but none one is called at installation time and afterinstall is not called.
i am installing service like
Could any one help to get out of this? actually i am trying to set my name and description while service installed.
Comments
Post a Comment