Difference between revisions of "ServiceManager/es"

From Free Pascal wiki
Jump to navigationJump to search
 
Line 1: Line 1:
For Windows, the fcl-extra package contains a unit called ServiceManager.
+
Bajo Windows existe un paquete extra (fcl-extra package) llamado ServiceManager.
 
 
Example that checks whether the SamSs (Security Accounts Manager; should always be running) service is running is below.
 
(Command line equivalent sc query samss).
 
  
 +
El siguiente ejemplo chequea si está ejecutándose el servicio SamSs (Security Accounts Manager, que debería estar siempre arrancado).
 +
(equivalente en línea de mandatos de sc query samss).
  
 
<syntaxhighlight>program ServiceTest;
 
<syntaxhighlight>program ServiceTest;

Revision as of 10:48, 24 July 2013

Bajo Windows existe un paquete extra (fcl-extra package) llamado ServiceManager.

El siguiente ejemplo chequea si está ejecutándose el servicio SamSs (Security Accounts Manager, que debería estar siempre arrancado). (equivalente en línea de mandatos de sc query samss).

program ServiceTest;
// Check if a certain process is running.
{$mode objfpc}{$H+}
uses
  Classes,
  SysUtils,
  ServiceManager,
  JwaWinSvc {for services declarations};

function IsServiceRunning(ServiceName: string): boolean;
  {description Checks if a Windows service is running}
var
  Services: TServiceManager;
  ServiceStatus: TServiceStatus;
begin
  //Check for existing services
  //equivalent to sc query <servicename>
  Services := TServiceManager.Create(nil);
  try
    try
      Services.Acces := SC_MANAGER_CONNECT; //Note typo in property.
      //We don't need more access permissions than this; by default
      //the servicemanager is trying to get all access
      Services.Connect; //Now connect with requested access level
      Services.GetServiceStatus(ServiceName, ServiceStatus);
      if ServiceStatus.dwCurrentState = SERVICE_RUNNING then
      begin
        Result := True;
      end
      else
      begin
        Result := False;
      end;
      Services.Disconnect;
    except
      on E: EServiceManager do
      begin
        // A missing service might throw a missing handle exception? No?
      {LogOutput('Error getting service information for ' + ServiceName +
        '. Technical details: ' + E.ClassName + '/' + E.Message);}
        Result := False;
        raise; //rethrow original exception
      end;
      on E: Exception do
      begin
      {LogOutput('Error getting service information for ' + ServiceName +
        '. Technical details: ' + E.ClassName + '/' + E.Message);
        }
        Result := False;
        raise; //rethrow original exception
      end;
    end;
  finally
    Services.Free;
  end;
end;

const
  ServiceToTest = 'SamSs';

//Security Accounts Manager, should be running, at least on Vista
begin
  WriteLn('Starting test for ' + ServiceToTest + ' service.');
  if IsServiceRunning(ServiceToTest) then
  begin
    WriteLn('The ' + ServiceToTest + ' service is running');
  end
  else
  begin
    WriteLn('The ' + ServiceToTest + ' service is not running');
  end;
end.