M
mazheng
Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls, ComCtrls, ExtCtrls, Buttons,<br> //----------------------<br> WinSvc,TlHelp32, Menus, PsApi;<br><br>type<br> TForm1 = class(TForm)<br> ListBox1: TListBox;<br> procedure FormCreate(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br> gbCanClose: Boolean;<br> SvcHandle: SC_HANDLE;<br> porFile: String;<br> ESS: Array of TEnumServiceStatus;<br> nBytesNeeded,nServices,nResumeHandle: Cardinal;<br> i: Integer;<br> pList1,pList2,pList3 : TStringList;<br><br><br>implementation<br><br>{$R *.DFM}<br><br> function GetProcessFullFileName(pID:Integer):String;<br> var s:String;<br> hProcess:THandle;<br> begin<br> Result:='';<br> SetLength(s,256);<br> hProcess:=OpenProcess(PROCESS_ALL_ACCESS or PROCESS_QUERY_INFORMATION ,FALSE,pID);<br> if(hProcess>0) then<br> begin<br> if(GetModuleFileNameEx(hProcess,0,PChar(s),255)>0) then<br> begin<br> SetLength(s, StrLen(PCHAR(s)));<br> Result:=s;<br> end;<br> <br> end;<br> CloseHandle(hProcess);<br> end;<br><br>procedure EnumProcess(List: TListBox);<br>//枚举进程<br>var<br> ContinueLoop : BOOL;<br> FSnapshotHandle : THandle;<br> FProcessEntry32 : TProcessEntry32;<br>begin<br> FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);<br> try<br> List.Clear;<br> FProcessEntry32.dwSize := Sizeof(FProcessEntry32);<br> ContinueLoop := Process32First(FSnapshotHandle,FProcessEntry32);<br><br> while integer(ContinueLoop)<>0 do<br> begin<br> //进程名称<br> List.Items.Add(FProcessEntry32.szExeFile);<br> List.Items.Add('-');<br> //包含全路经的进程名称<br> // 但不知道为何返回空? 请指教<br> List.Items.add(GetProcessFullFileName(FProcessEntry32.th32ProcessID) );<br><br> ContinueLoop := Process32Next(FSnapshotHandle,FProcessEntry32);<br> end;<br> finally<br> CloseHandle(FSnapshotHandle);<br> end;<br>end;<br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> EnumProcess(ListBox1);<br>end;<br><br>end.