Delphi Programming Start Service Stop service new service method __ Programming

Source: Internet
Author: User

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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.