How to tell the SCM that a Service has failed?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have written a Service in C++ (VC8) derived from
System::ServiceProcess::ServiceBase.

In the OnStart() method I detect a service failure but the SCM reports that
the service started successfully. How can I tell the SCM that the service
failed?

I tried with SetServiceStatus(SERVICE_STOPPED), but either I got it wrong or
it just didn't work.
 
Hi Peter,
In the OnStart() method I detect a service failure, but the
SCM reports that the service started successfully. How
can I tell the SCM that the service failed?

I am afraid I am not very clear about the detailed scenario, do you mean
you detected the current service started failed on its OnStrat() method, or
detected some other service failed, and how do you detect it failed when
SCM reports it started successfully?
I tried with SetServiceStatus(SERVICE_STOPPED), but either I got it wrong or
it just didn't work.

The reason you cannot stopped a service may be some other running services
have dependency on it or you don't have enough permission to stop it. Have
you checked the return value of the SetServiceStatus call, is there any
useful info?


Thanks!

Best regards,

Gary Chang
Microsoft Community Support
--------------------
Get Secure! ¡§C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.asp
&SD=msdn

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hello Gary

"Gary Chang[MSFT]" said:
Hi Peter,
In the OnStart() method I detect a service failure, but the
SCM reports that the service started successfully. How
can I tell the SCM that the service failed?

I am afraid I am not very clear about the detailed scenario, do you mean
you detected the current service started failed on its OnStrat() method, or
detected some other service failed, and how do you detect it failed when
SCM reports it started successfully?

I detect the failure in the worker thread within the service during or after
OnStart() finishes in another thread.
The reason you cannot stopped a service may be some other running services
have dependency on it or you don't have enough permission to stop it. Have
you checked the return value of the SetServiceStatus call, is there any
useful info?

Thanks, that was the problem: I didn't initialize all members of the
SERVICE_STATUS struct. I looked at the example code from
http://msdn2.microsoft.com/en-us/library/system.serviceprocess.servicebase.aspx
.. SetServiceStatus is not called from C++, so I used the C# example, where
only the dwCurrentState member is ever assigned. Perhaps in C# all struct
members are implicitly initialized by the compiler?

Anyway, here is what I did to initialize (parent_ is derived from
ServiceBase; I hope this wraps ok):

SERVICE_STATUS st;
st.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
st.dwControlsAccepted = 0
| (parent_->CanStop ? SERVICE_ACCEPT_STOP : 0)
| (parent_->CanShutdown ? SERVICE_ACCEPT_SHUTDOWN : 0)
| (parent_->CanPauseAndContinue ? SERVICE_ACCEPT_PAUSE_CONTINUE
: 0)
| (parent_->CanHandleSessionChangeEvent ? SERVICE_ACCEPT_SESSIONCHANGE :
0)
| (parent_->CanHandlePowerEvent ? SERVICE_ACCEPT_POWEREVENT : 0)
;
st.dwCheckPoint = 0;

// now fill in our specific data
st.dwCurrentState = status;
st.dwWaitHint = waitHint;
st.dwWin32ExitCode = failed_ ? ERROR_SERVICE_SPECIFIC_ERROR : NO_ERROR;
st.dwServiceSpecificExitCode = failed_ ? 42 : 0; // random error number...
SetServiceStatus(hdl, &st);

Regards, Peter
 
Hi Peter,
I detect the failure in the worker thread within the service
during or after OnStart() finishes in another thread.

Would you please give more detailed info on how do you detect the service
failed, I think you may check the return value of the SetServiceStatus call
while you start that service, how about that?
...,so I used the C# example, where only the dwCurrentState
member is ever assigned. Perhaps in C# all struct
members are implicitly initialized by the compiler?

The C# compiler will not implicitly assign those members of the
Service_Status struct, I suggest you refer to the following platform SDK
documentation for reference, there has an example on how assign it in C++,
you may customize it to your own requirement:

Writing a ServiceMain Function
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/bas
e/writing_a_servicemain_function.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/bas
e/service_status_str.asp
SERVICE_STATUS

By the way, I think the reason you can not stop that service might be you
haven't set the CanStop property of your ServiceBase class, I suggest you
need to set it to true in its InitializeComponent() method, such as:

public ref class SimpleService: public System::ServiceProcess::ServiceBase
{
...

private:

void InitializeComponent()
{
// Initialize the operating properties for the service.
this->CanStop= true;
this->CanPauseAndContinue = true;
this->CanShutdown = true;
this->ServiceName = "SimpleService";
this->CanHandleSessionChangeEvent = true;
}
...
}


Thanks!

Best regards,

Gary Chang
Microsoft Community Support
--------------------
Get Secure! ¡§C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.asp
&SD=msdn

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hello Gary,

"Gary Chang[MSFT]" said:
Hi Peter,
[...]

By the way, I think the reason you can not stop that service might be you
haven't set the CanStop property of your ServiceBase class, I suggest you
need to set it to true in its InitializeComponent() method, such as:

After you helped me discover that I need to initialize the SERVICE_STATUS
structure, I *can* tell the SCM that the service stopped and my problem is
solved. Thanks again!

Regards,
Peter
 
Back
Top