Solved

Starting service failed

Posted on 2003-12-10
6
643 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

912 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now