locked
why StartServiceCtrlDispatcher always returns 0? RRS feed

  • Question

  • Hi,

    I use an example ( Svc.cpp ) to implement windows service, but I do not understand why the StartServiceCtrlDispatcher function always returns 0. Why does the example ( SvcControl.cpp ) not use the StartServiceCtrlDispatcher function call, which leads to a critical error?

    Thanks


    • Edited by Vite Thursday, November 2, 2017 7:09 AM
    Thursday, November 2, 2017 7:09 AM

Answers

  • Sera Yu means that each process can only have one service control dispatcher running. So a service process will only have the first call to StartServiceCtrlDispatcher succeed. Any extra calls will fail and the last error code will be set to ERROR_SERVICE_ALREADY_RUNNING.

    Anyway, given the error code, that isn't the issue.

    Error 1063, according to the System Error Codes list, is  ERROR_FAILED_SERVICE_CONTROLLER_CONNECT. According to the documentation for StartServiceCtrlDispatcher, this error is returned when you try to run this application as a console application (i.e. you double click on the service executable or start it debugging through Visual Studio). To get a service to run, you need to install it into the service control manager using the CreateService function and then run it using the StartService function.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    • Marked as answer by Vite Thursday, November 2, 2017 12:34 PM
    Thursday, November 2, 2017 12:00 PM

All replies

  • Hi Vite,

    thanks for posting here.

    >>I use an example ( Svc.cpp ) to implement windows service, but I do not understand why the StartServiceCtrlDispatcher function always returns 0. Why does the example ( SvcControl.cpp ) not use the StartServiceCtrlDispatcher function call, which leads to a critical error?

    Could you please try to call  GetLastError to get the error code so that we could help you find the root cause.

    Please notice these things when you use StartServiceCtrlDispatcher method.

    1. Each process can call  StartServiceCtrlDispatcher only one time.

    2. Your program should run as a service rather than as a console application.

    Here is a example about how to write a Service program's main function, please refer to it.

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms687416(v=vs.85).aspx

    Best Regards,
    Sera Yu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 2, 2017 9:30 AM
  • I get the error code: 1063

    What do you mean by the definition "each process can call StartServiceCtrlDispatcher only once"? 

    When I start the service in manual mode, I do not get an error. But I need to implement some control by sending a command. I use the "Command Prompt" console to send parameters. Perhaps I misunderstood the specifics of the implementation of the program, for processing the commands of the control.

    Thursday, November 2, 2017 10:17 AM
  • Sera Yu means that each process can only have one service control dispatcher running. So a service process will only have the first call to StartServiceCtrlDispatcher succeed. Any extra calls will fail and the last error code will be set to ERROR_SERVICE_ALREADY_RUNNING.

    Anyway, given the error code, that isn't the issue.

    Error 1063, according to the System Error Codes list, is  ERROR_FAILED_SERVICE_CONTROLLER_CONNECT. According to the documentation for StartServiceCtrlDispatcher, this error is returned when you try to run this application as a console application (i.e. you double click on the service executable or start it debugging through Visual Studio). To get a service to run, you need to install it into the service control manager using the CreateService function and then run it using the StartService function.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    • Marked as answer by Vite Thursday, November 2, 2017 12:34 PM
    Thursday, November 2, 2017 12:00 PM
  • Darran Rowe, thanks for your reply. I used a workaround to avoid errors:

    if (argc == 1 && !(_tcscmp(argv[0], SERVICE_NAME) == 0) && 
       !StartServiceCtrlDispatcher(SvcDispatchTable)) {
        svc_report_event(_T("StartServiceCtrlDispatcher"));
    }

    Thursday, November 2, 2017 12:33 PM