我想像“Windows任务管理器”那样,把所有的进程都列出来,并取得所有进程的路径。但是使用下面这段代码的时候,找到了N多进程,而且所有进程的路径不是dll的,便是"C:\....\Project2.exe" (本程序自身路径),不知道是怎么回事啊?
function EnumWindowsFunc(Handle:THandle;List:TStringList):boolean;stdcall;
  var
  Caption:array[0..256] of Char;
  ExecName:array[0..256] of Char;
  begin
  if   GetWindowText(Handle,Caption,SizeOf(Caption)-1) <> 0  then begin
  hwd:=Handle;
      pid:=GetClassLong(hwd, GCL_HMODULE);
      GetModuleFileName(pid, Execname, 200);
      Form1.Listbox1.Items.Add(Caption+'   -->    '+inttostr(Handle)+'  ===   '+ExecName);
  end;
  Result   :=True;
  end;EnumWindows(@EnumWindowsFunc,LParam(Listbox1.Count));

解决方案 »

  1.   


    你这样找到的不是进程,而是进程中的模块你想要的功能如下:
    procedure TForm1.ProcessList(var pList: TList);
    var
      p: ProcessInfo;
      ok: Bool;
      ProcessListHandle: THandle;
      ProcessStruct: TProcessEntry32;
    begin
      PList := TList.Create;
      PList.Clear;
      ProcessListHandle := CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0); //得到PPROCESS的快照...
      ProcessStruct.dwSize := Sizeof(ProcessStruct);
      ok := Process32First(ProcessListHandle, ProcessStruct);
      while Integer(ok) <> 0 do
        begin
          new(p);
          p.ExeFile := ProcessStruct.szExeFile;
          p.ProcessID := ProcessStruct.th32ProcessID;
          PList.Add(p);
          ok := Process32Next(ProcessListHandle, ProcessStruct);
        end;
      CloseHandle(ProcessListHandle);
    end;
    再用如下代码调用就可以了:
    var
      i: Integer;
      p: PRocessInfo;
    begin
      current := TList.Create;
      Current.Clear;
      ListboxRunFile.Clear;
      ProcessList(Current);
      for i := 0 to Current.Count - 1 do
        begin
          p := Current.Items[i];
          ListboxRunFile.Items.Add(p.ExeFile); //列举出所有的进程
          dispose(p);
        end;
    end;
    By Syant J. Wang  2007/11/19
      

  2.   

    楼上朋友的current不知道怎么声明,一直编译不过去....
    稍微改改,改成这样.但是查找出来的结果有NN多,而且N多条目都是重复....  function NewEnumWindowsFunc(Handle:THandle;List:TStringList):boolean;stdcall;
      var
      Caption:array[0..256] of Char;
      lppe: TProcessEntry32;
      Hand : THandle;
      found : boolean;
      begin
      if   GetWindowText(Handle,Caption,SizeOf(Caption)-1) <> 0  then begin
      hwd:=Handle;
      Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
      found := Process32First(Hand,lppe);
        while found do begin
          Form1.ListBox1.Items.Add(StrPas(lppe.szExeFile));
          found := Process32Next(Hand,lppe);
          end;
      end;
      Result   :=True;
      end;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
    EnumWindows(@NewEnumWindowsFunc,LParam(Listbox1.Count));
    end;
      

  3.   

    uses PsAPI;function EnumWindowsFunc(AHandle: THandle; AStrings: TStrings): BOOL; stdcall;
    var
      vCaption: array[0..MAX_PATH] of Char;
      vExecName: array[0..MAX_PATH] of Char;
      vProcessId: THandle;
      vProcess: THandle;
    begin
      if GetWindowText(AHandle, vCaption, SizeOf(vCaption)) <> 0 then
      begin
        GetWindowThreadProcessId(AHandle, vProcessId);
        vProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,
          False, vProcessId);
        try
          if GetModuleFileNameEx(vProcess, 0, vExecName, SizeOf(vExecName)) > 0 then
            AStrings.Add(Format('%s-->%d===%s', [vCaption, AHandle, vExecName]));
        finally
          CloseHandle(vProcess);
        end;
      end;
      Result := True;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      ListBox1.Items.BeginUpdate;
      try
        ListBox1.Items.Clear;
        EnumWindows(@EnumWindowsFunc, Integer(ListBox1.Items));
      finally
        ListBox1.Items.EndUpdate;
      end;
    end;
      

  4.   

    楼上朋友的代码没问题,顺利通过~但是有点不明白,查找结果中Explore.exe/Firefox.exe/QQ.exe这样的程序都出现了20+次……
    只运行了一个记事本的notepad.exe也出现了3次
    有没有什么办法可以减少重复的数量啊 ?
      

  5.   

    一个进程有多个窗体是很平常的事情加一个条件,将不可见的窗体排除
      if IsWindowVisible(AHandle) and // 必须可见
        (GetWindowText(AHandle, vCaption, SizeOf(vCaption)) <> 0) then
    另外,可以加个列表将处理过的进程排除,这就不提供实现的代码了
      

  6.   

    Windows 98/me下要用ToolHelp32,WinNT/2K下要用PsAPI,WinXP/2003下ToolHelp32和PsAPI都可。