http://www.delphibbs.com/delphibbs/dispq.asp?lid=513063
http://www.delphibbs.com/delphibbs/dispq.asp?lid=431776
http://www.delphibbs.com/delphibbs/dispq.asp?lid=672687
http://www.delphibbs.com/delphibbs/dispq.asp?lid=477645
看懂了上面的帖子就可以自己写了。
下面是我写的,在Win2K下关闭记事本程序。
uses
TLHelp32;
procedure KillProcess(dwProcessId: DWORD);
var
ProcHandle: THandle;
begin
ProcHandle := OpenProcess(1, FALSE, dwProcessID);
try
if ProcHandle <> 0 then
begin
if TerminateProcess(ProcHandle, $FFFFFFFF) then
WaitForSingleObject(ProcHandle, INFINITE);
end;
finally
CloseHandle(ProcHandle);
end;
end;
procedure ListProc;
var
i:Integer;
snap:THandle;
pe32:TPROCESSENTRY32;
procedure IfThenTerminate;
begin
if String(pe32.szExeFile)='notepad.exe' then
KillProcess(pe32.th32ProcessID);
end;
begin
snap := 0;
try
snap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if snap <> 0 then begin
pe32.dwSize := SizeOf(TPROCESSENTRY32);
if Process32First(snap, pe32) then begin
IfThenTerminate;
while Process32Next(snap, pe32) do
IfThenTerminate;
end;
end;
finally
CloseHandle(snap);
end;
End;
procedure TForm1.Button1Click(Sender: TObject);
begin
ListProc;
end;