type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Function StartServices(Const svrName:String):Boolean;
Function StopServices(Const svrName:String):Boolean;
implementation
Uses Winsvc;
{$R *.dfm}
Function StartServices(Const svrName:String):Boolean;
var
a,b: SC_HANDLE;
c: PChar;
begin
StartServices:=False;
a:=OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);//ok is a>0
if a<=0 then exit;
b:=OpenService(a,PChar(svrName), SERVICE_ALL_ACCESS);//ok is b>0
if b<=0 then exit;
//开启服务
StartServices:=StartService(b, 0, c);
End;
Function StopServices(Const svrName:String):Boolean;
var
a, b: SC_HANDLE;
c: PChar;
d: TServiceStatus;
begin
StopServices:=False;
a := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);//ok is a>0
if a<=0 then exit;
b := OpenService(a,PChar(svrName), SERVICE_ALL_ACCESS);//ok is b>0
if b<=0 then exit;
StopServices:=ControlService(b, SERVICE_CONTROL_STOP, d);
End;
procedure TForm1.Button1Click(Sender: TObject);
begin
if StartServices(Edit1.Text) then ShowMessage('启动成功');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if StopServices(edit1.Text) then ShowMessage('失败');
end;