Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Trucos (https://www.clubdelphi.com/foros/forumdisplay.php?f=52)
-   -   Crear un servicio en C (https://www.clubdelphi.com/foros/showthread.php?t=80773)

seoane 16-11-2006 14:11:35

Crear un servicio en C
 
Aquí dejo una plantilla para crear un servicio en C, en concreto este esta creado para compilar con MinGW pero no creo que sea muy difícil compilarlo en otro compilador.

Para personalizarlo, solo hay que cambiar estas constantes:
Código:

#define VERSION "Version: 0.0"  // Esta informacion se mostrara cuando se utilice el parameto /Version
#define SERVICE_NAME "ServTemplate" // El nombre con el que se identificara al servicio
#define DISPLAY_NAME "Service Template" // El nombre que se mostrara en la lista de servicios
#define SERVICE_TYPE SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS // Tipo de servico
#define START_TYPE SERVICE_AUTO_START // Indica si el servicio se inciara automaticamente

Nuestro código lo pondremos en estos 3 eventos:
Código:

BOOL OnStart()
{
 // Si devuelves 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í esta el código completo:
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));
}

Enlaces de interés
MinGW - http://www.mingw.org/
DebugView - http://www.microsoft.com/technet/sysinternals/Miscellaneous/DebugView.mspx


La franja horaria es GMT +2. Ahora son las 22:58:20.

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