Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > API de Windows
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 16-11-2006
Avatar de aeff
aeff aeff is offline
Miembro
 
Registrado: oct 2006
Ubicación: Cuba, Guantánamo
Posts: 348
Poder: 18
aeff Va camino a la fama
Question Servicios???

hola colegas del CLub, necesito saber como puedo manejar, crear, eliminar o modificar servicios, he visto que en la unit WinSvc existen varias funciones para esto pero no me sale ni de "chamba" como hacerlas funcionar...

gracias de antemano...
Responder Con Cita
  #2  
Antiguo 16-11-2006
Avatar de roman
roman roman is offline
Moderador
 
Registrado: may 2003
Ubicación: Ciudad de México
Posts: 20.269
Poder: 10
roman Es un diamante en brutoroman Es un diamante en brutoroman Es un diamante en bruto
Pues un buen punto de partida sería el truco 45 del compañero seoane.

// Saludos
Responder Con Cita
  #3  
Antiguo 16-11-2006
Avatar de aeff
aeff aeff is offline
Miembro
 
Registrado: oct 2006
Ubicación: Cuba, Guantánamo
Posts: 348
Poder: 18
aeff Va camino a la fama
Wink eso está excelente!!!

ahora necesito saber como crear un nuevo servicio... para luego iniciarlo con el algoritmo que planteaba el compañoero seaone

y me pregunto, ¿me pueden ayudar en esto?


gracias de antemano...
Responder Con Cita
  #4  
Antiguo 16-11-2006
Avatar de Neftali [Germán.Estévez]
Neftali [Germán.Estévez] Neftali [Germán.Estévez] is offline
[becario]
 
Registrado: jul 2004
Ubicación: Barcelona - España
Posts: 18.269
Poder: 10
Neftali [Germán.Estévez] Es un diamante en brutoNeftali [Germán.Estévez] Es un diamante en brutoNeftali [Germán.Estévez] Es un diamante en bruto
Revisa estos links:
* Creating NT Services in Delphi
* Tutorial for making Service Applications using Delphi

Son un par que tengo yo, pero si buscar por internet seguro que encuentras más.
__________________
Germán Estévez => Web/Blog
Guía de estilo, Guía alternativa
Utiliza TAG's en tus mensajes.
Contactar con el Clubdelphi

P.D: Más tiempo dedicado a la pregunta=Mejores respuestas.
Responder Con Cita
  #5  
Antiguo 16-11-2006
Avatar de seoane
[seoane] seoane is offline
Miembro Premium
 
Registrado: feb 2004
Ubicación: A Coruña, España
Posts: 3.717
Poder: 24
seoane Va por buen camino
Hola aeff, por mensajes anteriores veo que eres de los que te gusta investigar y complicarse, que no te quedas con lo fácil. Pues bien, aquí te dejo un trocito de código que te puede resultar interesante. Es una plantilla para crear un servicio usando solo C, de hecho lo cree para poderlo compilar con el MinGW, y compila perfectamente.

Para personalizarlo, cambia las constantes que aparecen al principio del programa:
Código:
#define VERSION "Version: 0.0"
#define SERVICE_NAME "ServTemplate"
#define DISPLAY_NAME "Service Template"
#define SERVICE_TYPE SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS
#define START_TYPE SERVICE_AUTO_START
Y coloca tu código dentro de los 3 eventos que aparecen al final:
Código:
BOOL OnStart()
{ 
 // Si devuleves FALSE el servicio no se inica y muestra un error
 return TRUE;
}

void OnStop()
{
 // El servicio recibio la orden de pararse
}

void OnExecute()
{
 do
 {
  // Aqui hacemos un bucle, sino al terminar esta rutina el servicio acabaria
 } while (!Terminated(500));
}
Una vez compilado (si lo haces con MinGW usa la opción -mwindows), puedes usar varios parámetros en la linea de comandos:
Código:
/INSTALL  -INSTALL         Instala el servicio
/DELETE   -DELETE           Elimina el servicio
/START    -START            Inicia el servicio
/STOP      -STOP             Para el servicio
/VERSION  -VERSION        Muestra un mensaje con informacion
Bueno, aquí te dejo el código. Si te animas a hacerlo en C esto te puede ser de mucha ayuda, si lo quieres hacer en delphi, puede que un vistazo al código de C te aclare muchas cosas.

