Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug a windows service with Delphi?

Is there a way to debug completely a windows service with Delphi?

like image 268
Daniel Grillo Avatar asked May 21 '10 18:05

Daniel Grillo


People also ask

How do I debug a Windows service?

In the Options dialog box, choose Debugging, Symbols, select the Microsoft Symbol Servers check box, and then choose the OK button. The Processes dialog box appears. Select the Show processes from all users check box. In the Available Processes section, choose the process for your service, and then choose Attach.

How do I debug in Delphi?

Debugging delphi source files You need to go to "project->options->compiler" on this tab you need to check the "use debug DCUs". After that you need to build your project again and you can run your application. From now on breakpoints also stop in Delphi source files.

How do I create a Windows Service in Delphi?

How to create Windows Service in Delphi? To create a service application in Delphi start Delphi IDE and select File->New->Other. Then select "Service Application". Then you can see application which includes a Datamodule descendent Tservice class.


1 Answers

It's actually quite easy. Just use the standard DEBUG compiler directive to start the service as a console application instead of a service.

program MyServiceApp;  {$ifdef DEBUG}   {$APPTYPE CONSOLE} {$endif}  uses   System.SysUtils, 

[..]

begin   {$ifdef DEBUG}   try     // In debug mode the server acts as a console application.     WriteLn('MyServiceApp DEBUG mode. Press enter to exit.');      // Create the TService descendant manually.     ServerContainer1 := TServerContainer.Create(nil);      // Simulate service start.     ServerContainer1.ServiceStart(ServerContainer1, MyDummyBoolean);      // Keep the console box running (ServerContainer1 code runs in the background)     ReadLn;      // On exit, destroy the service object.     FreeAndNil(ServerContainer1);   except     on E: Exception do     begin       Writeln(E.ClassName, ': ', E.Message);       WriteLn('Press enter to exit.');       ReadLn;     end;   end;   {$else}   // Run as a true windows service (release).   if not Application.DelayInitialize or Application.Installing then     Application.Initialize;   Application.CreateForm(TServerContainer, ServerContainer1);   Application.Run;   {$endif} end. 
like image 59
Scott Beltgens Avatar answered Sep 24 '22 03:09

Scott Beltgens