uses WinSvc; const // // Tipos de Servicios // SERVICE_KERNEL_DRIVER = $00000001; SERVICE_FILE_SYSTEM_DRIVER = $00000002; SERVICE_ADAPTER = $00000004; SERVICE_RECOGNIZER_DRIVER = $00000008; SERVICE_DRIVER = (SERVICE_KERNEL_DRIVER or SERVICE_FILE_SYSTEM_DRIVER or SERVICE_RECOGNIZER_DRIVER); SERVICE_WIN32_OWN_PROCESS = $00000010; SERVICE_WIN32_SHARE_PROCESS = $00000020; SERVICE_WIN32 = (SERVICE_WIN32_OWN_PROCESS or SERVICE_WIN32_SHARE_PROCESS); SERVICE_INTERACTIVE_PROCESS = $00000100; SERVICE_TYPE_ALL = (SERVICE_WIN32 or SERVICE_ADAPTER or SERVICE_DRIVER or SERVICE_INTERACTIVE_PROCESS); //------------------------------------- // Obtener la lista de servicios // // Desvuelve True si es exitoso // // sMachine: // Nombre del equipo, Ej: \SERVIDOR o \ENECUMENE_PC // Vacío = Equipo Local // // dwServiceType // SERVICE_WIN32, // SERVICE_DRIVER ó // SERVICE_TYPE_ALL // // dwServiceState // SERVICE_ACTIVE, // SERVICE_INACTIVE ó // SERVICE_STATE_ALL // // slServicesList // Variable donde se guarda los datos TStrings // function ServiceGetList( sMachine : string; dwServiceType, dwServiceState : DWord; slServicesList : TStrings ) : boolean; const // // Asumimos que el total de // servicios es menos de 4096. // si es necesario lo podemos incrementar cnMaxServices = 4096; type TSvcA = array[0..cnMaxServices] of TEnumServiceStatus; PSvcA = ^TSvcA; var // // Uso Temporal j : integer; // // Control de servicio // manejador del handle schm : SC_Handle; // // Bytes necesarios para el // siguiente buffer, si existe nBytesNeeded, // // Número de servicios nServices, // // Puntero a la siguiente entrada // del servicio sin leer nResumeHandle : DWord; // // Estado del servicio ssa : PSvcA; begin Result := false; // Conectamos al servicio // Control de Manejo schm := OpenSCManager( PChar(sMachine), Nil, SC_MANAGER_ALL_ACCESS); // Si todo anda bien... if(schm > 0)then begin nResumeHandle := 0; New(ssa); EnumServicesStatus( schm, dwServiceType, dwServiceState, ssa^[0], SizeOf(ssa^), nBytesNeeded, nServices, nResumeHandle ); for j := 0 to nServices-1 do begin slServicesList. Add( StrPas( ssa^[j].lpDisplayName ) ); end; Result := true; Dispose(ssa); // Cerramos el control de servicio CloseServiceHandle(schm); end; end;
Su modo de uso sería:
//Si es un equipo local ServiceGetList('',SERVICE_WIN32,SERVICE_STATE_ALL,ListBox1.Items); //Algún equipo en RED ServiceGetList('\\ENECUMENE_PC',SERVICE_WIN32,SERVICE_STATE_ALL,ListBox1.Items);
Qué lo disfruten .
Saludos.