简体   繁体   中英

how to display startup error message in windows service?

So I wanted to create a windows service that runs a few commands in a batch file.

However, while I assume I can redirect stdout from the subprocess and read out an error message, I can't seem to find out how I would get that to display to the screen.

when starting a windows service, it gives errors when the service fails to start, so ideally I would like to just use the interface that pops up the other service errors rather than popping up some window of my own or writing a log file.

I used this to get started but it doesn't seem to have anything on error processing.

http://www.codeproject.com/Articles/499465/Simple-Windows-Service-in-Cplusplus

He just outputs to a debugger. I can definitely do that, but ideally the person starting the service would want to know if there was an error starting.

Yes, you can redirect the STDOUT of the spawned process. MSDN has an article on that topic:

Creating a Child Process with Redirected Input and Output

You can use ReportEvent() , EventWrite() , or TraceEvent() to write log messages to the System Event Log (which is located within Windows' Control Panel), depending on which logging API you decide to use. Refer to MSDN for more details:

Event Logging

Windows Event Log

A service is a background task, it should not display its own UIs. Use the system Event Viewer to view log messages. The popup the user sees if the service fails to start is not displayed by the service itself, but by the Service Control Manager.

Starting with Windows Vista, services do not run in the same desktop session as logged in users ( Session 0 Isolation ), so they cannot display their own UI anymore. If your service must display a popup message, it can use WTSSendMessage() for that. For more complex UIs, it is best to implement that as a separate non-service GUI app that runs in the user's session, and then the service can launch/communicate with that app as needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM