unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Winsvc;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure StopService(ServiceName: string);
procedure Button2Click(Sender: TObject);
procedure StartService(ServiceName: string);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
StopService(Edit1.Text);
end;
procedure TForm1.StopService(ServiceName: string);
var
schService,
schSCManager: DWORD;
p: PChar;
ss: _SERVICE_STATUS;
begin
p := nil;
schSCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if schSCManager = 0 then
RaiseLastWin32Error;
try
schService := OpenService(schSCManager, PChar(ServiceName),
SERVICE_ALL_ACCESS);
if schService = 0 then
RaiseLastWin32Error;
try
if not ControlService(schService, SERVICE_CONTROL_STOP, SS) then
RaiseLastWin32Error;
finally
CloseServiceHandle(schService);
end;
finally
CloseServiceHandle(schSCManager);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
StartService(Edit1.Text);
end;
procedure TForm1.StartService(ServiceName: string);
var
schService,
schSCManager: Dword;
p: PChar;
begin
p := nil;
schSCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if schSCManager = 0 then
RaiseLastWin32Error;
try
schService := OpenService(schSCManager, PChar(ServiceName),
SERVICE_ALL_ACCESS);
if schService = 0 then
RaiseLastWin32Error;
try
if not Winsvc.startService(schService, 0, p) then
RaiseLastWin32Error;
finally
CloseServiceHandle(schService);
end;
finally
CloseServiceHandle(schSCManager);
end;
end;
end.
|