473,785 Members | 2,321 Online
Bytes | Software Development & Data Engineering Community
+ 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::Service Process::Servic eBase.

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 SetServiceStatu s(SERVICE_STOPP ED), but either I got it wrong or
it just didn't work.
Nov 24 '05 #1
5 1816
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 SetServiceStatu s(SERVICE_STOPP ED), 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 SetServiceStatu s 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 SetServiceStatu s(SERVICE_STOPP ED), 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 SetServiceStatu s 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
.. SetServiceStatu s 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.dwServiceTyp e = SERVICE_WIN32_O WN_PROCESS;
st.dwControlsAc cepted = 0
| (parent_->CanStop ? SERVICE_ACCEPT_ STOP : 0)
| (parent_->CanShutdown ? SERVICE_ACCEPT_ SHUTDOWN : 0)
| (parent_->CanPauseAndCon tinue ? SERVICE_ACCEPT_ PAUSE_CONTINUE
: 0)
| (parent_->CanHandleSessi onChangeEvent ? SERVICE_ACCEPT_ SESSIONCHANGE :
0)
| (parent_->CanHandlePower Event ? SERVICE_ACCEPT_ POWEREVENT : 0)
;
st.dwCheckPoint = 0;

// now fill in our specific data
st.dwCurrentSta te = status;
st.dwWaitHint = waitHint;
st.dwWin32ExitC ode = failed_ ? ERROR_SERVICE_S PECIFIC_ERROR : NO_ERROR;
st.dwServiceSpe cificExitCode = failed_ ? 42 : 0; // random error number...
SetServiceStatu s(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 SetServiceStatu s 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_servi cemain_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 InitializeCompo nent() method, such as:

public ref class SimpleService: public System::Service Process::Servic eBase
{
...

private:

void InitializeCompo nent()
{
// Initialize the operating properties for the service.
this->CanStop= true;
this->CanPauseAndCon tinue = true;
this->CanShutdown = true;
this->ServiceName = "SimpleService" ;
this->CanHandleSessi onChangeEvent = 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 InitializeCompo nent() 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
4685
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 of the service is not set (this can be checked by right-clicking on the service in the Services window (Admin tools>Services), choosing Properties, LogOn tab). In order to enable my service to launch a GUI at startup, I added the following lines...
1
3884
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 that I work for develops software that monitors Customer Service Representative (CSR) performance. When you call a help desk and hear "This call may be monitored for quality purposes", the software that does this monitoring very likely has been...
4
2911
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 time failed.. I created an atl service through the wizard, and the only thing I do is override the PreMessageLoop()...
0
9822
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 mates. When I run a simple windows application which uses my web service I get the following exception which is thrown when my mates' web method is called. An unhandled exception of type 'System.Web.Services.Protocols.SoapException' occurred in...
0
10356
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10162
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7509
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5396
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.