[TRUCO DELPHI] Verificar si un Servicio se esta Ejecutando.
delphi
uses WinSvc; function ServiceGetStatus(sMachine, sService: PChar): DWORD; {******************************************} {*** Parametros: ***} {*** sService: Especifica el nombre del servicio {*** sMachine: Especifica el nombre de la Computadora {*** ***} {*** Return Values: ***} {*** -1 = Error iniciando el Servicio ***} {*** 1 = SERVICE_STOPPED ***} {*** 2 = SERVICE_START_PENDING ***} {*** 3 = SERVICE_STOP_PENDING ***} {*** 4 = SERVICE_RUNNING ***} {*** 5 = SERVICE_CONTINUE_PENDING ***} {*** 6 = SERVICE_PAUSE_PENDING ***} {*** 7 = SERVICE_PAUSED ***} {******************************************} var SCManHandle, SvcHandle: SC_Handle; SS: TServiceStatus; dwStat: DWORD; begin dwStat := 0; SCManHandle := OpenSCManager(sMachine, nil, SC_MANAGER_CONNECT); if (SCManHandle > 0) then begin SvcHandle := OpenService(SCManHandle, sService, SERVICE_QUERY_STATUS); if (SvcHandle > 0) then begin if (QueryServiceStatus(SvcHandle, SS)) then dwStat := ss.dwCurrentState; CloseServiceHandle(SvcHandle); end; CloseServiceHandle(SCManHandle); end; Result := dwStat; end; function ServiceRunning(sMachine, sService: PChar): Boolean; begin Result := SERVICE_RUNNING = ServiceGetStatus(sMachine, sService); end;
Ejemplo de uso:
delphi
//Verificar si el Eventlog esta ejecutandose procedure TForm1.Button1Click(Sender: TObject); begin if ServiceRunning(nil, 'Eventlog') then ShowMessage('Eventlog ejecutandose') else ShowMessage('Eventlog no ejecutandose') end;
Saludos!