1 { KOL MCK } // Do not remove this line!
3 unit MainControlKOLService
;
8 uses Windows
, Messages
, ShellAPI
, KOL
{$IFNDEF KOL_MCK}, mirror
, Controls
, mckObjs
, mckCtrls
, Classes
{$ENDIF},
12 Windows
, Messages
, SysUtils
, Classes
, Graphics
, Controls
, Forms
, Dialogs
,
18 {$I MCKfakeClasses.inc}
25 KOLProject1
: TKOLProject
;
26 fmControlKOLService
: TKOLForm
;
28 btnInstall
: TKOLButton
;
32 btnResume
: TKOLButton
;
33 btnRemove
: TKOLButton
;
44 procedure btnInstallClick(Sender
: PObj
);
45 procedure Button1Click(Sender
: PObj
);
46 procedure btnStartClick(Sender
: PObj
);
47 procedure Button2Click(Sender
: PObj
);
48 procedure btnStopClick(Sender
: PObj
);
49 procedure Button3Click(Sender
: PObj
);
50 procedure btnPauseClick(Sender
: PObj
);
51 procedure Button4Click(Sender
: PObj
);
52 procedure btnResumeClick(Sender
: PObj
);
53 procedure Button5Click(Sender
: PObj
);
54 procedure btnRemoveClick(Sender
: PObj
);
55 procedure Button6Click(Sender
: PObj
);
56 procedure fmControlKOLServiceFormCreate(Sender
: PObj
);
57 function fmControlKOLServiceMessage(var Msg
: tagMSG
;
58 var Rslt
: Integer): Boolean;
59 procedure Timer1Timer(Sender
: PObj
);
61 { Private declarations }
62 procedure RefreshStatus
;
63 procedure DoRefreshStatus
;
64 function BadHandles( SrvCtl
: PServiceCtl
): Boolean;
66 { Public declarations }
70 Form1
{$IFDEF KOL_MCK} : PForm1
{$ELSE} : TForm1
{$ENDIF} ;
73 procedure NewForm1( var Result
: PForm1
; AParent
: PControl
);
78 {$IFNDEF KOL_MCK} {$R *.DFM} {$ENDIF}
81 {$I MainControlKOLService_1.inc}
84 procedure TForm1
.DoRefreshStatus
;
85 var SrvCtl
: PServiceCtl
;
86 E_Inst
, E_Remv
, E_Strt
, E_Stop
, E_Paus
, E_Resm
: Boolean;
94 SrvCtl
:= OpenServiceCtl( '', '', 'KOL_ServiceA', SERVICE_QUERY_STATUS
);
95 if SrvCtl
.Handle
= 0 then
97 Label1
.Caption
:= 'Not installed';
101 case SrvCtl
.Status
.dwCurrentState
of
102 SERVICE_STOPPED
: begin
103 Label1
.Caption
:= 'Stopped';
107 SERVICE_START_PENDING
: begin
108 Label1
.Caption
:= 'Starting...';
112 SERVICE_STOP_PENDING
: begin
113 Label1
.Caption
:= 'Stopping...';
116 SERVICE_RUNNING
: begin
117 Label1
.Caption
:= 'RUNNING';
122 SERVICE_CONTINUE_PENDING
:begin
123 Label1
.Caption
:= 'Resuming...';
127 SERVICE_PAUSE_PENDING
: begin
128 Label1
.Caption
:= 'Pausing...';
131 SERVICE_PAUSED
: begin
132 Label1
.Caption
:= 'Paused';
138 btnInstall
.Enabled
:= E_Inst
;
139 btnRemove
.Enabled
:= E_Remv
;
140 btnStart
.Enabled
:= E_Strt
;
141 btnStop
.Enabled
:= E_Stop
;
142 btnPause
.Enabled
:= E_Paus
;
143 btnResume
.Enabled
:= E_Resm
;
152 SrvCtl
:= OpenServiceCtl( '', '', 'KOL_ServiceB', SERVICE_QUERY_STATUS
);
153 if SrvCtl
.Handle
= 0 then
155 Label3
.Caption
:= 'Not installed';
159 case SrvCtl
.Status
.dwCurrentState
of
160 SERVICE_STOPPED
: begin
161 Label3
.Caption
:= 'Stopped';
165 SERVICE_START_PENDING
: begin
166 Label3
.Caption
:= 'Starting...';
170 SERVICE_STOP_PENDING
: begin
171 Label3
.Caption
:= 'Stopping...';
174 SERVICE_RUNNING
: begin
175 Label3
.Caption
:= 'RUNNING';
180 SERVICE_CONTINUE_PENDING
:begin
181 Label3
.Caption
:= 'Resuming...';
185 SERVICE_PAUSE_PENDING
: begin
186 Label3
.Caption
:= 'Pausing...';
189 SERVICE_PAUSED
: begin
190 Label3
.Caption
:= 'Paused';
196 Button1
.Enabled
:= E_Inst
;
197 Button6
.Enabled
:= E_Remv
;
198 Button2
.Enabled
:= E_Strt
;
199 Button3
.Enabled
:= E_Stop
;
200 Button4
.Enabled
:= E_Paus
;
201 Button5
.Enabled
:= E_Resm
;
205 procedure TForm1
.btnInstallClick(Sender
: PObj
);
206 var SrvCtl
: PServiceCtl
;
208 SrvCtl
:= NewServiceCtl( '',
212 GetStartDir
+ 'TestKOLService.exe',
218 SERVICE_WIN32_OWN_PROCESS
or
219 SERVICE_INTERACTIVE_PROCESS
,
220 SERVICE_DEMAND_START
,
221 SERVICE_ERROR_NORMAL
);
222 if BadHandles( SrvCtl
) then Exit
;
225 ShowMessage( 'Installed OK.' );
228 procedure TForm1
.Button1Click(Sender
: PObj
);
229 var SrvCtl
: PServiceCtl
;
231 SrvCtl
:= NewServiceCtl( '',
235 GetStartDir
+ 'TestKOLService.exe',
241 SERVICE_WIN32_OWN_PROCESS
or
242 SERVICE_INTERACTIVE_PROCESS
,
243 SERVICE_DEMAND_START
,
244 SERVICE_ERROR_NORMAL
);
245 if BadHandles( SrvCtl
) then Exit
;
248 ShowMessage( 'Installed OK.' );
251 procedure TForm1
.btnStartClick(Sender
: PObj
);
252 var SrvCtl
: PServiceCtl
;
255 SrvCtl
:= OpenServiceCtl( '', '', 'KOL_ServiceA', SERVICE_ALL_ACCESS
);
256 if BadHandles( SrvCtl
) then Exit
;
257 OK
:= SrvCtl
.Start( [ 'param1', 'param2', 'param3' ] );
260 ShowMessage( SysErrorMessage( GetLastError
) );
264 procedure TForm1
.Button2Click(Sender
: PObj
);
265 var SrvCtl
: PServiceCtl
;
268 SrvCtl
:= OpenServiceCtl( '', '', 'KOL_ServiceB', SERVICE_ALL_ACCESS
);
269 if BadHandles( SrvCtl
) then Exit
;
270 OK
:= SrvCtl
.Start( [ 'param1', 'param2', 'param3' ] );
273 ShowMessage( SysErrorMessage( GetLastError
) );
277 function TForm1
.BadHandles(SrvCtl
: PServiceCtl
): Boolean;
280 if SrvCtl
.SCHandle
= 0 then
282 ShowMessage( 'Can not obtain SCHandle, ' + SysErrorMessage( GetLastError
) );
287 if SrvCtl
.Handle
= 0 then
289 ShowMessage( 'Can not obtain service handle, ' + SysErrorMessage( GetLastError
) );
296 procedure TForm1
.btnStopClick(Sender
: PObj
);
297 var SrvCtl
: PServiceCtl
;
300 SrvCtl
:= OpenServiceCtl( '', '', 'KOL_ServiceA', SERVICE_ALL_ACCESS
);
301 if BadHandles( SrvCtl
) then Exit
;
305 ShowMessage( SysErrorMessage( GetLastError
) );
309 procedure TForm1
.Button3Click(Sender
: PObj
);
310 var SrvCtl
: PServiceCtl
;
313 SrvCtl
:= OpenServiceCtl( '', '', 'KOL_ServiceB', SERVICE_ALL_ACCESS
);
314 if BadHandles( SrvCtl
) then Exit
;
318 ShowMessage( SysErrorMessage( GetLastError
) );
322 procedure TForm1
.btnPauseClick(Sender
: PObj
);
323 var SrvCtl
: PServiceCtl
;
326 SrvCtl
:= OpenServiceCtl( '', '', 'KOL_ServiceA', SERVICE_ALL_ACCESS
);
327 if BadHandles( SrvCtl
) then Exit
;
331 ShowMessage( SysErrorMessage( GetLastError
) );
335 procedure TForm1
.Button4Click(Sender
: PObj
);
336 var SrvCtl
: PServiceCtl
;
339 SrvCtl
:= OpenServiceCtl( '', '', 'KOL_ServiceB', SERVICE_ALL_ACCESS
);
340 if BadHandles( SrvCtl
) then Exit
;
344 ShowMessage( SysErrorMessage( GetLastError
) );
348 procedure TForm1
.btnResumeClick(Sender
: PObj
);
349 var SrvCtl
: PServiceCtl
;
352 SrvCtl
:= OpenServiceCtl( '', '', 'KOL_ServiceA', SERVICE_ALL_ACCESS
);
353 if BadHandles( SrvCtl
) then Exit
;
357 ShowMessage( SysErrorMessage( GetLastError
) );
361 procedure TForm1
.Button5Click(Sender
: PObj
);
362 var SrvCtl
: PServiceCtl
;
365 SrvCtl
:= OpenServiceCtl( '', '', 'KOL_ServiceB', SERVICE_ALL_ACCESS
);
366 if BadHandles( SrvCtl
) then Exit
;
370 ShowMessage( SysErrorMessage( GetLastError
) );
374 procedure TForm1
.btnRemoveClick(Sender
: PObj
);
375 var SrvCtl
: PServiceCtl
;
379 SrvCtl
:= OpenServiceCtl( '', '', 'KOL_ServiceA', SERVICE_ALL_ACCESS
);
380 if BadHandles( SrvCtl
) then Exit
;
382 Count
:= 30; // wait 3 seconds
383 while SrvCtl
.Status
.dwCurrentState
= SERVICE_STOP_PENDING
do
387 if Count
= 0 then break
;
389 //if SrvCtl.Status.dwCurrentState = SERVICE_STOPPED then
393 ShowMessage( SysErrorMessage( GetLastError
) );
397 procedure TForm1
.Button6Click(Sender
: PObj
);
398 var SrvCtl
: PServiceCtl
;
402 SrvCtl
:= OpenServiceCtl( '', '', 'KOL_ServiceB', SERVICE_ALL_ACCESS
);
403 if BadHandles( SrvCtl
) then Exit
;
405 Count
:= 30; // wait 3 seconds
406 while SrvCtl
.Status
.dwCurrentState
= SERVICE_STOP_PENDING
do
410 if Count
= 0 then break
;
412 //if SrvCtl.Status.dwCurrentState = SERVICE_STOPPED then
416 ShowMessage( SysErrorMessage( GetLastError
) );
420 procedure TForm1
.fmControlKOLServiceFormCreate(Sender
: PObj
);
425 procedure TForm1
.RefreshStatus
;
428 PostMessage( Form
.Handle
, WM_USER
, 0, 0 );
431 function TForm1
.fmControlKOLServiceMessage(var Msg
: tagMSG
;
432 var Rslt
: Integer): Boolean;
434 if Msg
.message = WM_USER
then
439 procedure TForm1
.Timer1Timer(Sender
: PObj
);