Services return values RRS feed

  • Question

  • I have been testing error situations with a service module. This module is written in C++ and closely modules the sample control handler provided at https://docs.microsoft.com/en-us/windows/win32/services/writing-a-control-handler-function

    When I force an error with so that the service does not start, I see a message from the Windows Services GUI stating :

    "Windows could not start the <service_name> on Local Computer. ... contact the service vendor, and refer to service-specific error code -1."

    What function in the handler returns this service-specific error code or what API can be used to set the error code?

    The last successful call from my service to SetServiceStatus sets the dwWin32ExitCode to -2 and the dwServiceSpecificExitCode to -4. The call to StartServiceCtrlDispatcher returns with a value of 1.  The service main function ends with a return value of 0.  I added a call to ExitProcess before the return in main which sets the exit code to -14. The prototype for the service control handler used in StartServiceCtrlDispatcher does not have a return value.

    I can not determine where the service-specific error code of -1 is being set.

    Monday, July 20, 2020 7:39 PM

All replies

  • Hi.

    I am updating a C++ module that interacts with Windows Server Controller. While producing some error situations, the services panel displays a message "Windows could not start the <service_name> on Local Computer. For more information, review the System Event Log. If this is a non-Microsoft service, contact the service vendor, and refer to service-specific error code -1."

    I am trying to figure out where the service-specific error code of -1 comes from and how to provide better return values.

    The last SetServiceStatus call sets dwServiceSpecificExitCode to -50, My service main function ends returning a value of 0. The Dispatch function (set with StartServiceCtrlDispatch) does not provide a return value. The call to StartServiceCtrlDispatch returns with a return value of 1.

    How is the service-specific error code shown in the services GUI message set or determined ? 

    Thanks for any input.


    Monday, July 20, 2020 5:08 PM
  • In order to reflect your service-specific exit code when you call SetServiceStatus you must set dwWin32ExitCode to ERROR_SERVICE_SPECIFIC_ERROR (1066).  If you do not do this the value of dwServiceSpecificExitCode is ignored.

    The above is documented here - https://docs.microsoft.com/en-us/windows/win32/api/winsvc/ns-winsvc-service_status

    It says -


    The error code the service uses to report an error that occurs when it is starting or stopping. To return an error code specific to the service, the service must set this value to ERROR_SERVICE_SPECIFIC_ERROR to indicate that the dwServiceSpecificExitCode member contains the error code. The service should set this value to NO_ERROR when it is running and on normal termination.


    A service-specific error code that the service returns when an error occurs while the service is starting or stopping. This value is ignored unless the dwWin32ExitCode member is set to ERROR_SERVICE_SPECIFIC_ERROR.

    For example, When I set the exit codes as documented with a service-specific exit code of -50 upon attempting to start the service and setting SERVICE_STOPPED this is the error message displayed -

    Monday, July 20, 2020 9:29 PM