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.