-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuServiceController.pas
174 lines (156 loc) · 6.18 KB
/
uServiceController.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
unit uServiceController;
// The following example demonstrates the use of the TCnServiceController Component
// to control the SimpleService service example.
interface
uses
{$IF CompilerVersion > 22}
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.TypInfo,
{$ELSE}
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, TypInfo,
{$IFEND}
CNClrLib.Control.EnumTypes, CNClrLib.Control.Base, CNClrLib.Component.ServiceController,
CNClrLib.Component.EventLog;
type
TSimpleServiceCustomCommands = (ccStopWorker = 128, ccRestartWorker, ccCheckWorker);
TfrmServiceCtrl = class(TForm)
Button1: TButton;
CnServiceController1: TCnServiceController;
Memo1: TMemo;
CnEventLog1: TCnEventLog;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmServiceCtrl: TfrmServiceCtrl;
implementation
{$R *.dfm}
uses Rtti, CNClrLib.Host.Helper;
procedure TfrmServiceCtrl.Button1Click(Sender: TObject);
var
scServices: _ServiceControllerArray;
elec: _EventLogEntryCollection;
I, X: Integer;
{$IF CompilerVersion <= 23}
argArray: TArray<String>;
{$IFEND}
begin
scServices := TCnServiceController.GetServices;
for I := 0 to scServices.Length - 1 do
begin
if scServices[I].ServiceName = 'Simple Service' then
begin
// Display properties for the Simple Service sample
// from the ServiceBase example.
CnServiceController1.ServiceName := 'Simple Service';
{$IF CompilerVersion > 23}
Memo1.Lines.Add('Status = ' + TRttiEnumerationType.GetName(CnServiceController1.Status));
{$ELSE}
Memo1.Lines.Add('Status = ' + GetEnumName(TypeInfo(TServiceControllerStatus), Ord(CnServiceController1.Status)));
{$IFEND}
Memo1.Lines.Add('Can Pause and Continue = ' + BoolToStr(CnServiceController1.CanPauseAndContinue, True));
Memo1.Lines.Add('Can ShutDown = ' + BoolToStr(CnServiceController1.CanShutdown, True));
Memo1.Lines.Add('Can Stop = ' + BoolToStr(CnServiceController1.CanStop, True));
if CnServiceController1.Status = TServiceControllerStatus.scsStopped then
begin
CnServiceController1.Start;
while CnServiceController1.Status = TServiceControllerStatus.scsStopped do
begin
Sleep(1000);
CnServiceController1.Refresh;
end;
end;
// Issue custom commands to the service
// enum TSimpleServiceCustomCommands
// (ccStopWorker = 128, ccRestartWorker, ccCheckWorker);
CnServiceController1.ExecuteCommand(Ord(TSimpleServiceCustomCommands.ccStopWorker));
CnServiceController1.ExecuteCommand(Ord(TSimpleServiceCustomCommands.ccRestartWorker));
CnServiceController1.Pause();
while CnServiceController1.Status <> TServiceControllerStatus.scsPaused do
begin
Sleep(1000);
CnServiceController1.Refresh;
end;
{$IF CompilerVersion > 23}
Memo1.Lines.Add('Status = ' + TRttiEnumerationType.GetName(CnServiceController1.Status));
{$ELSE}
Memo1.Lines.Add('Status = ' + GetEnumName(TypeInfo(TServiceControllerStatus), Ord(CnServiceController1.Status)));
{$IFEND}
CnServiceController1.Continue();
while CnServiceController1.Status = TServiceControllerStatus.scsPaused do
begin
Sleep(1000);
CnServiceController1.Refresh;
end;
{$IF CompilerVersion > 23}
Memo1.Lines.Add('Status = ' + TRttiEnumerationType.GetName(CnServiceController1.Status));
{$ELSE}
Memo1.Lines.Add('Status = ' + GetEnumName(TypeInfo(TServiceControllerStatus), Ord(CnServiceController1.Status)));
{$IFEND}
CnServiceController1.Stop();
while CnServiceController1.Status <> TServiceControllerStatus.scsStopped do
begin
Sleep(1000);
CnServiceController1.Refresh;
end;
{$IF CompilerVersion > 23}
Memo1.Lines.Add('Status = ' + TRttiEnumerationType.GetName(CnServiceController1.Status));
CnServiceController1.Start(['ServiceController arg1', 'ServiceController arg2']);
{$ELSE}
Memo1.Lines.Add('Status = ' + GetEnumName(TypeInfo(TServiceControllerStatus), Ord(CnServiceController1.Status)));
SetLength(argArray, 2);
argArray[0] := 'ServiceController arg1';
argArray[1] := 'ServiceController arg2';
CnServiceController1.Start(argArray);
{$IFEND}
while CnServiceController1.Status = TServiceControllerStatus.scsStopped do
begin
Sleep(1000);
CnServiceController1.Refresh;
end;
{$IF CompilerVersion > 23}
Memo1.Lines.Add('Status = ' + TRttiEnumerationType.GetName(CnServiceController1.Status));
{$ELSE}
Memo1.Lines.Add('Status = ' + GetEnumName(TypeInfo(TServiceControllerStatus), Ord(CnServiceController1.Status)));
{$IFEND}
// Display the event log entries for the custom commands
// and the start arguments.
CnEventLog1.Log := 'Application';
elec := CnEventLog1.Entries;
for X := 0 to elec.Count - 1 do
begin
if (TClrStringHelper.IndexOf(elec[X].Source, 'SimpleService.OnCustomCommand') >= 0) or
(TClrStringHelper.IndexOf(elec[X].Source,'SimpleService.Arguments') >= 0) then
begin
Memo1.Lines.Add(elec[X].Message);
end;
end;
Memo1.Lines.Add('==============================================');
Memo1.Lines.Add('');
end;
end;
end;
//Output
//=================================================================
// This sample displays the following output if the Simple Service
// sample is running:
//=================================================================
//Status = Running
//Can Pause and Continue = True
//Can ShutDown = True
//Can Stop = True
//Status = Paused
//Status = Running
//Status = Stopped
//Status = Running
//4:14:49 PM - Custom command received: 128
//4:14:49 PM - Custom command received: 129
//ServiceController arg1
//ServiceController arg2
//=================================================================
//
end.