BOOL StartServiceCtrlDispatcher(SERVICE_TABLE_ENTRY *Caracteristiques);
La fonction de rappel que le gestionnaire doit invoquer est définie dans Caracteristiques.
#include <windows.h>
#include <winbase.h>
SERVICE_TABLE_ENTRY Caracteristiques;
HANDLE HandleService;
SERVICE_STATUS ServiceStatus;
static void TraiterMessages(DWORD Message)
{
switch (Message)
{
...
}
}
static void ServiceProcedure(DWORD Argc, LPCSTR *Argv)
{
HandleService=RegisterServiceCtrlHandler("Mon service",(LPHANDLER_FUNCTION)TraiterMessages);
if (!HandleService)
{
...
}
...
ServiceStatus.dwServiceType=SERVICE_WIN32_OWN_PROCESS;
ServiceStatus.dwCurrentState=SERVICE_RUNNING;
ServiceStatus.dwControlsAccepted=SERVICE_ACCEPT_SHUTDOWN|SERVICE_ACCEPT_STOP;
ServiceStatus.dwWin32ExitCode=NO_ERROR;
ServiceStatus.dwServiceSpecificExitCode=0;
ServiceStatus.dwCheckPoint=0;
ServiceStatus.dwWaitHint=2000;
if (!SetServiceStatus(HandleService, &ServiceStatus))
{
...
}
...
}
Caracteristiques.lpServiceName="Mon gestionnaire";
Caracteristiques.lpServiceProc=ServiceProcedure;
if (!StartServiceCtrlDispatcher(&Caracteristiques))
{
...
}
...
ServiceStatus.dwServiceType=SERVICE_WIN32_OWN_PROCESS;
ServiceStatus.dwCurrentState=SERVICE_STOP_PENDING;
ServiceStatus.dwControlsAccepted=0;
ServiceStatus.dwWin32ExitCode=NO_ERROR;
ServiceStatus.dwServiceSpecificExitCode=0;
ServiceStatus.dwCheckPoint=0;
ServiceStatus.dwWaitHint=0;
if (!SetServiceStatus(HandleService, &ServiceStatus))
{
...
}
...
Cette Application Program Interface (API) ne fonctionne pas sur Windows 98 et Windows Millenium