Jump to content


Photo

Obteniendo los servicios instalados de Windows


  • Please log in to reply
7 replies to this topic

#1 enecumene

enecumene

    Webmaster

  • Administrador
  • 7419 posts
  • LocationRepública Dominicana

Posted 22 September 2009 - 05:19 PM

Con esta función podemos obtener una lista de Servicios instalados en un equipo determinado, aquí el code:



delphi
  1. uses WinSvc;
  2.  
  3. const
  4.   //
  5.   // Tipos de Servicios
  6.   //
  7.   SERVICE_KERNEL_DRIVER      = $00000001;
  8.   SERVICE_FILE_SYSTEM_DRIVER  = $00000002;
  9.   SERVICE_ADAPTER            = $00000004;
  10.   SERVICE_RECOGNIZER_DRIVER  = $00000008;
  11.  
  12.   SERVICE_DRIVER              =
  13.     (SERVICE_KERNEL_DRIVER or
  14.     SERVICE_FILE_SYSTEM_DRIVER or
  15.     SERVICE_RECOGNIZER_DRIVER);
  16.  
  17.   SERVICE_WIN32_OWN_PROCESS  = $00000010;
  18.   SERVICE_WIN32_SHARE_PROCESS = $00000020;
  19.   SERVICE_WIN32              =
  20.     (SERVICE_WIN32_OWN_PROCESS or
  21.     SERVICE_WIN32_SHARE_PROCESS);
  22.  
  23.   SERVICE_INTERACTIVE_PROCESS = $00000100;
  24.  
  25.   SERVICE_TYPE_ALL            =
  26.     (SERVICE_WIN32 or
  27.     SERVICE_ADAPTER or
  28.     SERVICE_DRIVER  or
  29.     SERVICE_INTERACTIVE_PROCESS);
  30.  
  31. //-------------------------------------
  32. // Obtener la lista de servicios
  33. //
  34. // Desvuelve True si es exitoso
  35. //
  36. // sMachine:
  37. //  Nombre del equipo, Ej: \SERVIDOR o \ENECUMENE_PC
  38. //  Vacío = Equipo Local
  39. //
  40. // dwServiceType
  41. //  SERVICE_WIN32,
  42. //  SERVICE_DRIVER ó
  43. //  SERVICE_TYPE_ALL
  44. //
  45. // dwServiceState
  46. //  SERVICE_ACTIVE,
  47. //  SERVICE_INACTIVE ó
  48. //  SERVICE_STATE_ALL
  49. //
  50. // slServicesList
  51. //  Variable donde se guarda los datos TStrings
  52. //
  53. function ServiceGetList(
  54.   sMachine : string;
  55.   dwServiceType,
  56.   dwServiceState : DWord;
  57.   slServicesList : TStrings )
  58.   : boolean;
  59. const
  60.   //
  61.   // Asumimos que el total de
  62.   // servicios es menos de 4096.
  63.   // si es necesario lo podemos incrementar
  64.   cnMaxServices = 4096;
  65.  
  66. type
  67.   TSvcA = array[0..cnMaxServices]
  68.           of TEnumServiceStatus;
  69.   PSvcA = ^TSvcA;
  70.          
  71. var
  72.   //
  73.   // Uso Temporal
  74.   j : integer;
  75.  
  76.   //
  77.   // Control de servicio
  78.   // manejador del handle
  79.   schm          : SC_Handle;
  80.  
  81.   //
  82.   // Bytes necesarios para el
  83.   // siguiente buffer, si existe
  84.   nBytesNeeded,
  85.  
  86.   //
  87.   // Número de servicios
  88.   nServices,
  89.  
  90.   //
  91.   // Puntero a la siguiente entrada
  92.   // del servicio sin leer
  93.   nResumeHandle : DWord;
  94.  
  95.   //
  96.   // Estado del servicio
  97.   ssa : PSvcA;
  98. begin
  99.   Result := false;
  100.  
  101.   // Conectamos al servicio
  102.   // Control de Manejo
  103.   schm := OpenSCManager(
  104.     PChar(sMachine),
  105.     Nil,
  106.     SC_MANAGER_ALL_ACCESS);
  107.  
  108.   // Si todo anda bien...
  109.   if(schm > 0)then
  110.   begin
  111.     nResumeHandle := 0;
  112.  
  113.     New(ssa);
  114.  
  115.     EnumServicesStatus(
  116.       schm,
  117.       dwServiceType,
  118.       dwServiceState,
  119.       ssa^[0],
  120.       SizeOf(ssa^),
  121.       nBytesNeeded,
  122.       nServices,
  123.       nResumeHandle );
  124.    
  125.    
  126.     for j := 0 to nServices-1 do
  127.     begin
  128.       slServicesList.
  129.         Add( StrPas(
  130.           ssa^[j].lpDisplayName ) );
  131.     end;
  132.  
  133.     Result := true;
  134.  
  135.     Dispose(ssa);
  136.  
  137.     // Cerramos el control de servicio
  138.     CloseServiceHandle(schm);
  139.   end;
  140. end;



