Windows Services Programming 9






Debugging a Service


You can use any one of the following methods to debug your service.


  1. Use your debugger to debug the service while it is running. First, obtain the process identifier (PID) of the service process using Windows Task Manager for example. After you have obtained the PID, attach to the running process. However, using Visual Studio, you can attach to the running process directly from the IDE without knowing the PID.


Debugging: Attach to Process menu


Debugging: Attach to Process page


For syntax information, see the documentation included with your debugger.

  1. Call the DebugBreak() function to invoke the debugger for just-in-time debugging. DebugBreak() function will causes a breakpoint exception to occur in the current process. This allows the calling thread to signal the debugger to handle the exception. To cause a breakpoint exception in another process, use the DebugBreakProcess() function.
  2. Specify a debugger to use when starting a program. To do so, create a key called image File Execution Options in the following registry location:


HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion


Create a subkey with the same name as your service (for example, MYSERV.EXE). To this subkey, add a value of type REG_SZ, named Debugger. Use the full path to the debugger as the string value. In the Services control panel applet, select your service, click Startup and check Allow Service to Interact with Desktop. The service must be an interactive service, or else the debugger cannot run on the default desktop. Note that this technique is no longer supported as of Windows Vista because all services are run in session that is reserved exclusively for services and does not support displaying a user interface.

  1. Use Event Tracing to log information.


To debug the initialization code of an auto-start service, you will have to temporarily install and run the service as a demand-start service. At times, it may be necessary to run a service as a console application for debugging purposes. In this scenario, the StartServiceCtrlDispatcher() function will return ERROR_FAILED_SERVICE_CONTROLLER_CONNECT. Therefore, be sure to structure your code such that service-specific code is not called when this error is returned.





< Windows Services 8 | Win32 Programming | Windows Services Win32 Programming | Windows Services 10 >