Código:
#include <windows.h>

#define DEBUGGING

#define VERSION "Version: 0.0"
#define SERVICE_NAME "ServTemplate"
#define DISPLAY_NAME "Service Template"
#define SERVICE_TYPE SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS
#define START_TYPE SERVICE_AUTO_START

// Debug
void ShowError(char* s);

// NT Service
SERVICE_STATUS ServiceStatus;
SERVICE_STATUS_HANDLE ServiceStatusHandle = 0;
HANDLE StopServiceEvent = 0;

void WINAPI ServiceControlHandler(DWORD fdwControl);
void WINAPI ServiceMain(DWORD dwArgc,LPTSTR *lpszArgv);
void InstallService();
void UninstallService();
void StartSrv();
void StopService();
BOOL Terminated(DWORD Milliseconds);
BOOL OnStart();
void OnStop();
void OnExecute();

int main(int argc, char *argv[])
{
 if (argc == 2)
 {
  if (stricmp(argv[1],"/INSTALL")==0 || stricmp(argv[1],"-INSTALL")==0) 
  {
   InstallService(); 
   return 0;
  }
  if (stricmp(argv[1],"/DELETE")==0 || stricmp(argv[1],"-DELETE")==0) 
  {
   UninstallService(); 
   return 0;
  }  
  if (stricmp(argv[1],"/START")==0 || stricmp(argv[1],"-START")==0) 
  {
   StartSrv(); 
   return 0;
  } 
  if (stricmp(argv[1],"/STOP")==0 || stricmp(argv[1],"-STOP")==0) 
  {
   StopService(); 
   return 0;
  }
  if (stricmp(argv[1],"/VERSION")==0 || stricmp(argv[1],"-VERSION")==0) 
  {   
   MessageBox(0,VERSION,DISPLAY_NAME,MB_OK);
   return 0;
  } 
 }
 
 SERVICE_TABLE_ENTRY ServiceTable[] =
 {
  {SERVICE_NAME, ServiceMain},
  { 0, 0 }
 };
 if (!StartServiceCtrlDispatcher(ServiceTable))
   ShowError("main - StartServiceCtrlDispatcher");

 return 0;
}

void ShowError(char* s)
{
#ifdef DEBUGGING
 DWORD MessageId;
 LPVOID MsgBuf;
 
 MessageId = GetLastError();
 OutputDebugString(s);
 if (MessageId != NO_ERROR)
 {
  FormatMessage( 
    FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,NULL,MessageId,
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPTSTR) &MsgBuf,0,NULL); 
  OutputDebugString(MsgBuf);            
  LocalFree(MsgBuf);              
 }
#endif
}

void WINAPI ServiceControlHandler(DWORD fdwControl)
{
 switch (fdwControl)
 {
  case SERVICE_CONTROL_INTERROGATE:
    break;
  case SERVICE_CONTROL_SHUTDOWN:
  case SERVICE_CONTROL_STOP:
    SetEvent(StopServiceEvent);
    ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
    break;
 }
 SetServiceStatus(ServiceStatusHandle, &ServiceStatus );
}

