Event Logging
Services frequently run "headless" without user interaction, so it is not generally appropriate for a service to display status messages directly. Some services will create a console, message box,[2] or window for user interaction, but the best technique is to log events to a log file or use the event logging functionality provided with Windows. Such events are maintained in the registry and can be viewed from the event viewer provided in the control panel's Administrative Tools.
[2] If a service calls MessageBox, it must specify MB_SERVICE_NOTIFICATION for the message box type. The message is then displayed on the active desktop, even if no user is logged on to the computer.
The serviceSK.c and SimpleService.c programs on the book's Web site show how to log significant service events and errors to a log file, and commented code shows how to use event logging. There are three functions, all described in the on-line documentation.
- RegisterEventSource obtains a handle to the log file.
- ReportEvent is used to register a record in the log file.
- DeregisterEventSource closes the handle to the log file.