Unit u_startservices;
Interface
Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, winsvc, stdctrls;
Type
Tform1 = Class (tform)
Btn_startservices: tbutton;
Btn_stopservices: tbutton;
Procedure btn_startservicesclick (Sender: tobject );
Procedure btn_stopservicesclick (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
End;
VaR
Form1: tform1;
Function startservices (const svrname: string): Boolean;
Implementation
{$ R *. DFM}
// Enable the Service
Function startservices (const svrname: string): Boolean;
VaR
Sch, svcsch: SC _handle;
Arg: pchar;
Dwstarttype: DWORD;
Begin
Result: = false;
Sch: = openscmanager (nil, nil, SC _manager_all_access );
If Sch <= 0 Then exit;
Svcsch: = openservice (SCH, pchar (svrname), service_all_access );
If (changeserviceconfig (
Svcsch, // handle of service
Service_no_change, // service_no_change, // service type: No change
Service_auto_start, // change service start Type
Service_no_change, // Error Control: No change
Nil, // binary path: No change
Nil, // load order group: No change
Nil, // tag ID: No change
Nil, // dependencies: No change
Nil, // Account name: No change
Nil, // password: No change
Nil) then
Showmessage ('Auto start OK ')
Else
Showmessage ('Auto start error ');
If svcsch <= 0 Then exit;
Try
Result: = startservice (svcsch, 0, ARG );
Closeservicehandle (svcsch );
Closeservicehandle (Sch );
Except
Closeservicehandle (svcsch );
Closeservicehandle (Sch );
Exit;
End;
End;
// Stop the service
Function stopservices (const svrname: string): Boolean;
VaR
Sch, svcsch: SC _handle;
SS: tservicestatus;
Begin
Result: = false;
Sch: = openscmanager (nil, nil, SC _manager_all_access );
If Sch <= 0 Then exit;
Svcsch: = openservice (SCH, pchar (svrname), service_all_access );
If svcsch <= 0 Then exit;
Try
Result: = controlservice (svcsch, service_control_stop, SS );
Closeservicehandle (Sch );
Closeservicehandle (svcsch );
Except
Closeservicehandle (Sch );
Closeservicehandle (svcsch );
Exit;
End;
End;
Procedure tform1.btn _ startservicesclick (Sender: tobject );
Begin
If startservices ('policyagent') = true then
Application. MessageBox (pchar ('yyagent service started successfully'), pchar ('service information'), mb_iconinformation)
Else
Application. MessageBox (pchar ('yyagent Service Startup failed'), pchar ('service information'), mb_iconerror );
End;
Procedure tform1.btn _ stopservicesclick (Sender: tobject );
Begin
If stopservices ('policyagent') = true then
Application. MessageBox (pchar ('yyagent Service stopped successfully'), pchar ('service information'), mb_iconinformation)
Else
Application. MessageBox (pchar ('yyagent Service stopped successfully'), pchar ('service information'), mb_iconerror );
End;
End.