void WINAPI ServiceMain(DWORD dwArgc,LPTSTR *lpszArgv)
{   
 ServiceStatus.dwServiceType = SERVICE_WIN32;
 ServiceStatus.dwCurrentState = SERVICE_STOPPED;
 ServiceStatus.dwControlsAccepted = 0;
 ServiceStatus.dwWin32ExitCode = NO_ERROR;
 ServiceStatus.dwServiceSpecificExitCode = NO_ERROR;
 ServiceStatus.dwCheckPoint = 0;
 ServiceStatus.dwWaitHint = 0;

 ServiceStatusHandle = RegisterServiceCtrlHandler(SERVICE_NAME,ServiceControlHandler);
 if (ServiceStatusHandle)
 {
  ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
  SetServiceStatus(ServiceStatusHandle, &ServiceStatus );
  StopServiceEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  if (StopServiceEvent)
  {
   if (OnStart())
   {
    ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
    ServiceStatus.dwCurrentState = SERVICE_RUNNING;
    SetServiceStatus(ServiceStatusHandle, &ServiceStatus );
    OnExecute();
   } else OutputDebugString("ServiceMain - OnStart");   
   CloseHandle(StopServiceEvent);
   StopServiceEvent = NULL;
  } else ShowError("ServiceMain - CreateEvent"); 
  ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
  SetServiceStatus(ServiceStatusHandle, &ServiceStatus );
  OnStop();
  ServiceStatus.dwControlsAccepted = 0;
  ServiceStatus.dwCurrentState = SERVICE_STOPPED;
  SetServiceStatus(ServiceStatusHandle, &ServiceStatus );
 } else ShowError("ServiceMain - RegisterServiceCtrlHandler");
}

void InstallService()
{
 SC_HANDLE ServiceControlManager;
 SC_HANDLE Service;
 char Path[MAX_PATH + 1]; 
 
 ServiceControlManager = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
 if (ServiceControlManager)
 {
  memset(Path,0,sizeof(Path));
  if (GetModuleFileName(0,Path,sizeof(Path)-1) > 0)
  {
   Service = CreateService(ServiceControlManager,SERVICE_NAME, DISPLAY_NAME,
						   SERVICE_ALL_ACCESS, SERVICE_TYPE, START_TYPE,
						   SERVICE_ERROR_IGNORE, Path, 0, 0, 0, 0, 0 );
   if (Service)
   {
    CloseServiceHandle(Service);
   } else ShowError("InstallService - CreateService");  
  } else ShowError("InstallService - GetModuleFileName"); 
  CloseServiceHandle(ServiceControlManager);
 } else ShowError("InstallService - OpenSCManager");
}

void UninstallService()
{
 SC_HANDLE ServiceControlManager; 
 SC_HANDLE Service;
 SERVICE_STATUS ServiceStatus; 
 
 ServiceControlManager = OpenSCManager( 0, 0, SC_MANAGER_CONNECT );
 if (ServiceControlManager)
 {
  Service = OpenService(ServiceControlManager,SERVICE_NAME,SERVICE_ALL_ACCESS | DELETE);
  if (Service)  
  {   
   if (QueryServiceStatus(Service, &ServiceStatus ))
   {
    if (ServiceStatus.dwCurrentState != SERVICE_STOPPED )
    {
     if (ControlService(Service, SERVICE_CONTROL_STOP, &ServiceStatus))
     {
      Sleep(500);
      if (QueryServiceStatus(Service, &ServiceStatus ))
      { 
       if (ServiceStatus.dwCurrentState == SERVICE_STOPPED )
         DeleteService(Service);  
      } else ShowError("UninstallService - QueryServiceStatus");                           
     } else ShowError("UninstallService - ControlService");                               
    } else DeleteService(Service);
   } else ShowError("UninstallService - QueryServiceStatus");
   CloseServiceHandle(Service);
  } else ShowError("UninstallService - OpenService");
  CloseServiceHandle(ServiceControlManager);
 } else ShowError("UninstallService - OpenSCManager");
}

void StartSrv()
{
 SC_HANDLE ServiceControlManager; 
 SC_HANDLE Service;
 SERVICE_STATUS ServiceStatus; 
 
 ServiceControlManager = OpenSCManager( 0, 0, SC_MANAGER_CONNECT );
 if (ServiceControlManager)
 {
  Service = OpenService(ServiceControlManager,SERVICE_NAME,SERVICE_ALL_ACCESS);
  if (Service)  
  {   
   if (QueryServiceStatus(Service, &ServiceStatus ))
   {
    if (ServiceStatus.dwCurrentState != SERVICE_RUNNING )
      StartService(Service, 0, NULL);
   } else ShowError("StartSrv - QueryServiceStatus");
   CloseServiceHandle(Service);
  } else ShowError("StartSrv - OpenService");
  CloseServiceHandle(ServiceControlManager);
 } else ShowError("StartSrv - OpenSCManager");
}

