This page contains the two Delphi examples of how to start, stop, pause and resume
Windows services.
They both produce the same result. The first example is based on
TWmiQuery,
TWmiMethod and
TWmiConnection components from
WmiSet component collection.
The second example utilizes
TNTServiceManager component from
NTSet component collection. There are minor differences in the examples:
-
WmiSet components better handle the connection to the remote host.
They use the WMI connection capabilities.
NTSet components use "null session" method, which may
fail if the current user already has a connection to destination host
with different credentials.
- NTSet components use pure API and work little bit faster.
- Error handling is slightly different. For example, TNTServiceManager
component will report a success when trying to pause the service that
is already paused. WmiSet components will report an error.
See the description of
Win32_Service
class for the possible return values
The source code below creates and destroys the components on the fly,
so these functions may be used in console type applications. With minor
changes the functions may be adapted for UI-type apps.
|
type
TCommand = (UNKNOWN, SRV_START, SRV_STOP, SRV_PAUSE, SRV_RESUME);
// This function can start, stop, pause and resume Windows Service specified by name.
// It may be used for local or remote host. When used for local host
// TargetHost, UserName, Password parameters must be empty.
// For remote computer the parameters must be:
// TargetHost: name or IP address of the remote host, like '10.8.36.54'
// UserName: name of the user, may include domain, like 'MOON\Administrator';
// Password: the user's password.
// ServiceName: name of the service to start or stop.
// Command: the command to execute
// The function returns zero on success, error code on error.
function StartStopServices(TargetHost, UserName, Password,
ServiceName: string; Command: TCommand): integer;
var
Connection: TWmiConnection;
Query: TWmiQuery;
Method: TWmiMethod;
s: string;
begin
Result := -1;
Method := TWmiMethod.Create(nil);
Query := TWmiQuery.Create(nil);
Connection := TWmiConnection.Create(nil);
try
Query.Connection := Connection;
Query.WQL.Text := 'select * from Win32_Service';
Method.WmiObjectSource := Query;
Connection.MachineName := TargetHost;
Connection.Credentials.UserName := UserName;
Connection.Credentials.Password := Password;
try
// exception may happen on Win9x, WinNT if WMI core is not installed;
// The provided credentials may also be invalid.
Connection.Connected := true;
except
writeln('Cannot connect to the destination host');
Exit;
end;
Query.Open;
if not Query.Locate('name', ServiceName, [loCaseInsensitive]) then
begin
writeln('Service not found: '+ServiceName);
Exit;
end;
case Command of
SRV_START: Method.WmiMethodName := 'StartService';
SRV_STOP: Method.WmiMethodName := 'StopService';
SRV_PAUSE: Method.WmiMethodName := 'PauseService';
SRV_RESUME: Method.WmiMethodName := 'ResumeService';
end;
Result := Method.Execute;
if Result <> 0 then
begin
s := Method.LastWmiErrorDescription;
if s = '' then s := 'Error '+ IntToStr(Method.LastWmiError);
writeln(s);
end;
finally
Method.Free;
Query.Free;
Connection.Free;
end;
end;
|
type
TCommand = (UNKNOWN, SRV_START, SRV_STOP, SRV_PAUSE, SRV_RESUME);
// this procedure starts or stops Windows Service specified by name.
// It may be used for local or remote host. When used for local host
// TargetHost, UserName, Password parameters must be empty.
// For remote computer the parameters must be:
// TargetHost: name or IP address of the remote host, like '10.8.36.54'
// UserName: name of the user, may include domain, like 'MOON\Administrator';
// Password: the user's password.
// ServiceName: name of the service to start or stop.
// Command: the command to execute
// The function returns zero on success, error code on error.
function StartStopServices(TargetHost, UserName, Password,
ServiceName: string; Command: TCommand): integer;
var
ServiceMan: TNTServiceManager;
begin
ServiceMan := TNTServiceManager.Create(nil);
try
ServiceMan.LogonAs.UserName := UserName;
ServiceMan.LogonAs.Password := Password;
if TargetHost <> '' then ServiceMan.MachineName := '\\'+ TargetHost;
if (ServiceMan.MachineName <> '') and (ServiceMan.ConnectIPC <> 0) then
writeln('Warning: could not use the provided credentials.');
ServiceMan.ServiceName := ServiceName;
try
ServiceMan.ActiveManager := true;
ServiceMan.ActiveService := true;
case Command of
SRV_START: ServiceMan.Startservice(nil);
SRV_STOP: ServiceMan.ControlService(CONTROL_STOP);
SRV_PAUSE: ServiceMan.ControlService(CONTROL_PAUSE);
SRV_RESUME: ServiceMan.ControlService(CONTROL_CONTINUE);
end;
Result := 0
except
On e: ENTException do
begin
Result := e.ErrorCode;
writeln(e.message)
end;
end;
finally
ServiceMan.Free;
end;
end;
|