This article mainly explains how to use Delphi to create a new service, stop system services, and get service status and new system server methods, the following is the key code:
Unit Servicescontrol;
Interface
Uses windows,messages,sysutils,winsvc,dialogs;
function startservices (Const svrname:string): Boolean;
function stopservices (Const svrname:string): Boolean;
function Queryservicestatu (Const svrname:string): String;
function createservices (Const svrname,filepath:string): Boolean;
function deleteservices (Const svrname:string): Boolean;
Implementation
Open service
function startservices (Const svrname:string): Boolean;
Var
A,b:sc_handle;
C:pchar;
Begin
Result:=false;
A:=openscmanager (nil,nil,sc_manager_all_access);
If a <=0 then Exit;
B:=openservice (A,pchar (svrname), service_all_access);
If b <=0 then Exit;
Try
Result:=startservice (B,0,C);
Closeservicehandle (b);
Closeservicehandle (a);
Except
Closeservicehandle (b);
Closeservicehandle (a);
Exit;
End
End
Stop Service
function stopservices (Const svrname:string): Boolean;
Var
A,b:sc_handle;
D:tservicestatus;
Begin
Result: = False;
A: =openscmanager (nil,nil,sc_manager_all_access);
If a <=0 then Exit;
B:=openservice (A,pchar (svrname), service_all_access);
If b <=0 then Exit;
Try
Result:=controlservice (B,SERVICE_CONTROL_STOP,D);
Closeservicehandle (a);
Closeservicehandle (b);
Except
Closeservicehandle (a);
Closeservicehandle (b);
Exit;
End
End
Querying the status of the current service
function Queryservicestatu (Const svrname:string): String;
Var
A,b:sc_handle;
D:tservicestatus;
Begin
Result: = ' not installed ';
A: = OpenSCManager (nil,nil,sc_manager_all_access);
If a <=0 then Exit;
B: = OpenService (A,pchar (svrname), service_all_access);
If b <= 0 then Exit;
Try
QueryServiceStatus (B,D);
If d.dwcurrentstate = Service_running Then
Result: = ' start '//run
else if d.dwcurrentstate = Service_running Then
Result: = ' wait '//runing
else if d.dwcurrentstate = Service_start_pending Then
Result: = ' wait '//pause
else if d.dwcurrentstate = Service_stop_pending Then
Result: = ' Stop '//pause
else if d.dwcurrentstate = service_paused Then
Result: = ' pause '//pause
else if d.dwcurrentstate = service_stopped Then
Result: = ' Stop '//stop
else if d.dwcurrentstate = Service_continue_pending Then
Result: = ' wait '//pause
else if d.dwcurrentstate = Service_pause_pending Then
Result: = ' wait '; Pause
Closeservicehandle (a);
Closeservicehandle (b);
Except
Closeservicehandle (a);
Closeservicehandle (b);
Exit;
End
End
{Create service}
function createservices (Const svrname,filepath:string): Boolean;
Var
A,b:sc_handle;
Begin
Result:=false;
If FilePath = ' then Exit;
A: = OpenSCManager (Nil,nil,sc_manager_create_service);
If a <= 0 then Exit;
Try
B: = CreateService (A,pchar (Svrname),
Pchar (Svrname),
Service_all_access,
Service_interactive_process or Service_win32_own_process,
Service_auto_start,service_error_normal,
Pchar (FilePath), nil,nil,nil,nil,nil);
If b <= 0 THEN BEGIN
ShowMessage (Syserrormessage (GetLastError));
Exit;
End
Closeservicehandle (a);
Closeservicehandle (b);
Result: = True;
Except
Closeservicehandle (a);
Closeservicehandle (b);
Exit;
End
End
{Uninstall Service}
function deleteservices (Const svrname:string): Boolean;
Var
A,b:sc_handle;
Begin
Result:=false;
A: = OpenSCManager (nil,nil,sc_manager_all_access);
If a <= 0 then Exit;
B: =openservice (A,pchar (svrname), standard_rights_required);
If b <= 0 then Exit;
Try
Result: = DeleteService (b);
If not result then
ShowMessage (Syserrormessage (GetLastError));
Closeservicehandle (b);
Closeservicehandle (a);
Except
Closeservicehandle (b);
Closeservicehandle (a);
Exit;
End
End
End.
Call Method:
{Start Service}
Startservices (service name);
{Stop Service}
Stopservices (service name);
{New Service}
Createservices (service name, exe file path);
{Delete Service}
Deleteservices (service name);
{Get Service Status}
String:=queryservicestatu (service name);