Delphi 服务程序[4] 两栖服务(系统服务和桌面程序)

Delphi 服务程序[4] 两栖服务(系统服务和桌面程序)

方法1:

uses
  {$IFDEF DEBUG}
  Forms,
  {$ELSE}
  SvcMgr,
  {$ENDIF}
  Unit1 in 'Unit1.pas' {SerTest: TSerT},
  Unit2 in 'Unit2.pas' {Form2};

{$R *.RES}

begin
  Application.Initialize;
  {$IFDEF DEBUG}
  Application.CreateForm(TForm2, Form2);
  {$ELSE}
  Application.CreateForm(TSerTest, SerTest);
  {$ENDIF}
  Application.Run;

方法2:

uses
  SvcMgr,
  Forms,
  SysUtils,
  Windows,
  Dialogs,
  Unit2 in 'Unit2.pas' {Form2},
  Unit1 in 'Unit1.pas' {SerTest: TService};

{$R *.RES}

const
  CSMutexName = 'Global\Services_Application_Mutex';

var
  OneInstanceMutex: THandle;
  SecMem: SECURITY_ATTRIBUTES;
  aSD: SECURITY_DESCRIPTOR;

begin
  InitializeSecurityDescriptor(@aSD, SECURITY_DESCRIPTOR_REVISION);
  SetSecurityDescriptorDacl(@aSD, True, nil, False);
  SecMem.nLength := SizeOf(SECURITY_ATTRIBUTES);
  SecMem.lpSecurityDescriptor := @aSD;
  SecMem.bInheritHandle := False;
  OneInstanceMutex := CreateMutex(@SecMem, False, CSMutexName);
  if (GetLastError = ERROR_ALREADY_EXISTS) then
  begin
    ShowMessage('程序或服务已经运行!');
    Exit;
  end;
  if FindCmdLineSwitch('svc', True) or FindCmdLineSwitch('install', True) or FindCmdLineSwitch('uninstall', True) then
  begin
    SvcMgr.Application.Initialize;
    SvcMgr.Application.CreateForm(TSerTest, SerTest);
    SvcMgr.Application.Run;
  end
  else
  begin
    Forms.Application.Initialize;
    Forms.Application.CreateForm(TForm2, Form2);
    Forms.Application.Run;
  end;

  

 

 

创建时间:2021.01.21  更新时间:

 

posted on 2021-01-21 10:08  滔Roy  阅读(444)  评论(0编辑  收藏  举报

导航