<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'Stopping and starting services in delphi code.' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'Stopping and starting services in delphi code.' posted on the 'Delphi and Kylix' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2013 Programmers Heaven</copyright>
    <pubDate>Sun, 19 May 2013 01:22:21 -0700</pubDate>
    <lastBuildDate>Sun, 19 May 2013 01:22:21 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>Stopping and starting services in delphi code.</title>
      <link>http://www.programmersheaven.com/mb/delphikylix/234511/234511/stopping-and-starting-services-in-delphi-code/</link>
      <description>How do i stop and start services in XP using delphi code, thankyou.&lt;br /&gt;
&lt;br /&gt;
Pyle&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/delphikylix/234511/234511/stopping-and-starting-services-in-delphi-code/</guid>
      <pubDate>Fri, 02 Jan 2004 23:10:43 -0700</pubDate>
      <category>Delphi and Kylix</category>
    </item>
    <item>
      <title>Re: Stopping and starting services in delphi code.</title>
      <link>http://www.programmersheaven.com/mb/delphikylix/234511/234516/re-stopping-and-starting-services-in-delphi-code/#234516</link>
      <description>: How do i stop and start services in XP using delphi code, thankyou.&lt;br /&gt;
: &lt;br /&gt;
: Pyle&lt;br /&gt;
: &lt;br /&gt;
Look into the CreateService()/OpenService() and the CloseServiceHandle()/DeleteService() API calls in the windows SDK help files.&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/delphikylix/234511/234516/re-stopping-and-starting-services-in-delphi-code/#234516</guid>
      <pubDate>Sat, 03 Jan 2004 00:45:08 -0700</pubDate>
      <category>Delphi and Kylix</category>
    </item>
    <item>
      <title>Re: Stopping and starting services: a component</title>
      <link>http://www.programmersheaven.com/mb/delphikylix/234511/234649/re-stopping-and-starting-services-a-component/#234649</link>
      <description>: How do i stop and start services in XP using delphi code, thankyou.&lt;br /&gt;
: &lt;br /&gt;
: Pyle&lt;br /&gt;
: &lt;br /&gt;
Hi,&lt;br /&gt;
Once wrote a component for that, it can also start/stop services on a different machine. It's very basic, but it does the trick.&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
unit ServiceStarter;

interface

uses
  WinSvc, Windows, Messages, SysUtils, Classes;

type
  TServiceState = (svsStopped, svsStarting, svsStopping, svsRunning, scvContinueing, svsPausing, svsPaused);

  TServiceStarter = class(TComponent)
  private
    FSCHandle: THandle;
    FState: TServiceState;
    FServiceName: string;
    FHandle: THandle;
    FMachineName: string;
    FActive: Boolean;
    procedure SetState(const Value: TServiceState);
    function GetState: TServiceState;
    procedure SetServiceName(const Value: string);
    procedure SetMachineName(const Value: string);
    function GetHandle: THandle;
    procedure SetActive(const Value: Boolean);
    procedure CloseDependendServices(Handle: THandle);
    { Private declarations }
  protected
    { Protected declarations }
    procedure CloseHandle;
    procedure CloseHandleSC;
    procedure HandleNeeded;
  public
    { Public declarations }
    destructor Destroy; override;
    property Handle: THandle read GetHandle;
  published
    { Published declarations }
    property ServiceName: string read FServiceName write SetServiceName;
    property MachineName: string read FMachineName write SetMachineName;
    property State: TServiceState read GetState write SetState;
    property Active: Boolean read FActive write SetActive;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('AppGadgets', [TServiceStarter]);
end;

{ TServiceStarter }


procedure TServiceStarter.CloseHandle;
begin
  if FHandle &amp;lt;&amp;gt; 0 then
    begin
    CloseServiceHandle(FHandle);
    FHandle := 0;
    end;
end;

procedure TServiceStarter.CloseHandleSC;
begin
  if FSCHandle &amp;lt;&amp;gt; 0 then
    begin
    CloseServiceHandle(FSCHandle);
    FSCHandle := 0;
    end;
end;

destructor TServiceStarter.Destroy;
begin
  CloseHandle;
  inherited;
end;

function TServiceStarter.GetHandle: THandle;
begin
  HandleNeeded;
  Result := FHandle;
end;

function TServiceStarter.GetState: TServiceState;
var
  ServiceStatus: TServiceStatus;
begin
  if FActive then
    begin
    if (FServiceName = '') then
      begin
      Result := svsStopped;
      Exit;
      end;
    HandleNeeded;
    if not QueryServiceStatus(FHandle, ServiceStatus) then
      RaiseLastOSError;
    Result := TServiceState(ServiceStatus.dwCurrentState - 1);
    end
  else
    Result := FState;
end;

procedure TServiceStarter.HandleNeeded;
begin
  if FHandle = 0 then
    begin
    if FSCHandle = 0 then
      begin
      FSCHandle := OpenSCManager(Pointer(FMachineName), nil, GENERIC_EXECUTE);
      if FSCHandle = 0 then
        RaiseLastOSError;
      end;
    FHandle := OpenService(FSCHandle, PChar(FServiceName), GENERIC_EXECUTE+SERVICE_QUERY_STATUS+SERVICE_ENUME
RATE_DEPENDENTS);
    if FHandle = 0 then
      RaiseLastOSError;
    end;
end;


procedure TServiceStarter.SetActive(const Value: Boolean);
begin
  if FActive = Value then
    Exit;

  FActive := Value;
  if FActive then
    State := FState
  else
    begin
    CloseHandle;
    CloseHandleSC;
    end;
