Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / November 2005

Tip: Looking for answers? Try searching our database.

How to tell the SCM that a Service has failed?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Peter Steiner - 24 Nov 2005 17:16 GMT
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.
"Gary Chang[MSFT]" - 25 Nov 2005 07:33 GMT
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.
Peter Steiner - 25 Nov 2005 13:24 GMT
Hello Gary

> Hi Peter,
>
[quoted text clipped - 6 lines]
> 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
[quoted text clipped - 4 lines]
> 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
"Gary Chang[MSFT]" - 28 Nov 2005 07:09 GMT
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.
Peter Steiner - 29 Nov 2005 07:25 GMT
Hello Gary,

> 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
"Gary Chang[MSFT]" - 29 Nov 2005 08:22 GMT
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.aspx?scid=/servicedesks/msdn/nospam.asp
&SD=msdn

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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.