How to change service properties?
This page contains the two examples of how to configure Windows services. The first example is based on TWmiQuery, TWmiMethod, TWmiConnection components from WmiSet component collection. These components make use of WMI class Win32_Service and its "Change" method in particular. Click here to look at MSDN web site to read the detailed description of the parameters that may be changed. The second example utilizes TNTServiceManager component from NTSet component collection. The component encapsulates ChangeServiceConfig API function. There are some differences between the examples:
  • WmiSet components better handle the connection to the remote host. They use the WMI connection capabilities. NTSet components use "null session" method, which may fail if the current user already has a connection to destination host with different credentials.
  • NTSet components use pure API and work little bit faster.
Both components can change the following properties:
  • Display Name
  • Path to service's binary
  • Service Type
  • Error Control
  • Start Mode
  • if Interacts with Desktop
  • Start Name
  • Start Password
  • Load Order Group
  • Load Order Group Dependencies
  • Service Dependencies
The source code below creates and destroys the components on the fly, so it may be used in console and UI type applications.
How to configure services with WmiSet?  Download

const
  WQL = 'select * from Win32_Service where name = ''%s''';
  IGNORE = 0;

var
  Query: TWmiQuery;
  Connection: TWmiConnection;
  Method: TWmiMethod;
  s: string;
begin
  // setup: create components and link them together
  Query      := TWmiQuery.Create(nil);
  Connection := TWmiConnection.Create(nil);
  Method     := TWmiMethod.Create(nil);
  try
    Query.Connection := Connection;
    Method.WmiObjectSource := Query;
    // uncomment this block if configuring service on a remote host.
    // Connection.MachineName := '10.8.26.54';
    // Connection.Credentials.UserName := 'MyDomain\MyUser';
    // Connection.Credentials.Password := 'MyPassword';

    Connection.Connected := true;
    // Provide the name of the service to be configured.
    Query.WQL.Text := Format(WQL, ['Alerter']);
    Query.Active   := true;
    if not Query.EOF then
    begin
      Method.WmiMethodName := 'Change';
      Method.InParams.ParamByName('DisplayName').AsString := 'MyAlerter';
      Method.InParams.ParamByName('ErrorControl').AsInteger := IGNORE;
      // assign more parameters here as needed.
      
      if Method.Execute <> 0 then
      begin
        s := Method.LastWmiErrorDescription;
        if s = '' then s := 'Error '+ IntToStr(Method.LastWmiError);
        writeln(s);
      end else
      begin
        writeln('Service configured OK');
      end;
    end;

  finally;
    Query.Free;
    Connection.Free;
    Method.Free;
  end;
end.
How to configure services with NTSet?  Download

var
  vServiceMan: TNTServiceManager;
begin
  vServiceMan := TNTServiceManager.Create(nil);
  try
    // uncomment this block if configuring service on a remote host.
    // vServiceMan.LogonAs.UserName := 'MyDomain\MyUser';
    // vServiceMan.LogonAs.Password := 'MyPassword';
    // vServiceMan.MachineName := '\\10.8.26.54';
    try
      vServiceMan.ActiveManager := true;
      vServiceMan.ServiceName := 'Alerter';
      vServiceMan.ActiveService := true;
      vServiceMan.DisplayName := 'MyAlerter2';
      vServiceMan.ErrorControl := ERROR_IGNORE;
      // assign more paremeters here as needed.

      writeln('Service configured OK');
    except
      on E: Exception do
        writeln('Error: ' + E.Message);
    end;
  finally
    vServiceMan.ActiveService := false;
    vServiceMan.ActiveManager := false;
    vServiceMan.Free;
  end;
end.