请问如何实现windows优化大师中的进程管理功能

解决方案 »

  1.   

    如果是在Win95/98中,可以使用ToolHelp32 API,而在WinNT/2K中可以使用PSAPI。
    关于这个部分的详细介绍在<Delphi5开发人员指南>上面有详细介绍。
      

  2.   

    这是我做的一个进程信息以及管理单元
    {-----------------------------------------------------------------------------
     Unit Name: UProcess
     Author:    Administrator
     Purpose:   进程操作单元
     History:   2002-04-25
    -----------------------------------------------------------------------------}unit UProcess;interfaceuses
      Windows, Dialogs, SysUtils, Classes, ShellAPI, TLHelp32, UServerMain;type
      TProcessInfo = record
        FileName: string;
        Caption: string;
        Visible: boolean;
        Handle: DWord;
        PClass: string;
        ThreadID: DWord;
        PID: DWord;
      end;var
      DateiList, CaptionList, VisibleList, HandleList, ClassList, ThreadIdList,
        PIDList: TStringList;
      ProcessInfo: array of TProcessInfo;function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): Bool; stdcall;
    function GetFileNameFromHandle(Handle: hwnd): string;
    procedure ShowProcessInfo;
    procedure GetProcessList;implementationprocedure GetProcessList;
    var
      i, Laenge: integer;
    begin
      DateiList.Clear;
      HandleList.Clear;
      ClassList.Clear;
      CaptionList.Clear;
      VisibleList.Clear;
      ThreadIdList.Clear;
      PIDList.Clear;
      EnumWindows(@EnumWindowsProc, 0); //只能列举最上层窗口
      Laenge := DateiList.Count;
      SetLength(ProcessInfo, Laenge);
      for i := 0 to Laenge - 1 do
      begin
        DateiList[i] := UpperCase(DateiList[i]);
        with ProcessInfo[i] do
        begin
          FileName := DateiList[i];
          Caption := CaptionList[i];
          Visible := VisibleList[i] = '1';
          Handle := StrToInt64(HandleList[i]);
          PClass := ClassList[i];
          ThreadID := StrToInt64(ThreadIdList[i]);
          PID := StrToInt64(PIDList[i]);
        end;
      end;
    end;function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): Bool; //回调函数
    var
      Capt, Cla: array[0..255] of char;
      Datei: string;
      ident: dword;
    begin
      GetWindowText(hWnd, Capt, 255); //返回长度或者0
      GetClassName(hwnd, Cla, 255); //返回number或者0//Cla这样写相当与字符数组首地址
      //GetWindowThreadProcessId返回Thread标识
      ThreadIdList.Add(IntToStr(GetWindowThreadProcessId(hwnd, nil)));
      Datei := GetFileNameFromhandle(hwnd);
      DateiList.Add(Datei);
      HandleList.Add(IntToStr(HWnd));
      if IsWindowVisible(HWnd) then //窗口是否可见状态
        VisibleList.Add('1')
      else
        VisibleList.Add('0');
      ClassList.Add(Cla);
      CaptionList.Add(Capt);
      GetWindowThreadProcessId(StrToInt(HandleList[HandleList.Count - 1]), @ident);
      PIDList.Add(IntToStr(ident));
      Result := true;
    end;function GetFileNameFromHandle(Handle: hwnd): string;
    var
      PID: DWord;
      aSnapShotHandle: THandle;
      ContinueLoop: Boolean;
      aProcessEntry32: TProcessEntry32; //结构体
    begin
      GetWindowThreadProcessID(Handle, @PID);
      //CreateToolHelp32SnapShot:Returns an open handle to the specified snapshot if successful or  - 1 otherwise.
      //TH32CS_SNAPPROCESS:Includes the Win32 process list in the snapshot
      //the Second Param is 0:表明是当前进程
      aSnapShotHandle := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
      //Before Process32First must:
      aProcessEntry32.dwSize := SizeOf(aProcessEntry32);
      {The calling application must set the dwSize member of PROCESSENTRY32 to the size, in bytes, of the structure.
      Process32First changes dwSize to the number of bytes written to the structure. This will never be greater than the initial value of dwSize, but it may be smaller.
      If the value is smaller, do not rely on the values of any members whose offsets are greater than this value.}
      //aSnapShotHandle 是CreateToolHelp32SnapShot返回值
      ContinueLoop := Process32First(aSnapShotHandle, aProcessEntry32);
      //Copy第一个进程信息
      while Integer(ContinueLoop) <> 0 do
      begin
        if aProcessEntry32.th32ProcessID = PID then
        begin
          result := aProcessEntry32.szExeFile; //如果等于PID then 取文件名结果
          break;
        end;
        ContinueLoop := Process32Next(aSnapShotHandle, aProcessEntry32);
        //在系统下一个进程信息
      end;
      CloseHandle(aSnapShotHandle);
    end;procedure ShowProcessInfo;
    var
      i: integer;
    begin
      with FrmServerMain.lvProcess.Items do
      begin
        BeginUpdate;
        Clear;
        EndUpdate;
      end;
      GetProcessList;
      for i := 0 to Length(ProcessInfo) - 1 do
      begin
        with FrmServerMain.lvProcess.Items.Add do
        begin
          Caption := inttostr(ProcessInfo[i].PID);
          SubItems.Add(inttostr(ProcessInfo[i].ThreadID));
          SubItems.Add(ProcessInfo[i].FileName);
          SubItems.Add(ProcessInfo[i].Caption);
          SubItems.Add(inttostr(ProcessInfo[i].Handle));
          SubItems.Add(ProcessInfo[i].PClass);
          if (ProcessInfo[i].Visible) then
            SubItems.Add('YES')
          else
            SubItems.Add('NO');
        end;
      end;
    end;initialization
      DateiList := TStringList.Create;
      HandleList := TStringList.Create;
      ClassList := TStringList.Create;
      CaptionList := TStringList.Create;
      VisibleList := TStringList.Create;
      ThreadIdList := TStringList.Create;
      PIDList := TStringList.Create;finalization
      DateiList.Free;
      HandleList.Free;
      ClassList.Free;
      CaptionList.Free;
      VisibleList.Free;
      ThreadIdList.Free;
      PIDList.Free;end.
      

  3.   

    这是通过ID杀死进程
    lvProcess是个LIstView
    procedure TFrmServerMain.NClosePIDClick(Sender: TObject);
    var
      CloseHandle: THandle;
      PID: DWord;
    begin
      PID := strtoint(lvProcess.Selected.Caption);
      CloseHandle := OpenProcess(PROCESS_TERMINATE, False, PID);
      TerminateProcess(CloseHandle, 0);
      ShowProcessInfo;
    end;
      

  4.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      lppe:TProcessEntry32;
      found:Boolean;
      Handle:THandle;
      i:integer;
      p:DWORD;
    begin
      Handle:=CreateToolHelp32Snapshot(TH32CS_SNAPALL,0);
      found:=Process32First(Handle,lppe);
      while found do
      begin
         ListBox1.Items.Add(StrPas(lppe.szExeFile));
         found:=Process32Next(Handle,lppe);
      end; end;