Delphi starts/stops Windows Services and changes the Startup Type to "automatic"

Source: Internet
Author: User
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.

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.