关闭进程的代码,注意有些系统进程是不能的。 //转代码uses tlhelp32;procedure TForm1.Button2Click(Sender: TObject); var hSnapshot: Integer; bFound: Boolean; ProcessEntry: ProcessEntry32; begin hSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if hSnapshot = 0 then Exit; bFound := Process32First(hSnapshot, ProcessEntry); while bFound do begin if LowerCase(ExtractFileExt(ProcessEntry.szExeFile)) = '.exe' then with ListView1.Items.Add do begin Caption := ProcessEntry.szExeFile; SubItems.Add(IntToStr(ProcessEntry.th32ProcessID)); end; bFound := Process32Next(hSnapshot, ProcessEntry); end; CloseHandle(hSnapshot); end;procedure TForm1.Button3Click(Sender: TObject); var iProcessId: Integer; hProcess: Integer; begin if ListView1.Selected <> nil then begin iProcessId := StrToInt(ListView1.Selected.SubItems[0]); hProcess := OpenProcess(PROCESS_ALL_ACCESS, false, iProcessId); if hProcess <> 0 then TerminateProcess(hProcess, 0); end; end;
//转代码uses tlhelp32;procedure TForm1.Button2Click(Sender: TObject);
var
hSnapshot: Integer;
bFound: Boolean;
ProcessEntry: ProcessEntry32;
begin
hSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if hSnapshot = 0 then Exit;
bFound := Process32First(hSnapshot, ProcessEntry);
while bFound do
begin
if LowerCase(ExtractFileExt(ProcessEntry.szExeFile)) = '.exe' then
with ListView1.Items.Add do
begin
Caption := ProcessEntry.szExeFile;
SubItems.Add(IntToStr(ProcessEntry.th32ProcessID));
end;
bFound := Process32Next(hSnapshot, ProcessEntry);
end;
CloseHandle(hSnapshot);
end;procedure TForm1.Button3Click(Sender: TObject);
var
iProcessId: Integer;
hProcess: Integer;
begin
if ListView1.Selected <> nil then
begin
iProcessId := StrToInt(ListView1.Selected.SubItems[0]);
hProcess := OpenProcess(PROCESS_ALL_ACCESS, false, iProcessId);
if hProcess <> 0 then
TerminateProcess(hProcess, 0);
end;
end;
需要用Application.Terminate才能将 主程序关掉