如何远程获取正在运行的进程列表?

时间:2011-10-30 18:54:36

标签: delphi process

如何使用Delphi获取某些其他计算机(通过网络)上运行进程的列表?

2 个答案:

答案 0 :(得分:11)

您可以使用Win32_Process WMI类。

检查此样本

{$APPTYPE CONSOLE}
uses
  SysUtils,
  ActiveX,
  ComObj,
  Variants;


procedure  GetWin32_ProcessInfo;
const
  WbemUser            ='';//set the user name to log in
  WbemPassword        ='';//set the password
  WbemComputer        ='localhost';//set the name of the remote machine or IP address
  wbemFlagForwardOnly = $00000020;
var
  FSWbemLocator : OLEVariant;
  FWMIService   : OLEVariant;
  FWbemObjectSet: OLEVariant;
  FWbemObject   : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;
begin;
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService   := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword);
  FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_Process','WQL',wbemFlagForwardOnly);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  while oEnum.Next(1, FWbemObject, iValue) = 0 do
  begin
    Writeln(Format('Name         %s',[String(FWbemObject.Name)]));// String
    Writeln(Format('ProcessId    %d',[Integer(FWbemObject.ProcessId)]));// Uint32
    Writeln;
    FWbemObject:=Unassigned;
  end;
end;


begin
 try
    CoInitialize(nil);
    try
      GetWin32_ProcessInfo;
    finally
      CoUninitialize;
    end;
 except
    on E:EOleException do
        Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
 Writeln('Press Enter to exit');
 Readln;
end.

要在远程计算机中使用WMI,您必须设置防火墙和DCOM设置,查看这些文章以获取更多信息。

答案 1 :(得分:0)

请参阅TlHelp32单元中的EnumProcesses过程。