How to enumerate services?
This page contains the two examples of how to enumerate Windows services. They both produce the same result. The first example is based on TWmiQuery and TWmiConnection components from WmiSet component collection. The second example utilizes TNTServiceManager component from NTSet component collection. There are minor differences in 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.
The source code below creates and destroys the components on the fly, so these functions may be used in console type applications. With minor changes the functions may be adapted for UI-type apps.
How to enumerate services with WmiSet?  Download

// this function lists Windows Services and their states.
// It may be used for local or remote host. When used for local host
// all parameters must be empty. 
// For remote computer the parameters must be: 
// TargetHost: name or IP address of the remote host, like '10.8.36.54' 
// UserName: name of the user, may include domain, like 'MOON\Administrator'; 
// Password: the user's password. 
procedure ListServices(TargetHost, UserName, Password: string);
var 
  Connection: TWmiConnection;
  Query: TWmiQuery;
begin
  Query := TWmiQuery.Create(nil);
  Connection := TWmiConnection.Create(nil);
  try
    Query.Connection := Connection;
    Query.WQL.Text := 'select * from Win32_Service';
    Connection.MachineName := TargetHost;
    Connection.Credentials.UserName := UserName;
    Connection.Credentials.Password := Password;

    try
      // exception may happen on Win9x, WinNT if WMI core is not installed; 
      // The provided credentials may also be invalid. 
      Connection.Connected := true;
    except
      writeln('Cannot connect to the destination host');
      Exit;
    end;

    Query.Open;
    while not Query.EOF do
    begin
      writeln(Query.FieldByName('Name').AsString + ': ' +
              Query.FieldByName('State').AsString);
      Query.Next;
    end;
  finally
    Query.Free;
    Connection.Free;
  end;
end;
How to enumerate services with NTSet?  Download

// this function lists Windows Services and their states.
// It may be used for local or remote host. When used for local host
// all parameters must be empty. 
// For remote computer the parameters must be: 
// TargetHost: name or IP address of the remote host, like '10.8.36.54' 
// UserName: name of the user, may include domain, like 'MOON\Administrator'; 
// Password: the user's password. 
procedure ListServices(TargetHost, UserName, Password: string);
var
  ServiceMan: TNTServiceManager;
  vList: TEnumList;
  i: integer;
begin
  ServiceMan := TNTServiceManager.Create(nil);
  vList := nil;
  try
    ServiceMan.LogonAs.UserName := UserName;
    ServiceMan.LogonAs.Password := Password;
    if TargetHost <> '' then ServiceMan.MachineName := '\\'+ TargetHost;

    if (ServiceMan.MachineName <> '') and (ServiceMan.ConnectIPC <> 0) then
      writeln('Warning: could not use the provided credentials.');

    vList := ServiceMan.GetServiceList(
                [STATE_ACTIVE, STATE_INACTIVE], [PROCESS]);
    for i := 0 to vList.Count - 1 do
    begin
      case vList[i].CurrentState of
        STOPPED:  writeln(vList[i].ServiceName + ': Stopped');
        START_PENDING:  writeln(vList[i].ServiceName + ': Start pending');
        STOP_PENDING:  writeln(vList[i].ServiceName + ': Stop pending');
        RUNNING:  writeln(vList[i].ServiceName + ': Running');
        CONTINUE_PENDING:  writeln(vList[i].ServiceName + ': Continue pending');
        PAUSE_PENDING:  writeln(vList[i].ServiceName + ': Pause pending');
        PAUSED:  writeln(vList[i].ServiceName + ': Paused');
      end;
    end;

  finally
    ServiceMan.Free;
    vList.Free;
  end;
end;