Wolfgang Adamec Wolfgang Adamec - 2 months ago 10
C Question

Which control codes have be implemented in the control handler of a service

On this page (https://msdn.microsoft.com/en-us/library/windows/desktop/ms685996(v=vs.85).aspx) information about the
SERVICE_STATUS structure can be found. This structure has to filled out for the function call "SetServiceStatus".

Attribute number 3 is "dwControlsAccepted".

Unfortunately I have found no information about which control codes MUST ALWAYS be implemented/react to at least.

The page says:

By default, all services accept the SERVICE_CONTROL_INTERROGATE value.


But: Is there not a problem when the service control handler does not react to the control code "SERVICE_CONTROL_STOP"? Is there not a problem when the service control handler does not at least call "SetServiceStatus" in this case.

Thanks alot in advance

Answer

As far as dwControlsAccepted is concerned, there are no mandatory control codes. You can set this value to zero if that meets your needs. Apart from SERVICE_CONTROL_INTERROGATE your code does not need to handle any control codes that you have not specified as acceptable.

For example, if you have not set SERVICE_ACCEPT_STOP then Windows will never send you the SERVICE_CONTROL_STOP control. Any attempt to stop the service will result in error 1052, "The requested control is not valid for this service."

Note that unless you have a specific need to perform a clean shutdown (for example, because you have a database file that has to be properly closed) you do not need to accept shutdown controls either. Such a service will continue to run until the computer is actually powered down.

If you always set dwControlsAccepted to zero, this is all you need for a control handler:

static DWORD WINAPI ServiceHandlerEx(DWORD control, DWORD eventtype, LPVOID lpEventData, LPVOID lpContext) 
{
    if (control == SERVICE_CONTROL_INTERROGATE)
    {
        return NO_ERROR;
    }
    else
    {
        return ERROR_CALL_NOT_IMPLEMENTED;
    }
}
Comments