Su modo de uso sería:



delphi
  1. //Si es un equipo local
  2. ServiceGetList('',SERVICE_WIN32,SERVICE_STATE_ALL,ListBox1.Items);
  3.  
  4. //Algún equipo en RED
  5. ServiceGetList('\\ENECUMENE_PC',SERVICE_WIN32,SERVICE_STATE_ALL,ListBox1.Items);



Qué lo disfruten (y).

Saludos.
  • 0

#2 egostar

egostar

    missing my father, I love my mother.

  • Administrador
  • 14469 posts
  • LocationMéxico

Posted 22 September 2009 - 05:21 PM

Ah orale, se ve bien, gracias por el aporte amigo, ¿ esto no es como lo que hizo poliburro ?

Salud OS
  • 0

#3 enecumene

enecumene

    Webmaster

  • Administrador
  • 7419 posts
  • LocationRepública Dominicana

Posted 22 September 2009 - 05:23 PM

Ah orale, se ve bien, gracias por el aporte amigo, ¿ esto no es como lo que hizo poliburro ?

Salud OS


Gracias amigo, no, no es como lo de Poliburro, lo de él tiene que ver con el TaskManager, el mío sólo lista todos los servicios instalados, no los procesos que se está corriendo ;).

Saludos.
  • 0

#4 egostar

egostar

    missing my father, I love my mother.

  • Administrador
  • 14469 posts
  • LocationMéxico

Posted 22 September 2009 - 05:28 PM

Ah ok,

Bueno, pero estaría genial ponerle su "status" de iniciado o no :D, ahí se lo dejo de tarea :p

Salud OS
  • 0

#5 enecumene

enecumene

    Webmaster

  • Administrador
  • 7419 posts
  • LocationRepública Dominicana

Posted 22 September 2009 - 05:29 PM

Ah ok,

Bueno, pero estaría genial ponerle su "status" de iniciado o no :D, ahí se lo dejo de tarea :p

Salud OS


Pues no sería mala idea, :D, veré si puedo implementarla. ;)
  • 0

#6 Caral

Caral

    Advanced Member

  • Moderador
  • PipPipPip
  • 4266 posts
  • LocationCosta Rica

Posted 22 September 2009 - 07:31 PM

Hola
No te cansas amigo.
Exelente, esto es muy util.
Saludos
  • 0

#7 Wilson

Wilson

    Advanced Member

  • Moderadores
  • PipPipPip
  • 2137 posts

Posted 23 September 2009 - 07:56 AM

Buen aporte amigo.
Gracias. (y)
  • 0

#8 escafandra

escafandra

    Advanced Member

  • Administrador
  • 4111 posts
  • LocationMadrid - España

Posted 23 September 2009 - 10:52 AM

Buen ejemplo, enecumene  (y)

Saludos.
  • 0




IP.Board spam blocked by CleanTalk.