Hi Jason,
Based on my understanding, you want to show some customized error from your
.net Windows Service application. Since you are referring to ExitCode
property, I am assuming you are using VS2005. Thanks.
First, I am not sure where do you want to show the customized erorr. In
Windows Service architecture, there are 2 processes: service process and
SCP process(which proivdes UI to the user and communicates with the service
process through any IPC technologies). Defaultly, Windows provided a
general style of SCP application services.msc as a MMC, so we normally use
this services.msc to manipulate the service applications. So my question is
do you want to show the customized error in standard services.msc or your
own written SCP application? To write a customized SCP application, we may
leverage ServiceController .Net class, please see the article below for
details:
"Communicate With Windows Services"
http://msdn.microsoft.com/library/en-us/dnvsm02/html/vs0201dt.asp?frame=true
In my test project, I created a sample Windows Service application, with
the following simple code:
protected override void OnStop()
{
this.ExitCode = 100;
}
In services.msc, I clicked this deployed service and choose "Stop" command
to trigger the test code. The services.msc will report a dialog with the
error below:
"Error 100: Cannot create another system semaphore.". Is this what you
want? Note: the "Cannot create another system semaphore" string is the
win32 predefined error message for error code 100. So your application can
report any predefined win32 standard error through ExitCode property.
If you want to report something that is not specific to win32 predefined
error code, you should report it through
SERVICE_STATUS.dwServiceSpecificExitCode field.(Note: SERVICE_STATUS is a
win32 SDK C++ structure which is encapsulated in .Net service classes, you
can look it in MSDN for more information). However, there is one problem
of using SERVICE_STATUS.dwServiceSpecificExitCode field to report
customized failure:
"SERVICE_STATUS structure is encapsulated in .Net BCL, however, it is
marked as private and .Net did not provide any public property to
expose.(although .Net provide ExitCode property to expose
SERVICE_STATUS.dwWin32ExitCode field), so it is impossible for us to use
this field in .Net code."
For testing purpose, I use VS2005 to debug my test Windows Service process,
and set a breakpoint in OnStop() method. When the execution breaks in
OnStop() method, I use the "Watch Window" to view "this.status" field,
which contains .Net encapsulated SERVICE_STATUS structure. In this window,
I modified dwWin32ExitCode to 1066L(ERROR_SERVICE_SPECIFIC_ERROR) and
dwServiceSpecificExitCode to 100.(Yes, debugger can ignore the .Net
protection and change the memory directly)
I really will get the following dialog in services.msc:
"Windows could not stop the WindowsServiceErrorReport on local computer.
For more information, review the System Event log. If it is a non-Microsoft
serivce, contact the service vendor, and refer to service-specific error
code 100"
I also got an error message in eventlog below:
"The WindowsServiceErrorReport service terminated with service-specific
error 100 (0x64)."
So SERVICE_STATUS.dwServiceSpecificExitCode really meets our need, however
it is not exposed by current .Net FCL.
Hope this helps.
Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.