end;

procedure TServiceStarter.SetMachineName(const Value: string);
begin
  if FMachineName = Value then
    Exit;

  CloseHandle;

  FMachineName := Value;
end;

procedure TServiceStarter.SetServiceName(const Value: string);
begin
  if FServiceName = Value then
    Exit;

  CloseHandle;
  CloseHandleSC;

  FServiceName := Value;
end;

procedure TServiceStarter.CloseDependendServices(Handle: THandle);
type
  TEnumServiceStatusArray = array[0..$FFFF] of TEnumServiceStatus;
  PEnumServiceStatusArray = ^TEnumServiceStatusArray;
var
  Ok: boolean;
  DependendHandle: THandle;
  I: Integer;
  ServicesCount: Cardinal;
  NeededBytes: Cardinal;
  DependendServices : PEnumServiceStatusArray;
  ServiceStatus: TServiceStatus;
begin
  NeededBytes := 1 * SizeOf(TEnumServiceStatusArray);
  Ok := True;
  GetMem(DependendServices, NeededBytes);
  while not EnumDependentServices(Handle, SERVICE_ACTIVE, DependendServices^[0], NeededBytes, NeededBytes, ServicesCount) do
    begin
    if GetLastError = ERROR_MORE_DATA then
      begin
      FreeMem(DependendServices);
      Ok:= False;
      GetMem(DependendServices, NeededBytes);
      end
    else
      RaiseLastOSError;
    end;
  for I := 0 to ServicesCount - 1 do
    with DependendServices^[I] do
      begin
      DependendHandle := OpenService(FSCHandle, lpServiceName, GENERIC_EXECUTE+SERVICE_QUERY_STATUS+SERVICE_ENUME
RATE_DEPENDENTS);
      while not ControlService(DependendHandle, SERVICE_CONTROL_STOP, ServiceStatus) do
        case GetLastError of
          ERROR_DEPENDENT_SERVICES_RUNNING:
            CloseDependendServices(DependendHandle);
          ERROR_SERVICE_NOT_ACTIVE:
            Break;
          ERROR_SERVICE_CANNOT_ACCEPT_CTRL: ;
          else
            RaiseLastOSError;
          end;
      CloseServiceHandle(DependendHandle);
      end;
  FreeMem(DependendServices);
end;

procedure TServiceStarter.SetState(const Value: TServiceState);
const
{
SERVICE_CONTROL_STOP
Requests the service to stop. The hService handle must have SERVICE_STOP access.
SERVICE_CONTROL_PAUSE
Requests the service to pause. The hService handle must have SERVICE_PAUSE_CONTINUE access.
SERVICE_CONTROL_CONTINUE
Requests the paused service to resume. The hService handle must have SERVICE_PAUSE_CONTINUE access.
SERVICE_CONTROL_INTERROGATE
Requests the service to update immediately its current status information to the service control manager. The hService handle must have SERVICE_INTERROGATE access.
SERVICE_CONTROL_SHUTDOWN
}
  StateControlMap: array[TServiceState] of Integer = (SERVICE_CONTROL_STOP, SERVICE_CONTROL_CONTINUE, SERVICE_CONTROL_STOP, SERVICE_CONTROL_CONTINUE, SERVICE_CONTROL_CONTINUE, SERVICE_CONTROL_PAUSE, SERVICE_CONTROL_PAUSE);
var
  Error: Cardinal;
  StateSet: boolean;
  Arg: PChar;
  ServiceStatus: TServiceStatus;
begin
  FState := Value;
  if Active then
    begin
    HandleNeeded;
    Arg := nil;
    StateSet := False;
    // svsStopped, svsStarting, svsStopping, svsRunning, scvContinueing, svsPausing, svsPaused
    repeat
      if not ControlService(FHandle, StateControlMap[Value], ServiceStatus) then
        begin
        Error := GetLastError;
        case Error of
          ERROR_SERVICE_CANNOT_ACCEPT_CTRL:
            begin
            Sleep(10);
            end;
          ERROR_SERVICE_NOT_ACTIVE:
            if not (Value in [svsStopped, svsStopping]) then
              begin
              if not StartService(FHandle, 0, Arg) then
                begin
                Error := GetLastError;
                if Error &amp;lt;&amp;gt; ERROR_SERVICE_CANNOT_ACCEPT_CTRL then
                  RaiseLastOSError
                else
                  Sleep(10);
                end;
              StateSet := Value in [svsRunning, scvContinueing];
              end
            else
              StateSet := True;
          ERROR_DEPENDENT_SERVICES_RUNNING:
            CloseDependendServices(FHandle);
          else
            RaiseLastOSError;
          end;
        end
      else
        StateSet := True;
    until StateSet;
  end;
end;

end.
&lt;/pre&gt;&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/delphikylix/234511/234649/re-stopping-and-starting-services-a-component/#234649</guid>
      <pubDate>Sun, 04 Jan 2004 12:25:17 -0700</pubDate>
      <category>Delphi and Kylix</category>
    </item>
    <item>
      <title>Re: Stopping and starting services: a component</title>
      <link>http://www.programmersheaven.com/mb/delphikylix/234511/429648/re-stopping-and-starting-services-a-component/#429648</link>
      <description>Thankyou for code sharing :)&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/delphikylix/234511/429648/re-stopping-and-starting-services-a-component/#429648</guid>
      <pubDate>Fri, 28 Sep 2012 00:05:00 -0700</pubDate>
      <category>Delphi and Kylix</category>
    </item>
  </channel>
</rss>