void StopService()
{
 SC_HANDLE ServiceControlManager; 
 SC_HANDLE Service;
 SERVICE_STATUS ServiceStatus; 
 
 ServiceControlManager = OpenSCManager( 0, 0, SC_MANAGER_CONNECT );
 if (ServiceControlManager)
 {
  Service = OpenService(ServiceControlManager,SERVICE_NAME,SERVICE_ALL_ACCESS);
  if (Service)  
  {   
   if (QueryServiceStatus(Service, &ServiceStatus ))
   {
    if (ServiceStatus.dwCurrentState != SERVICE_STOPPED )
      ControlService(Service, SERVICE_CONTROL_STOP, &ServiceStatus); 
   } else ShowError("StopService - QueryServiceStatus");
   CloseServiceHandle(Service);
  } else ShowError("StopService - OpenService");
  CloseServiceHandle(ServiceControlManager);
 } else ShowError("StopService - OpenSCManager");
}

BOOL Terminated(DWORD Milliseconds)
{
 return (WaitForSingleObject(StopServiceEvent, Milliseconds) != WAIT_TIMEOUT);    
}

// Insertar codigo a partir de aqui

BOOL OnStart()
{ 
 return TRUE;
}

void OnStop()
{
}

void OnExecute()
{
 do
 {
  
 } while (!Terminated(500));
}
Responder Con Cita
  #6  
Antiguo 17-11-2006
Avatar de aeff
aeff aeff is offline
Miembro
 
Registrado: oct 2006
Ubicación: Cuba, Guantánamo
Posts: 348
Poder: 18
aeff Va camino a la fama
Question tengo duda todavía

estimado seoane, tu trocito de código esta excelente para C, he llevado una parte de ello a Delphi, logré instalar el servicio, pero no se inicia...

pienso que el problema está en la función OnStart, pero en delphi no se como implementarla...

¿puedes continuar ayudándome?

gracias de antemano
Responder Con Cita
  #7  
Antiguo 17-11-2006
Avatar de seoane
[seoane] seoane is offline
Miembro Premium
 
Registrado: feb 2004
Ubicación: A Coruña, España
Posts: 3.717
Poder: 24
seoane Va por buen camino
Vamos por partes, ¿que estas intentando hacer?, me explico, estas intentando hacer un programa para controlar un servicio (instalar/desintalar/iniciar/parar) o estas creando un servicio. Si es lo primero la funcion OnStart que aparece en mi trozo de codigo no tienen nada que ver, si es lo segundo, Delphi puede crear un servicio facilmente usando la clase TService.

Asi que, cuentanos un poco lo que estas haciendo para asi poder ayudarte
Responder Con Cita
  #8  
Antiguo 17-11-2006
Avatar de aeff
aeff aeff is offline
Miembro
 
Registrado: oct 2006
Ubicación: Cuba, Guantánamo
Posts: 348
Poder: 18
aeff Va camino a la fama
Unhappy continuamos con los inventos!!!

bueno, ya que estás dispuesto a ayudarme déjame explicarte lo que quiero hacer...

Estoy haciendo una programa que es para que determinadas aplicaciones no se ejecuten, necesito que este programa se ejecute cada vez que se inician las sesiones en Windows, no quiero escribir en el registro (en ...Run...) para hacer esto, lo que quiero es crear un servicio por el cual mi programa se ejecute al iniciar las sesiones, ahora, el servicio lo voy a mandar a craer cuando estoy instalando el programa, es decir, en otro programa que hará función de instalador es donde voy descomprimir el programa para una carpeta y luego instalar el servicio e iniciarlo...

