-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUnit1.pas
176 lines (155 loc) · 4.72 KB
/
Unit1.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
175
176
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Menus, ComCtrls;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
Start1: TMenuItem;
Stop1: TMenuItem;
Timer1: TTimer;
Pause1: TMenuItem;
Continue1: TMenuItem;
ListView1: TListView;
Refresh1: TMenuItem;
Memo1: TMemo;
Recreate1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure ChgEtatService(Sender: TObject);
procedure Rafraichir(Sender: TObject);
procedure Recreate1Click(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
SC : THandle;
end;
var
Form1: TForm1;
implementation uses WinSvc;
{$R *.dfm}
const SrvAccess = SERVICE_INTERROGATE or SERVICE_PAUSE_CONTINUE or
SERVICE_START or SERVICE_STOP or SERVICE_QUERY_STATUS;
function CurrState(State : dword) : string;
Begin
Case State Of
SERVICE_STOPPED: Result := 'STOP';
SERVICE_START_PENDING: Result := 'starting';
SERVICE_STOP_PENDING: Result := 'stopping';
SERVICE_RUNNING: Result := 'RUN';
SERVICE_CONTINUE_PENDING: Result := 'continuing...';
SERVICE_PAUSE_PENDING: Result := 'pausing...';
SERVICE_PAUSED: Result := 'PAUSE';
End;
End;
// Inutilisé : ErrSt et StartSt
function ErrSt(Err : dword) : string;
Begin
Case Err of
SERVICE_ERROR_IGNORE: Result := 'ignore';
SERVICE_ERROR_NORMAL: Result := 'normal';
SERVICE_ERROR_SEVERE: Result := 'SEVERE';
SERVICE_ERROR_CRITICAL: Result := 'CRITICAL';
else Result := '?';
End;
End;
function StartSt(St : dword) : string;
Begin
Case St of
SERVICE_BOOT_START : Result := 'BOOT';
SERVICE_SYSTEM_START : Result := 'SYSTEM';
SERVICE_AUTO_START : Result := ' auto ';
SERVICE_DEMAND_START : Result := 'Manual';
SERVICE_DISABLED : Result := 'Disabled';
else Result := '?';
End;
End;
{ =========================================================================== }
procedure TForm1.FormCreate(Sender: TObject);
begin
SC := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
Recreate1Click(Sender);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
CloseServiceHandle(SC);
end;
procedure TForm1.ChgEtatService(Sender: TObject);
var x : Integer;
OS : THandle;
SrvName : string;
lpServiceStatus : _SERVICE_STATUS;
begin
Timer1.Enabled := False;
With ListView1, Items do
If Count > 0 Then
for x := 0 to Count-1 do
With Item[x] do
If Selected Then
Begin
SrvName := SubItems.Strings[0]; // ServiceInternalName
OS := OpenService(SC, PChar(SrvName), SrvAccess);
If Sender = Start1 Then
StartService(OS, 0, PChar(nil^))
else if Sender = Stop1 Then
ControlService(OS, SERVICE_CONTROL_STOP, lpServiceStatus)
else if Sender = Continue1 Then
ControlService(OS, SERVICE_CONTROL_CONTINUE, lpServiceStatus)
else if Sender = Pause1 Then
ControlService(OS, SERVICE_CONTROL_PAUSE, lpServiceStatus);
CloseServiceHandle(OS);
End;
Timer1.Enabled := True;
end;
procedure TForm1.Rafraichir(Sender: TObject);
var Tbl : array[1..500] of TEnumServiceStatus;
card, card2, nbsvc, x : cardinal;
function TrouverItem(Caption : string) : TListItem;
var n : Integer;
begin
n := Memo1.Lines.IndexOf(Caption);
With ListView1 do
TrouverItem := Items.Item[n];
end;
begin
card2 := 0;
nbsvc := 0;
EnumServicesStatus(SC, SERVICE_WIN32, SERVICE_STATE_ALL,
Tbl[1], SizeOf(Tbl), card, nbsvc, card2);
If nbsvc > 0 Then
for x := 1 to nbsvc do
With Tbl[x], ServiceStatus do
try
TrouverItem(lpServiceName).SubItems.Strings[1] :=
CurrState(dwCurrentState);
except
showmessage('Impossible de mettre à jour '+
lpServiceName+'. Recréez la liste !');
end;
end;
procedure TForm1.Recreate1Click(Sender: TObject);
var Tbl : array[1..500] of TEnumServiceStatus;
card, card2, nbsvc, x : cardinal;
begin
Timer1.Enabled := False;
// Enumération
ListView1.Clear;
Memo1.Clear;
card2 := 0;
nbsvc := 0;
EnumServicesStatus(SC, SERVICE_WIN32, SERVICE_STATE_ALL,
Tbl[1], SizeOf(Tbl), card, nbsvc, card2);
If nbsvc > 0 Then
for x := 1 to nbsvc do
With Tbl[x], ServiceStatus, ListView1.Items, Add do
Begin
Memo1.Lines.Add(lpServiceName);
Caption := StrPas(lpDisplayName);
SubItems.Add(StrPas(lpServiceName));
SubItems.Add(CurrState(dwCurrentState));
End;
Timer1.Enabled := True;
end;
end.