浏览代码

set service description after service install

David Cornelius 1 年之前
父节点
当前提交
ea8893c2b0
共有 1 个文件被更改,包括 34 次插入2 次删除
  1. 34 2
      Quick.AppService.pas

+ 34 - 2
Quick.AppService.pas

@@ -96,15 +96,18 @@ type
     fOnStop : TSvcAnonMethod;
     fOnStop : TSvcAnonMethod;
     fOnExecute : TSvcAnonMethod;
     fOnExecute : TSvcAnonMethod;
     fAfterRemove : TSvcRemoveEvent;
     fAfterRemove : TSvcRemoveEvent;
+    fServiceDescription : string;
     procedure ReportSvcStatus(dwCurrentState, dwWin32ExitCode, dwWaitHint: DWORD);
     procedure ReportSvcStatus(dwCurrentState, dwWin32ExitCode, dwWaitHint: DWORD);
     procedure Execute;
     procedure Execute;
     procedure Help;
     procedure Help;
     procedure DoStop;
     procedure DoStop;
+    procedure SetServiceDescription;
   public
   public
     constructor Create;
     constructor Create;
     destructor Destroy; override;
     destructor Destroy; override;
     property ServiceName : string read fServiceName write fServiceName;
     property ServiceName : string read fServiceName write fServiceName;
     property DisplayName : string read fDisplayName write fDisplayName;
     property DisplayName : string read fDisplayName write fDisplayName;
+    property ServiceDescription : string read fServiceDescription write fServiceDescription;
     property LoadOrderGroup : string read fLoadOrderGroup write fLoadOrderGroup;
     property LoadOrderGroup : string read fLoadOrderGroup write fLoadOrderGroup;
     property Dependencies : string read fDependencies write fDependencies;
     property Dependencies : string read fDependencies write fDependencies;
     property DesktopInteraction : Boolean read fDesktopInteraction write fDesktopInteraction;
     property DesktopInteraction : Boolean read fDesktopInteraction write fDesktopInteraction;
@@ -138,6 +141,11 @@ var
 
 
 implementation
 implementation
 
 
+{$IFDEF MSWINDOWS}
+uses
+  Registry;
+{$ENDIF}
+
 procedure ServiceCtrlHandler(Control: DWORD); stdcall;
 procedure ServiceCtrlHandler(Control: DWORD); stdcall;
 begin
 begin
   case Control of
   case Control of
@@ -242,6 +250,27 @@ begin
   SetServiceStatus(StatusHandle,ServiceStatus);
   SetServiceStatus(StatusHandle,ServiceStatus);
 end;
 end;
 
 
+procedure TAppService.SetServiceDescription;
+{$IFDEF MSWINDOWS}
+var
+  reg: TRegistry;
+{$ENDIF}
+begin
+{$IFDEF MSWINDOWS}
+  reg := TRegistry.Create(KEY_READ or KEY_WRITE);
+  try
+    reg.RootKey := HKEY_LOCAL_MACHINE;
+    if reg.OpenKey('\SYSTEM\CurrentControlSet\Services\' + fServiceName, False) then
+    begin
+      reg.WriteString('Description', fServiceDescription);
+      reg.CloseKey;
+    end;
+  finally
+    reg.Free;
+  end;
+{$ENDIF}
+end;
+
 procedure TAppService.Execute;
 procedure TAppService.Execute;
 begin
 begin
   //we have to do something or service will stop
   //we have to do something or service will stop
@@ -320,7 +349,7 @@ begin
   end;
   end;
   //service interacts with desktop
   //service interacts with desktop
   if fDesktopInteraction then servicetype := SERVICE_WIN32_OWN_PROCESS and SERVICE_INTERACTIVE_PROCESS
   if fDesktopInteraction then servicetype := SERVICE_WIN32_OWN_PROCESS and SERVICE_INTERACTIVE_PROCESS
-    else servicetype := SERVICE_WIN32_OWN_PROCESS; 
+    else servicetype := SERVICE_WIN32_OWN_PROCESS;
   //service load order
   //service load order
   if fLoadOrderGroup.IsEmpty then svcloadgroup := nil
   if fLoadOrderGroup.IsEmpty then svcloadgroup := nil
     else svcloadgroup := PChar(fLoadOrderGroup);
     else svcloadgroup := PChar(fLoadOrderGroup);
@@ -333,7 +362,7 @@ begin
   //service user password
   //service user password
   if fUserPass.IsEmpty then svcuserpass := nil
   if fUserPass.IsEmpty then svcuserpass := nil
     else svcuserpass := PChar(fUserPass);
     else svcuserpass := PChar(fUserPass);
-    
+
   fSvHandle := CreateService(fSCMHandle,
   fSvHandle := CreateService(fSCMHandle,
                               PChar(fServiceName),
                               PChar(fServiceName),
                               PChar(fDisplayName),
                               PChar(fDisplayName),
@@ -348,6 +377,9 @@ begin
                               svcusername, //user
                               svcusername, //user
                               svcuserpass); //password
                               svcuserpass); //password
 
 
+  if Length(fServiceDescription) > 0 then
+    SetServiceDescription;
+
   if fSvHandle <> 0 then
   if fSvHandle <> 0 then
   begin
   begin
     if fSilent then Writeln(Format(cInstallMsg,[fServiceName]))
     if fSilent then Writeln(Format(cInstallMsg,[fServiceName]))