-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServiceMain.pas
87 lines (69 loc) · 2.12 KB
/
ServiceMain.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
{ ************************************************************************
NetTime is copyrighted by Graham Mainwaring. Permission is hereby
granted to use, modify, redistribute and create derivative works
provided this attribution is not removed. I also request that if you
make any useful changes, please e-mail the diffs to [email protected]
so that I can include them in an 'official' release.
************************************************************************ }
unit ServiceMain;
interface
uses
Windows, Messages, SysUtils, Classes, SvcMgr,
NetTimeThread, ConfigObj, Mutex, NetTimeCommon, WinSvc;
type
TNetTimeSvc = class(TService)
procedure ServiceStart(Sender: TService; var Started: Boolean);
procedure ServiceStop(Sender: TService; var Stopped: Boolean);
procedure DoExitNow(Sender: TObject);
private
tt: TNetTimeServer;
procedure WMEndSession(var Msg: TWMEndSession); message WM_ENDSESSION;
public
function GetServiceController: TServiceController; override;
end;
var
NetTimeSvc: TNetTimeSvc;
implementation
{$R *.DFM}
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
NetTimeSvc.Controller(CtrlCode);
end;
function TNetTimeSvc.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TNetTimeSvc.DoExitNow(Sender: TObject);
begin
Controller(SERVICE_CONTROL_STOP);
end;
procedure TNetTimeSvc.ServiceStart(Sender: TService; var Started: Boolean);
var
co: TConfigObj;
begin
if not GetExclusivity(ExNameServer) then
raise exception.create('Cannot load NetTime server: Another server is already running');
tt := TNetTimeServer.Create;
co := TConfigObj.Create;
try
co.ReadFromRegistry;
co.WriteToRunning(tt);
finally
co.Free;
end;
if (tt.Config.ServerCount = 0) then
raise exception.create('NetTime has not been configured');
tt.OnExitNow := DoExitNow;
tt.Start;
Started := true;
end;
procedure TNetTimeSvc.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
tt.Stop;
Stopped := true;
end;
procedure TNetTimeSvc.WMEndSession(var Msg: TWMEndSession);
begin
DoExitNow(Self);
end;
end.