How to enumerate processes on a local or remote host?
This page contains the two Delphi examples of how to enumerate running processes. They both produce the same result. The first example is based on TWmiQuery and TWmiConnection components from WmiSet component collection. TWmiQuery is used to retrieve the properties of Win32_Process class. Look at MSDN web site for more information about Win32_Process.

The second example utilizes TWmiProcessControl component from WmiSet component collection. This component is specialized for process management, while TWmiQuery is a universal component that may be used for many purposes. The source code below creates and destroys the components on the fly, so these functions may be used in UI and console applications.

How to enumerate services with TWmiQuery?  Download

// this function lists running processes. 
// It may be used for local or remote host. When used for local host 
// TargetHost, UserName, Password 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. 
// AList: receives the resulting list of processes. 
procedure ListProcesses(TargetHost, UserName, Password: string; AList: TStrings);
var
  Connection: TWmiConnection;
  Query: TWmiQuery;
begin
  Query := TWmiQuery.Create(nil);
  Connection := TWmiConnection.Create(nil);
  try
    Query.Connection := Connection;
    Query.WQL.Text := 'select name from Win32_Process';
    Connection.MachineName := TargetHost;
    Connection.Credentials.UserName := UserName;
    Connection.Credentials.Password := Password;

     // exception may happen on Win9x, WinNT if WMI core is not installed; 
     // The provided credentials may also be invalid. 
    Connection.Connected := true;

    Query.Open;
    while not Query.EOF do
    begin
      AList.Add(Query.FieldByName('Name').AsString);
      Query.Next;
    end;
  finally
    Query.Free;
    Connection.Free;
  end;
end;
How to enumerate services with TWmiProcessControl?  Download

// this function lists running processes. 
// It may be used for local or remote host. When used for local host 
// TargetHost, UserName, Password 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. 
// AList: receives the resulting list of processes. 
procedure ListProcesses(TargetHost, UserName, Password: string; AList: TStrings);
var
  ProcessControl: TWmiProcessControl;
  i: integer;
begin
  ProcessControl := TWmiProcessControl.Create(nil);
  try
    ProcessControl.MachineName := TargetHost;
    ProcessControl.Credentials.UserName := UserName;
    ProcessControl.Credentials.Password := Password;

     // exception may happen on Win9x, WinNT if WMI core is not installed; 
     // The provided credentials may also be invalid. 
    ProcessControl.Active := true;

    for i := 0 to ProcessControl.Processes.Count - 1 do
      AList.Add(ProcessControl.Processes[i].Name);
  finally
    ProcessControl.Free;
  end;
end;