0

I am working on utility which is used to install service application. I want to add one more feature as install service with custom service name and display name.

So far I tried with using CreateProcess with command string as ExePath sysNameId:XXX -install. using this I am able install service but the name is not able to change.

Can anyone give me some leads on the same?

Below Is the code I am using to install service.

    function ExecuteServiceCommand(pStrCommand: string; var Output, ErrorList: TStringList): Boolean;
var
  startUpInfo : TStartupInfo;
  processInfo : TProcessInformation;
  SecurityAttr : TSecurityAttributes;
  hPipeErrorsRead    : THandle;
  hPipeErrorsWrite   : THandle;
  hPipeOutputRead    : THandle;
  hPipeOutputWrite   : THandle;
  CreationFlags     : DWORD;
begin
  //initializing ProcessInfo
  FillChar(ProcessInfo, SizeOf(TProcessInformation), 0);

  //Initializing SecurityAttr
  FillChar(SecurityAttr, SizeOf(TSecurityAttributes), 0);
  SecurityAttr.nLength              := SizeOf(TSecurityAttributes);
  SecurityAttr.bInheritHandle       := True;
  SecurityAttr.lpSecurityDescriptor := nil;

  //Create Piping handles
  CreatePipe(hPipeOutputRead, hPipeOutputWrite, @SecurityAttr, 0);
  CreatePipe(hPipeErrorsRead, hPipeErrorsWrite, @SecurityAttr, 0);

  //initialing StartupInfo
  FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
  StartupInfo.cb          := SizeOf(TStartupInfo);
  StartupInfo.hStdInput   := 0;
  StartupInfo.hStdOutput  := hPipeOutputWrite;
  StartupInfo.hStdError   := hPipeErrorsWrite;
  StartupInfo.wShowWindow := SW_HIDE;
  StartupInfo.dwFlags     := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;

  // define flags
  CreationFlags := CREATE_DEFAULT_ERROR_MODE or
                   CREATE_NEW_CONSOLE or
                   NORMAL_PRIORITY_CLASS;

  // execute the process and get output
  if CreateProcess(nil,
                PChar(pStrCommand),
                nil,
                nil,
                true,
                CreationFlags,
                nil,
                nil,
                startUpInfo,
                processInfo
                ) then
   begin
     Result := true;
     // close write handles
     CloseHandle(hPipeErrorsWrite);
     CloseHandle(hPipeOutputWrite);

     // do something to read console output
     CloseHandle(hPipeErrorsRead);
     CloseHandle(hPipeOutputRead);
   end
  else
   begin
        //DoSomething
   end;

  Result := true;

end;

Calling for the function ExecuteServiceCommand is

ServiceEXEname.exe sysNameId:XXXX -install

Thanks in Advance.

A B
  • 1,461
  • 2
  • 19
  • 54
  • You have to make your `TService` read the command line parameters during construction and change its `TService.Name` and `TService.DisplayName` properties accordingly, and then use the `TService.AfterInstall` event to manually update the service's registered `ImagePath` in the Registry to add the same parameters so they apply every time the service runs – Remy Lebeau Feb 19 '19 at 17:01
  • Hi @RemyLebeau i did the changes but unable to start the service it is showing message as `The XXX service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other services or programs.` – A B Mar 11 '19 at 05:26
  • The technique I described works fine when used correctly. So you must be doing something wrong when applying it to your project. – Remy Lebeau Mar 11 '19 at 15:32

0 Answers0