delphi - 如何使用 Delphi 调试 Windows 服务?

标签 delphi debugging windows-services service

有没有办法用 Delphi 完全调试 Windows 服务?

最佳答案

这实际上很简单。只需使用标准 DEBUG 编译器指令将服务作为控制台应用程序而不是服务启动。

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.

关于delphi - 如何使用 Delphi 调试 Windows 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2884631/

相关文章:

delphi - 如何在参数化查询中显式插入空值?

Delphi自定义Listview绘制

delphi - 如何从Delphi获取网站标题?

python - 在调试期间更新函数(pdb 或 ipdb)

java - UI 在 Debug模式下按预期更新,但在正常运行时不执行任何操作

windows - 在 Windows 8 中调试 Win64 应用程序时出现 "Unable to create process ...\project1.exe"

c# - 如何从 Windows 服务打开 cmd?

c# - Windows服务中的多线程

delphi - 如何检测 TTreeViewItem 节点何时在 TTreeView 内展开?

windows - 是否可以完全在 node.js 中编写 Windows 服务?