[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 381
  • Last Modified:

Debug a service application

How do you debug a service application? If you run it from in delphi it never makes it to any of my code.
  • 2
  • 2
1 Solution
If you set delphi as your active debugger, you should be able to put a line of code 'DebugBreak' to set a run-time breakpoint in your code.

I use this all the time from C in multithreaded programs, but I'm not sure how well it'll work in Delphi.

(you might have to include debugging information in the .EXE using some project linker options)

essbbAuthor Commented:
so there isnt a simple way to 'step' through the code from the IDE?
Not that I know of...

In order to debug the service (while it is running as a service application) the host (the windows SCM - service control manager) must execute the .EXE and call the ServiceMain() function that is registered by the .EXE.

Because the host must execute the .EXE, you cannot 'step' through it because someone else (the host) took the first step.

Using the windows api call DebugBreak() will cause the system default debugger to execute and will allow you to step through the source code if the proper debugging symbols are present in the .EXE (or the debugger is smart enough to know where to find them).

I've just tested this, and it seems that even DebugBreak won't get you into the service (on my machine).

This is what MS has to say about it...

Debugging a Service
You can use the following methods to debug your service.

<LI>Use your debugger to debug the service while it is running. First, obtain the process identifier (PID) of the service process. This information is available from the <B>PView</B> application. After you have obtained the PID, attach to the running process. For syntax information, see the documentation included with your debugger.</LI>
<LI>Call the <B>DebugBreak</B> function to invoke the debugger for just-in-time debugging.<LI>
<LI>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:
<B>HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion</B>

Create a subkey with the same name as your service (for example, <B>MYSERV.EXE</B>). To this subkey, add a value of type REG_SZ, named <B>Debugger</B>. Use the full path to the debugger as the string value. In the Services control panel applet, select your service, click <B>Startup</B> and check <B>Allow Service to Interact with Desktop.</B></LI>

<B>Note</B>  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.

good luck!
essbbAuthor Commented:
Thanks for the help

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now