Debugging of NT service programs
------------ Excerpt from the book Delphi 6 Application Development Guide
There are two ways to debug a service program. The first is to define the working part of the service in a separate class and debug it in a separate program. The second type is to debug when the service is running. The first approach is a good idea; adding classes responsible for service work to the usual program is the easiest way to test service behavior and maintain consistency with the idea of workbench testing.
We use the first method to test when creating two programs: ServiceApp and TestMailer (also available on the CD of this book). Add the same components TIdMessage, TTimer, and TIdSTMP to a separate program and create a mail sender. When the defect of the TestMailer program is found, all modifications are updated to the service program. Creating a workbench or testing a program is easy, but not always enough.
The second way to test a service program is to install and run the service program, and then attach it to the run process in Delphi. Follow these steps to debug the ServiceApp.exe program while running the service.
1. Run Serviceapp.exe /INSTALL on the command line to install the service program.
2. Select Start | Settings | Control Panel | Administrative Tools | Services to run the Services applet. Locate MyService and click the Start Service toolbar button (the steps are applicable to Windows 2000; the steps in Windows NT4.0 are nearly the same).
3. Run Delphi.
4. Load the ServiceApp.dPR project.
5. Select the Run | Attach to Process menu item in Delphi.
6. In the Attach to Process dialog box (as shown in the figure below), select the Show System Processes check box.
7. Locate ServiceApp.exe and click the Attach button.
8. In Delphi, set a breakpoint on the OnExecute event method of the project source file and press F9.
Figure B.3 Attach to Process dialog box can be used to attach a Delphi debugger to a running process; this is useful for debugging service programs.
Delphi will attach the debugger to the running service program and open the CPU dialog. Unless you read assembly language very smoothly, CPU views are not very useful. But when the service program runs to a breakpoint, it stops on the breakpoint and switches to the Delphi code view. After the user obtains control of the running service through Delphi, the service can be stepped and debugged like other programs.
Note: As a developer, you should have administrator rights on your PC. Incredibly, some organizations trust the code written by developers but do not allow them to manage their own PCs. The current economic situation is still good, so find a new job.
The Delphi help documentation mentions that if permissions are insufficient, attaching to the service process may fail. When you have administrator rights on your PC, the attached service process always works fine. The help topic "Debugging Services" contains a third way to debug services, which involves modifying registry settings, which you can refer to.
Ye Zhengsheng
2005-05-22