unit servicetest;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, shellapi, WinSvc, XPMan, Buttons, colorbtn, ExtCtrls;
type
TForm1 =
class(TForm)
btstatus: TButton;
Label1: TLabel;
XPManifest1: TXPManifest;
btstop: TColorBtn;
btstart: TColorBtn;
ImageList1: TImageList;
Button1: TButton;
PaintBox1: TPaintBox;
Label2: TLabel;
procedure btstatusClick(Sender: TObject);
procedure stopClick(Sender: TObject);
procedure startClick(Sender: TObject);
procedure btstopClick(Sender: TObject);
procedure btstartClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
cname :
string;
status:boolean;
implementation
{$R *.dfm}
const
bit29 = 1
SHL 28;
NERR_Success = 0;
NERR_BASE = 2100;
NERR_NameNotFound = NERR_BASE + 173;
NERR_NetworkError = NERR_BASE + 36;
ERROR_FAILED_STARTING_SERVICE = 1
or bit29;
ERROR_FAILED_STOPPING_SERVICE = 2
or bit29;
function ServiceGetStatus(sMachine, sService: PChar): DWORD;
{******************************************}
{*** Parameters: ***}
{*** sService: specifies the name of the service to open
{*** sMachine: specifies the name of the target computer
{*** ***}
{*** Return Values: ***}
{*** -1 = Error opening service ***}
{*** 1 = SERVICE_STOPPED ***}
{*** 2 = SERVICE_START_PENDING ***}
{*** 3 = SERVICE_STOP_PENDING ***}
{*** 4 = SERVICE_RUNNING ***}
{*** 5 = SERVICE_CONTINUE_PENDING ***}
{*** 6 = SERVICE_PAUSE_PENDING ***}
{*** 7 = SERVICE_PAUSED ***}
{******************************************}
var
SCManHandle, SvcHandle: SC_Handle;
SS: TServiceStatus;
dwStat: DWORD;
begin
dwStat := 0;
// Open service manager handle.
SCManHandle := OpenSCManager(sMachine,
nil, SC_MANAGER_CONNECT);
if (SCManHandle > 0)
then
begin
SvcHandle := OpenService(SCManHandle, sService, SERVICE_QUERY_STATUS);
// if Service installed
if (SvcHandle > 0)
then
begin
// SS structure holds the service status (TServiceStatus);
if (QueryServiceStatus(SvcHandle, SS))
then
dwStat := ss.dwCurrentState;
CloseServiceHandle(SvcHandle);
end;
CloseServiceHandle(SCManHandle);
end;
Result := dwStat;
end;
function ServiceRunning(sMachine, sService: PChar): Boolean;
begin
Result := SERVICE_RUNNING = ServiceGetStatus(sMachine, sService);
end;
function ServiceStart(Machine, ServiceName:
string): Boolean;
// Machine is UNC path or local machine if empty
var
h_manager, h_svc: SC_Handle;
ServiceStatus: TServiceStatus;
dwCheckPoint: DWORD;
ServiceArgVectors: PChar;
begin
h_manager := OpenSCManager(PChar(Machine),
nil, SC_MANAGER_CONNECT);
if h_manager > 0
then
begin
h_svc := OpenService(h_manager, PChar(ServiceName),
SERVICE_START
or SERVICE_QUERY_STATUS
or SC_MANAGER_ALL_ACCESS);
if h_svc > 0
then
begin
if (StartService(h_svc, 0, ServiceArgVectors))
then { succeeded }
begin
if (QueryServiceStatus(h_svc, ServiceStatus))
then
begin
while (SERVICE_RUNNING <> ServiceStatus.dwCurrentState)
do
begin
dwCheckPoint := ServiceStatus.dwCheckPoint;
Sleep(ServiceStatus.dwWaitHint);
if (
not QueryServiceStatus(h_svc, ServiceStatus))
then
// couldn't check status
break;
if (ServiceStatus.dwCheckPoint < dwCheckPoint)
then
break;
end;
end;
end;
CloseServiceHandle(h_svc);
end;
CloseServiceHandle(h_manager);
end;
Result := (SERVICE_RUNNING = ServiceStatus.dwCurrentState);
end;
function ServiceStop(Machine, ServiceName:
string): Boolean;
// Machine is UNC path or local machine if empty
var
h_manager, h_svc: SC_Handle;
ServiceStatus: TServiceStatus;
dwCheckPoint: DWORD;
begin
h_manager := OpenSCManager(PChar(Machine),
nil, SC_MANAGER_CONNECT);
if h_manager > 0
then
begin
h_svc := OpenService(h_manager, PChar(ServiceName),
SERVICE_STOP
or SERVICE_QUERY_STATUS);
if h_svc > 0
then
begin
if (ControlService(h_svc, SERVICE_CONTROL_STOP, ServiceStatus))
then
begin
if (QueryServiceStatus(h_svc, ServiceStatus))
then
begin
while (SERVICE_STOPPED <> ServiceStatus.dwCurrentState)
do
begin
dwCheckPoint := ServiceStatus.dwCheckPoint;
Sleep(ServiceStatus.dwWaitHint);
if (
not QueryServiceStatus(h_svc, ServiceStatus))
then
// couldn't check status
break;
if (ServiceStatus.dwCheckPoint < dwCheckPoint)
then
break;
end;
end;
end;
CloseServiceHandle(h_svc);
end;
CloseServiceHandle(h_manager);
end;
Result := (SERVICE_STOPPED = ServiceStatus.dwCurrentState);
end;
procedure TForm1.btstatusClick(Sender: TObject);
begin
status := ServiceRunning(pchar('
pc265'),'
UmRdpService');
//if (status = True) then label1.Caption := 'läuft';
if (status = True)
then ImageList1.Draw(Paintbox1.Canvas,0,0,0);
if (status = False)
then ImageList1.Draw(Paintbox1.Canvas,0,0,1);
if (status = False)
then btstop.Enabled := false;
end;
procedure TForm1.stopClick(Sender: TObject);
begin
ServiceStop(pchar('
pc265'),'
UmRdpService');
end;
procedure TForm1.startClick(Sender: TObject);
begin
ServiceStart(pchar('
pc265'),'
UmRdpService');
btstop.Enabled := true;
end;
procedure TForm1.btstopClick(Sender: TObject);
begin
ServiceStop(pchar('
pc265'),'
UmRdpService');
btstop.Enabled := false;
end;
procedure TForm1.btstartClick(Sender: TObject);
begin
ServiceStart(pchar('
pc265'),'
UmRdpService');
btstop.Enabled := true;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ImageList1.Draw(Paintbox1.Canvas,0,0,0);
end;
procedure TForm1.FormCreate(Sender: TObject);
var ststatus:boolean;
begin
ststatus := ServiceRunning(pchar('
pc265'),'
UmRdpService');
if (ststatus = True)
then label1.Caption := '
läuft';
if (ststatus = True)
then ImageList1.Draw(Paintbox1.Canvas,0,0,0);
if (ststatus = False)
then ImageList1.Draw(Paintbox1.Canvas,0,0,1);
if (ststatus = False)
then btstop.Enabled := false;
label2.Caption := BoolToStr (ststatus);
end;
end.