Difference between revisions of "ServiceManager/es"

From Free Pascal wiki
Jump to navigationJump to search
m (Fixed syntax highlighting; deleted category included in page template)
(24 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
{{ServiceManager}}
 +
 +
== ServiceManager (Gestor de Servicios) ==
 +
 
Bajo Windows existe un paquete extra (fcl-extra package) llamado ServiceManager.
 
Bajo Windows existe un paquete extra (fcl-extra package) llamado ServiceManager.
  
Line 4: Line 8:
 
(equivalente en línea de mandatos de sc query samss).
 
(equivalente en línea de mandatos de sc query samss).
  
<syntaxhighlight>program ServiceTest;
+
== Nombre de Servicio ==
// Check if a certain process is running.
+
 
 +
Debes tener en cuenta que si desarrollas tu propio servicio utilizando el paquete Lazdaemon entonces el nombre que se le asigne a dicho servicio tiene su importancia. Mientras que los mandatos '''net start''' y '''net stop''' utilizan el servicio mostrar nombre, el mandato '''sc''' y el código que se muestra más abajo utilizan el nombre de servicio interno. El nombre mostrado es que el se tiene en la propiedad "Display Name" del elemento creado en los elementos DaemosDefs. El nombre del servicio es la propiedad "Name" de dicho elemento. El nombre mostrado es el que se muestra actualmente en la columna '''Name''' por medio del applet de MS services manager (services.msc). Vamos a utilizar el ejemplo para clarificar esto. Se asume que has escrito una aplicación servicio llamada myService.exe en la cual hay un elemento en DaemosDefs. El elemento tienen por nombre "MyServ" y la propiedad asociada "DisplayName" tiene el valor "Internal build service". Utilizando la línea de mandatos como administrador puedes:
 +
 
 +
== Gestor de servicios utilizando la consola ==
 +
 
 +
Para esto vas a necesitar tener funcionando alguno de los siguientes : Microsoft .Net Framework 4.5.2 (Web Installer) para Vista SP2, Windows 7 SP1, Windows 8, Windows 8.1, Windows Server 2008 SP2, Windows Server 2008 R2 SP1, Windows Server 2012 o bien Windows Server 2012 R2.
 +
 
 +
Tienes que tener soporte en el promt de mandatos con .Net SDK para instalar el servicio en tu máquina local.
 +
 
 +
1. Llama al prompt de mandatos SDK, seguido de
 +
Start>All Programs>Microsoft .NET Framework SDK v2.0>SDK Command prompt
 +
 
 +
* '''Instala el servicio:'''
 +
 
 +
<syntaxhighlight lang="dos">
 +
\tu\trayecto\a\tu\aplicacion\myService.exe --install
 +
rem .... o también funciona con /install, -install
 +
</syntaxhighlight>
 +
 
 +
* '''Activa el servicio:'''
 +
 
 +
En estos mandatos no olvides las '''dobles comillas''' cuando se necesiten múltiples palabras.
 +
 
 +
<syntaxhighlight lang="dos">
 +
net start "Internal build service"
 +
rem .... o alternativamente:
 +
sc start "MyServ"
 +
</syntaxhighlight>
 +
 
 +
* '''Para detener el servicio:'''
 +
 
 +
<syntaxhighlight lang="dos">
 +
net stop '''"Internal build service"'''
 +
rem ... o alternativamente
 +
sc stop "'''MyServ'''"
 +
</syntaxhighlight>
 +
 
 +
== Código ejemplo ==
 +
 
 +
Dicho esto, el fragmento de código para llamara la función de abajo debería ser en nuestro caso:
 +
 
 +
Programa de ejemplo:
 +
 
 +
<syntaxhighlight lang=pascal>
 +
program ServiceTest;
 +
// Chequea si un cierto proceso está corriendo.
 
{$mode objfpc}{$H+}
 
{$mode objfpc}{$H+}
 
uses
 
uses
Line 11: Line 60:
 
   SysUtils,
 
   SysUtils,
 
   ServiceManager,
 
   ServiceManager,
   JwaWinSvc {for services declarations};
+
   JwaWinSvc {para declaraciones de servicios};
  
 
function IsServiceRunning(ServiceName: string): boolean;
 
function IsServiceRunning(ServiceName: string): boolean;
   {description Checks if a Windows service is running}
+
   {Chequea  si un servicio Windows está corriendo}
 
var
 
var
 
   Services: TServiceManager;
 
   Services: TServiceManager;
 
   ServiceStatus: TServiceStatus;
 
   ServiceStatus: TServiceStatus;
 
begin
 
begin
   //Check for existing services
+
   //Chequea por la existencia de servicios.
   //equivalent to sc query <servicename>
+
   //equivalente a sc query <nombredeservicio>
 
   Services := TServiceManager.Create(nil);
 
   Services := TServiceManager.Create(nil);
 
   try
 
   try
 
     try
 
     try
 
       Services.Acces := SC_MANAGER_CONNECT; //Note typo in property.
 
       Services.Acces := SC_MANAGER_CONNECT; //Note typo in property.
       //We don't need more access permissions than this; by default
+
       //NO necesitamos más permisos de accesos que este; por defecto
       //the servicemanager is trying to get all access
+
       //servicemanager trata de obtener acceso total.
       Services.Connect; //Now connect with requested access level
+
       Services.Connect; //Ahora conecta con el nivel de acceso requerido.
 
       Services.GetServiceStatus(ServiceName, ServiceStatus);
 
       Services.GetServiceStatus(ServiceName, ServiceStatus);
 
       if ServiceStatus.dwCurrentState = SERVICE_RUNNING then
 
       if ServiceStatus.dwCurrentState = SERVICE_RUNNING then
Line 41: Line 90:
 
       on E: EServiceManager do
 
       on E: EServiceManager do
 
       begin
 
       begin
         // A missing service might throw a missing handle exception? No?
+
         // ¿Puede un servicio perdido lanzar una excepción de handle perdido? ¿No?
 
       {LogOutput('Error getting service information for ' + ServiceName +
 
       {LogOutput('Error getting service information for ' + ServiceName +
 
         '. Technical details: ' + E.ClassName + '/' + E.Message);}
 
         '. Technical details: ' + E.ClassName + '/' + E.Message);}
 
         Result := False;
 
         Result := False;
         raise; //rethrow original exception
+
         raise; //relanza la excepción original.
 
       end;
 
       end;
 
       on E: Exception do
 
       on E: Exception do
 
       begin
 
       begin
       {LogOutput('Error getting service information for ' + ServiceName +
+
       {LogOutput('Error obteniendo información de servicio para ' + ServiceName +
         '. Technical details: ' + E.ClassName + '/' + E.Message);
+
         '. Detalles técnicos: ' + E.ClassName + '/' + E.Message);
 
         }
 
         }
 
         Result := False;
 
         Result := False;
         raise; //rethrow original exception
+
         raise; //relanza excepción original.
 
       end;
 
       end;
 
     end;
 
     end;
Line 64: Line 113:
 
   ServiceToTest = 'SamSs';
 
   ServiceToTest = 'SamSs';
  
//Security Accounts Manager, should be running, at least on Vista
+
//Security Accounts Manager, debería estar corriendo, al menos en Vista.
 
begin
 
begin
 
   WriteLn('Starting test for ' + ServiceToTest + ' service.');
 
   WriteLn('Starting test for ' + ServiceToTest + ' service.');
 
   if IsServiceRunning(ServiceToTest) then
 
   if IsServiceRunning(ServiceToTest) then
 
   begin
 
   begin
     WriteLn('The ' + ServiceToTest + ' service is running');
+
     WriteLn('The ' + ServiceToTest + ' el servicio está corriendo');
 
   end
 
   end
 
   else
 
   else
 
   begin
 
   begin
     WriteLn('The ' + ServiceToTest + ' service is not running');
+
     WriteLn('The ' + ServiceToTest + ' el servicio no está corriendo');
 
   end;
 
   end;
 
end.</syntaxhighlight>
 
end.</syntaxhighlight>
  
[[Category:Standard Units]]
+
== Funciones, constantes y tipos: ==
 +
 
 +
 
 +
function ServiceStateToString (AState: DWord): String
 +
 
 +
const ServiceTypes = Array [0..3] of Word
 +
 
 +
function ServiceTypeToString (AType: DWord): String
 +
 
 +
const StartErrors = Array [0..3] of DWord
 +
 
 +
const StartTypes = Array [0..4] of DWord
 +
 
 +
type TServiceDescriptor = record
 +
 
 +
type TServiceEntries = class (TOwnedCollection)
 +
 
 +
type TServiceEntry = class (TCollectionItem)
 +
 
 +
type TServiceManager = class (Tcomponent)
 +
 
 +
function ControlAceptedToString (AValue: DWord): String
 +
 
 +
type EServiceManager = class (Exception)
 +
 
 +
function IsInteractiveService (AType: DWord): Boolean
 +
 
 +
 
 +
== Ver también ==
 +
* [[http://wiki.lazarus.freepascal.org/Daemons_and_Services/es Demonios y Servicios]]

Revision as of 13:54, 26 February 2020

English (en) español (es) polski (pl)

ServiceManager (Gestor de Servicios)

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).

Nombre de Servicio

Debes tener en cuenta que si desarrollas tu propio servicio utilizando el paquete Lazdaemon entonces el nombre que se le asigne a dicho servicio tiene su importancia. Mientras que los mandatos net start y net stop utilizan el servicio mostrar nombre, el mandato sc y el código que se muestra más abajo utilizan el nombre de servicio interno. El nombre mostrado es que el se tiene en la propiedad "Display Name" del elemento creado en los elementos DaemosDefs. El nombre del servicio es la propiedad "Name" de dicho elemento. El nombre mostrado es el que se muestra actualmente en la columna Name por medio del applet de MS services manager (services.msc). Vamos a utilizar el ejemplo para clarificar esto. Se asume que has escrito una aplicación servicio llamada myService.exe en la cual hay un elemento en DaemosDefs. El elemento tienen por nombre "MyServ" y la propiedad asociada "DisplayName" tiene el valor "Internal build service". Utilizando la línea de mandatos como administrador puedes:

Gestor de servicios utilizando la consola

Para esto vas a necesitar tener funcionando alguno de los siguientes : Microsoft .Net Framework 4.5.2 (Web Installer) para Vista SP2, Windows 7 SP1, Windows 8, Windows 8.1, Windows Server 2008 SP2, Windows Server 2008 R2 SP1, Windows Server 2012 o bien Windows Server 2012 R2.

Tienes que tener soporte en el promt de mandatos con .Net SDK para instalar el servicio en tu máquina local.

1. Llama al prompt de mandatos SDK, seguido de Start>All Programs>Microsoft .NET Framework SDK v2.0>SDK Command prompt

  • Instala el servicio:
\tu\trayecto\a\tu\aplicacion\myService.exe --install 
rem .... o también funciona con /install, -install
  • Activa el servicio:

En estos mandatos no olvides las dobles comillas cuando se necesiten múltiples palabras.

net start "Internal build service"
rem .... o alternativamente:
sc start "MyServ"
  • Para detener el servicio:
net stop '''"Internal build service"'''
rem ... o alternativamente 
sc stop "'''MyServ'''"

Código ejemplo

Dicho esto, el fragmento de código para llamara la función de abajo debería ser en nuestro caso:

Programa de ejemplo:

program ServiceTest;
// Chequea si un cierto proceso está corriendo.
{$mode objfpc}{$H+}
uses
  Classes,
  SysUtils,
  ServiceManager,
  JwaWinSvc {para declaraciones de servicios};

function IsServiceRunning(ServiceName: string): boolean;
  {Chequea  si un servicio Windows está corriendo}
var
  Services: TServiceManager;
  ServiceStatus: TServiceStatus;
begin
  //Chequea por la existencia de servicios.
  //equivalente a sc query <nombredeservicio>
  Services := TServiceManager.Create(nil);
  try
    try
      Services.Acces := SC_MANAGER_CONNECT; //Note typo in property.
      //NO necesitamos más permisos de accesos que este; por defecto
      //servicemanager trata de obtener acceso total.
      Services.Connect; //Ahora conecta con el nivel de acceso requerido.
      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
        // ¿Puede un servicio perdido lanzar una excepción de handle perdido? ¿No?
      {LogOutput('Error getting service information for ' + ServiceName +
        '. Technical details: ' + E.ClassName + '/' + E.Message);}
        Result := False;
        raise; //relanza la excepción original.
      end;
      on E: Exception do
      begin
      {LogOutput('Error obteniendo información de servicio para ' + ServiceName +
        '. Detalles técnicos: ' + E.ClassName + '/' + E.Message);
        }
        Result := False;
        raise; //relanza excepción original.
      end;
    end;
  finally
    Services.Free;
  end;
end;

const
  ServiceToTest = 'SamSs';

//Security Accounts Manager, debería estar corriendo, al menos en Vista.
begin
  WriteLn('Starting test for ' + ServiceToTest + ' service.');
  if IsServiceRunning(ServiceToTest) then
  begin
    WriteLn('The ' + ServiceToTest + ' el servicio está corriendo');
  end
  else
  begin
    WriteLn('The ' + ServiceToTest + ' el servicio no está corriendo');
  end;
end.

Funciones, constantes y tipos:

function ServiceStateToString (AState: DWord): String

const ServiceTypes = Array [0..3] of Word

function ServiceTypeToString (AType: DWord): String

const StartErrors = Array [0..3] of DWord

const StartTypes = Array [0..4] of DWord

type TServiceDescriptor = record

type TServiceEntries = class (TOwnedCollection)

type TServiceEntry = class (TCollectionItem)

type TServiceManager = class (Tcomponent)

function ControlAceptedToString (AValue: DWord): String

type EServiceManager = class (Exception)

function IsInteractiveService (AType: DWord): Boolean


Ver también