Solved

Starting service failed

Posted on 2003-12-10
6
641 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
Comment Utility
>>// 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
Comment Utility
0
 
LVL 48

Author Comment

by:AlexFM
Comment Utility
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
Comment Utility
>>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
Comment Utility
I see no reason for a deletion.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
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 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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

15 Experts available now in Live!

Get 1:1 Help Now