网上找的,先看看,主要是EnumProcesses<br><br>1.列举进程<br>procedure TForm1.rzbtnListClick(Sender: TObject);<br>var<br> I : Integer;<br> <br> LAProcess, LAModule: array[0..1023] of Cardinal;<br> hProcess : THandle;<br> LLi : TListItem;<br> cNeeded, cProcess : DWORD;<br>begin<br> rzlvProcess.Clear;<br> if not EnumProcesses(@LAProcess, SizeOf(LAProcess), cNeeded) then<br> exit; //error<br> rzlvProcess.Items.BeginUpdate;<br> try<br> for I := 0 to cNeeded div SizeOf(DWORD) - 1 do // Iterate<br> begin<br> if LAProcess = 0 then Continue;<br> hProcess := OpenProcess({PROCESS_ALL_ACCESS, //}<br> PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,<br> False, LAProcess);<br> if (hProcess <> 0) and (<br> EnumProcessModules(hProcess, @LAModule, SizeOf(LAModule),<br> cProcess)) then<br> begin<br> LLi := rzlvProcess.Items.Add;<br> LLi.Caption := IntToHex(LAProcess, 4);//记下PID <br> LLi.SubItems.Add(MyGetModuleBaseName(hProcess, LAModule[0]));//进程可执行程序名字<br> LLi.SubItems.Add(MyGetModulePath(hProcess, LAModule[0]));//位置<br> CloseHandle(hProcess);<br> end;<br> end; // for<br> finally<br> rzlvProcess.Items.EndUpdate;<br> end;<br>end;<br>2. 杀死进程,根据PID<br>procedure TForm1.rzbtnEndTaskClick(Sender: TObject);<br>var<br> hProcess : THandle;<br> Msg : string;<br>begin<br> with rzlvProcess do<br> begin<br> if Selected <> nil then<br> begin<br> Msg := 'PID: ' + Selected.Caption + ' Name: ' + Selected.SubItems[0]<br> + #13'End Task ?';<br> if IDYES = MessageBox(Application.Handle, Pchar(Msg), 'Query',<br> MB_YESNO or MB_ICONINFORMATION or MB_APPLMODAL or MB_DEFBUTTON2)<br> then<br> begin<br> hProcess := OpenProcess(PROCESS_TERMINATE,<br> False, StrToInt('$' + rzlvProcess.Selected.Caption));<br> //rzlvProcess.Selected.Caption 上一个函数记下的进程 PID <br> if (hProcess <> 0) then<br> begin<br> TerminateProcess(hProcess, 0);<br> CloseHandle(hProcess);<br> end;<br> end;<br> end;<br> end;<br>end;<br><br>function TForm1.MyGetModuleBaseName(hProcess: THandle;<br> hModule: HMODULE): string;<br>begin<br> SetLength(Result, MAX_PATH);<br> SetLength(Result,<br> GetModuleBaseName(hProcess, hModule, @Result[1], MAX_PATH));<br>end;<br><br>function TForm1.MyGetModulePath(hProcess: THandle;<br> hModule: HMODULE): string;<br>begin<br> SetLength(Result, MAX_PATH);<br> SetLength(Result,<br> GetModuleFileNameEx(hProcess, hModule, @Result[1], MAX_PATH));<br> Result := ExtractFilePath(Result);<br>end;