el programa ya tiene una interfáz gráfica, botones, listbox, etc...
así que pienso que sea un poco más complicado...

**
si me puedes dar un empuón con esa clase TService... veamos!!!
Responder Con Cita
  #9  
Antiguo 17-11-2006
Avatar de seoane
[seoane] seoane is offline
Miembro Premium
 
Registrado: feb 2004
Ubicación: A Coruña, España
Posts: 3.717
Poder: 24
seoane Va por buen camino
Cita:
Empezado por aeff
Estoy haciendo una programa que es para que determinadas aplicaciones no se ejecuten, necesito que este programa se ejecute cada vez que se inician las sesiones en Windows
Pues mal vamos, porque los servicios no se ejecutan al iniciar sesión, sino mucho antes. Precisamente una de sus principales características es que se ejecutan de forma independiente a las sesiones de los usuarios. Es decir, si esta en arranque automático, arrancara al encender el equipo antes incluso de iniciar sesión y aunque cierres la sesión, o cambies de usuario seguirá ejecutándose sin inmutarse. Además los servicios a partir del nuevo vista no podrán interactuar con el usuario, así que olvidate de mostrar nada por pantalla, o ver lo que esta haciendo el usuario.

Hablando de sistemas operativos, antes de que sigamos mas adelante ¿que sistema operativo usas? no vaya a ser que tengas un windows 98 y estemos aquí haciendo el tonto Los servicios solo se pueden usar en sistemas NT es decir Windows2000, WindwsXP, Windowx2003, Vista.

Teniendo en cuenta lo dicho arriba. ¿Por que no quieres usar las claves del registro para ejecutar tu aplicación? ¿Por que no utilizas los scripts de inicio de sesión?

Cita:
el programa ya tiene una interfáz gráfica, botones, listbox, etc...
así que pienso que sea un poco más complicado...
Bueno, como ya te dije mas arriba. Aunque windowsxp todavía permite a los servicios interactuar con el usuario, no así vista, no es algo recomendable. Los servicios como normal general no deben interactuar con el usuario, saltarse esto puede traer muchas complicaciones y provocar no pocos errores. Lo normal es hacer una aplicación normal como interfaz gráfica y que esta se comunique con el servicio, a través de sockets, tuberías, memoria compartida, etc, de esta forma el servicio se ejecuta de forma independiente al usuario, cuando se necesita cambiar algo de la configuración, por ejemplo, se abre la aplicación de usuario, se hacen los cambios necesarios y esta se lo comunica al servicio.

Cita:
si me puedes dar un empuón con esa clase TService... veamos!!!
Para comenzar un servicio en delphi, crea un proyecto nuevo y en tipo de proyecto escoge "Service Application". Se creara un nuevo proyecto con un TService. Puedes usar las propiedades y eventos de este objeto para crear tu propio servicio. Además el ejecutable que se genera se puede instalar usando el modificador /install en la linea de comandos, así que ya no tienes que preocuparte de como instalarlo.
Responder Con Cita
  #10  
Antiguo 18-11-2006
Avatar de aeff
aeff aeff is offline
Miembro
 
Registrado: oct 2006
Ubicación: Cuba, Guantánamo
Posts: 348
Poder: 18
aeff Va camino a la fama
Thumbs up gracias!!!

estoy muy agradecido, aunque no del todo he resuelto los problemas, esa Tservice me quita gran parte de ellos, aún así conservo tu trocito de código para continuar estudiando...

gracias!!!
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Delphi y Servicios kavisch Varios 1 09-05-2006 21:30:16
Servicios Web Ricardo León Internet 4 22-09-2005 15:42:36
Servicios de IBReplicator AMINOA2R Firebird e Interbase 1 22-07-2005 01:58:42
Servicios en c# edgusano .NET 1 13-04-2005 03:19:05
Servicios arantzal .NET 1 11-11-2004 14:36:46


La franja horaria es GMT +2. Ahora son las 03:48:47.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi