| Gerry Hickman 2006-06-28, 6:56 pm |
| Hi,
Have you read "DLL's Processes and Threads" in the docs? From what I can
tell, the SCM sends a stop request and you can then handle it a bit like
an event, but there's more to it (as explained in the docs).
"The ServiceMain function should call the RegisterWaitForSingleObject
function on an event and exit. This will terminate the thread that is
running the ServiceMain function, but will not terminate the service.
The service control handler should signal this event when the service
stops. A thread from the thread pool will execute the code that performs
the cleanup and calls SetServiceStatus with SERVICE_STOPPED."
#include <windows.h>
SERVICE_STATUS MyServiceStatus;
SERVICE_STATUS_HANDLE MyServiceStatusHandle;
VOID SvcDebugOut(LPSTR String, DWORD Status);
VOID WINAPI MyServiceCtrlHandler (DWORD Opcode)
{
DWORD status;
switch(Opcode)
{
case SERVICE_CONTROL_PAUSE:
// Do whatever it takes to pause here.
MyServiceStatus.dwCurrentState = SERVICE_PAUSED;
break;
case SERVICE_CONTROL_CONTINUE:
// Do whatever it takes to continue here.
MyServiceStatus.dwCurrentState = SERVICE_RUNNING;
break;
case SERVICE_CONTROL_STOP:
// Do whatever it takes to stop here.
MyServiceStatus.dwWin32ExitCode = 0;
MyServiceStatus.dwCurrentState = SERVICE_STOPPED;
MyServiceStatus.dwCheckPoint = 0;
MyServiceStatus.dwWaitHint = 0;
if (!SetServiceStatus (MyServiceStatusHandle,
&MyServiceStatus))
{
status = GetLastError();
SvcDebugOut(" [MY_SERVICE] SetServiceStatus error %ld\n",
status);
}
SvcDebugOut(" [MY_SERVICE] Leaving MyService \n",0);
return;
case SERVICE_CONTROL_INTERROGATE:
// Fall through to send current status.
break;
default:
SvcDebugOut(" [MY_SERVICE] Unrecognized opcode %ld\n",
Opcode);
}
// Send current status.
if (!SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus))
{
status = GetLastError();
SvcDebugOut(" [MY_SERVICE] SetServiceStatus error %ld\n",
status);
}
return;
}
Alok wrote:
> Does a windows services gets a chance to stop itslef and perform cleanup when
> the system is about to shutdown and reboot.
>
> How can I make an application which can catch the shutdown event so that it
> can perform cleanup before being shut down.
--
Gerry Hickman (London UK)
|