进程问题(100分)

T

toe

Unregistered / Unconfirmed
GUEST, unregistred user!
怎么样列出当前的windows的进程?怎么样结束一个进程?
当该进程是系统进程时,是不允许结束的,怎么来区分?
对不同windows版本的进程是不是一样?
 
procedure TSerm.GetTaskList(St: TStrings);
var SnapShot: THandle;
a: TProcessEntry32;
begin
st.Clear;
SnapShot:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
a.dwSize:=Sizeof(TProcessEntry32);
Process32First(SnapShot,a);
repeat
st.Add(IntToHex(a.th32ProcessID,8)+'='+a.szExeFile);
until not Process32Next(SnapShot,a);
CloseHandle(SnapShot);
end;
 
需要包含头文件
TlHelp32
 
(注意uses TLHelp32)
然后
var lppe: TProcessEntry32;
found : boolean;
Hand : THandle;
begin
Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
found := Process32First(Hand,lppe);
while found do
begin
ListBox.Items.Add(StrPas(lppe.szExeFile));//列出所有进程。
found := Process32Next(Hand,lppe);
end;
end;

/////////////////////////////////////////////////////
uses ... TLHelp32, ...

type
TForm1 = class(TForm)
...
end;

var
Form1: TForm1;
l : Tlist; ////返回的东东在"L"这个TList中。

type
TProcessInfo = Record
ExeFile : String;
ProcessID : DWORD;
end;
pProcessInfo = ^TProcessInfo;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var p : pProcessInfo;
i : integer;
ContinueLoop:BOOL;
var
FSnapshotHandle:THandle;
FProcessEntry32:TProcessEntry32;
begin
l := TList.Create;
l.Clear;
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);
while integer(ContinueLoop)<>0 do
begin
New(p);
p.ExeFile := FProcessEntry32.szExeFile;
p.ProcessID := FProcessEntry32.th32ProcessID;
l.Add(p);
ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var p : pProcessInfo;
i : integer;
begin
With l do
for i := Count - 1 DownTo 0 do
begin p := items; Dispose(p); Delete(i); end;
end;

...
end.
///////////////////////////////////
procedure TForm1.Button1Click(Sender: TObject);
{Places the modulenames of the running/minimized tasks into a listbox }
var
pTask : pTaskEntry; {requires Uses ToolHelp}
Task : bool;
Pstr : array [0..79] of Char;
Str : string[80];
byt_j : byte;
begin
ListBox1.Clear;
GetMem(pTask, SizeOf(TTaskEntry)); {Reserve memory for TaskEntry}
pTask^.dwSize:=SizeOf(TTaskEntry);

byt_j:=0; {Set up a counter for number of tasks}
Task:=TaskFirst(pTask); {Find first task}
While task do
begin
inc(byt_j); {count number of different tasks}
Str:=StrPas(pTask^.szModule); {Convert PStr into Pascal string}
Listbox1.Items.Add(str); {Store Pascal string into listbox}
task:=taskNext(pTask); {Check for next possible task}
end;
Label1.Caption:=IntToStr(byt_j)+ ' tasks found'; {Show counter}
end;

 
顶部