Solved

Starting service failed

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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 article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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…

803 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