473,473 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to tell the SCM that a Service has failed?

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.
Nov 24 '05 #1
5 1792
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 orit 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...sdn/nospam.asp
&SD=msdn

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

Nov 25 '05 #2
Hello Gary

""Gary Chang[MSFT]"" wrote:
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.
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, 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/lib...rvicebase.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

Nov 25 '05 #3
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/de...us/dllproc/bas
e/writing_a_servicemain_function.asp

http://msdn.microsoft.com/library/de...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...sdn/nospam.asp
&SD=msdn

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

Nov 28 '05 #4
Hello Gary,

""Gary Chang[MSFT]"" wrote:
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

Nov 29 '05 #5
That's great, Peter! I am glad to help you on this issue:)

Good Luck!
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...sdn/nospam.asp
&SD=msdn

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

Nov 29 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: letibal | last post by:
Hello, I have written a windows service and created an installer for it. The service runs under the system accounts. When started, it launches a GUI. By default, the InteractiveProcess property...
1
by: Tom Yee | last post by:
I would like to write a Windows Service that can communicate with any open browser windows that an interactive user may be running. The service itself does not need to have a UI. The company...
4
by: Jianwei Sun | last post by:
Hi, all, I have a question, and I hope to get some hints here.. I created a really simple service just to check why the service manager doesn't start my service the second time if the first...
0
by: yoozioo | last post by:
hello, This year I'm learning C# at school and now we focus on web services. I created and published a web service which contains a method that calls another web service written by my class...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.