Difference between revisions of "ServiceManager/es"
Line 77: | Line 77: | ||
end.</syntaxhighlight> | end.</syntaxhighlight> | ||
− | Funciones, constantes y tipos: | + | |
+ | == Funciones, constantes y tipos: == | ||
+ | |||
function ServiceStateToString (AState: DWord): String | function ServiceStateToString (AState: DWord): String |
Revision as of 11:06, 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;
// 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; //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
// ¿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; //rethrow original exception
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