Solved

Starting service failed

Posted on 2003-12-10
6
649 Views
Last Modified: 2013-12-03
I am writing a service using the helper classes from the book "Programming Server-Size Applications for Microsoft Windows 2000" by Jeffrey Richter, Jason D. Clark. Starting the service in TimeServer book sample looks like this:

         case SERVICE_CONTROL_CONTINUE:
            hpipe = CreateNamedPipe(TEXT("\\\\.\\pipe\\TimeService"),
               PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED,
               PIPE_TYPE_BYTE, 1, sizeof(st), sizeof(st), 1000, NULL);

            iocp.AssociateDevice(hpipe, CK_PIPE);
            ZeroMemory(&o, sizeof(o));
            ConnectNamedPipe(hpipe, &o);
            g_ssTime.ReportUltimateState();
            break;

Since this is just a sample, there is no error handling and service start is supposed to succeed always. Appropriate code fragment from my program:

            case SERVICE_CONTROL_CONTINUE:
                if ( g_ServerSocket.Start(PORT_NUMBER) )
                {
                    // success
                    g_ssScan.ReportUltimateState();
                    g_EventLog.ReportEvent(EVENTLOG_INFORMATION_TYPE, CAT_APPEXECSTATUS, MSG_APPSTART);
                }
                else
                {
                    // failed
                    // What to do here ???
                }
                break;

I want to report error to SCM. Currently it tries to start my service a lot of time, I cannot say "Operation failed". Service remains in the state "Starting".
0
Comment
Question by:AlexFM
  • 3
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9912601
>>// What to do here ???

I don't know what 'ReportUltimateState()' does or whether there are other reporting functions, but the usual 'way of conduct' would be to set the service ststus to what is appropriate - that is 'SERVICE_STOPPED' if *starting* it failed - however, as the above code is about 'SERVICE_CONTROL_CONTINUE', which indicates resuming a paused service, 'SERVICE_PAUSED' might be appropriate.

0
 
LVL 11

Expert Comment

by:KurtVon
ID: 9912637
0
 
LVL 48

Author Comment

by:AlexFM
ID: 9912697
Thanks. Meanwhile I found some workaround:

                if ( g_ServerSocket.Start(PORT_NUMBER) )
                {
                    // CServiceStatus::ReportUltimateState changes current service status
                    // and calls SetServiceStatus API
                    g_ssScan.ReportUltimateState();
                    g_EventLog.ReportEvent(EVENTLOG_INFORMATION_TYPE, CAT_APPEXECSTATUS, MSG_APPSTART);
                }
                else
                {    
                    g_ssScan.SetUltimateState(SERVICE_STOPPED, 2000);
                    iocp.PostStatus(CK_SERVICECONTROL, SERVICE_CONTROL_STOP);
                }

As result of this, service reports Start and immidiately after this Stop. SCM is not so happy, but service state becomes Stopped. I hope there is a best solution.
Maybe somebody who worked service template from "Service Programming" book can help.
This code is in ServiceMain and not in HandlerEx function. HandlerEx already returned NO_ERROR. Maybe it is too late to report unsuccessful start and my workaround is the only solution?
0
 
LVL 86

Expert Comment

by:jkr
ID: 9912902
>>SCM is not so happy, but service state becomes Stopped. I hope there is a best solution.

Well, that *is* the solution, as the service status hardly can be anything else if the startup failed...
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 9978980
I see no reason for a deletion.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Does Microsoft Office 2016 support COM/ActiveX? 3 113
Slow process to read Excel 15 136
Input parameteres to DragOver 2 19
SSRS Deployment problem 5 64
This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question