Does anyone know how to debug a Delphi-written service (Service Application) on Windows?
I am making an API that get data from rclone and show it on an interface, and I want to make that into a Windows service. If there is anything that you think that can help me, I'd appreciate it.
You can debug it like any other app. The only trick is you have to start the service first in the SCM, and then you can attach Delphi's debugger to the service process, and then debugging works normally.
In my own services, I usually add code in the TService.OnStart
event to look for a /debugger
-style parameter in the TService.Param
property, and if present then use IsDebuggerPresent()
in a wait loop until the debugger attaches before continuing with my normal service logic. That value can be passed in the SCM startup parameters. For example:
procedure TMyService.ServiceStart(Sender: TObject;
var Started: Boolean);
var
I: Integer;
StartTicks: DWORD;
begin
for I := 0 to Self.ParamCount-1 do
begin
if Self.Param[I] = '/debugger' then
begin
StartTicks := GetTickCount;
repeat
Sleep(Self.WaitHint-100);
Self.ReportStatus;
until IsDebuggerPresent or
((GetTickCount - StartTicks) >= 30000);
end;
end;
Started := True;
end;