procedure TForm1.ProcessList(var pList: TList); var p: ProcessInfo; ok: Bool; ProcessListHandle: THandle; ProcessStruct: TProcessEntry32; begin PList := TList.Create; PList.Clear; ProcessListHandle := CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0); ProcessStruct.dwSize := Sizeof(ProcessStruct); ok := Process32First(ProcessListHandle, ProcessStruct); while Integer(ok) < > 0 do begin new(p); p.ExeFile := ProcessStruct.szExeFile; p.ProcessID := ProcessStruct.th32ProcessID; PList.Add(p); ok := Process32Next(ProcessListHandle, ProcessStruct); end; end;
procedure TForm1.Button1Click(Sender: TObject); var h: THandle; a: DWORD; p: PRocessInfo; begin if ListBox1.ItemIndex >= 0 then begin p := Current.Items[ListBox1.ItemIndex]; h := openProcess(Process_All_Access, true, p.ProcessID); GetExitCodeProcess(h, a);
if Integer(TerminateProcess(h, a)) < > 0 then begin My_RunFileScan(ListBox1); end; end else Application.MessageBox('请先选择一个进程!', '黑洞', MB_ICONERROR + MB_OK); end;
procedure TForm1.My_RunFileScan(ListboxRunFile: TListBox); var i: Integer; p: PRocessInfo; begin current := TList.Create; Current.Clear; ListboxRunFile.Clear; ProcessList(Current); for i := 0 to Current.Count - 1 do begin new(p); p := Current.Items[i]; ListboxRunFile.Items.Add(p.ExeFile); end; end;
procedure TForm1.Button2Click(Sender: TObject); begin My_RunFileScan(ListBox1); end;
或者直接给窗口句柄发送一个WM_CLOSE消息。SendMessage(FindWindow(NULL,"记事本"),WM_CLOSE,0,0);
PERFORM
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, TLHelp32, Controls, Forms, Dialogs,
StdCtrls;
type
TProcessInfo = record
ExeFile: string;
ProcessId: DWORD;
end;
ProcessInfo = ^TProcessInfo;
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure ProcessList(var pList: TList);
procedure My_RunFileScan(ListboxRunFile: TListBox);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
Current: TList;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.ProcessList(var pList: TList);
var
p: ProcessInfo;
ok: Bool;
ProcessListHandle: THandle;
ProcessStruct: TProcessEntry32;
begin
PList := TList.Create;
PList.Clear;
ProcessListHandle := CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0);
ProcessStruct.dwSize := Sizeof(ProcessStruct);
ok := Process32First(ProcessListHandle, ProcessStruct);
while Integer(ok) < > 0 do
begin
new(p);
p.ExeFile := ProcessStruct.szExeFile;
p.ProcessID := ProcessStruct.th32ProcessID;
PList.Add(p);
ok := Process32Next(ProcessListHandle, ProcessStruct);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
h: THandle;
a: DWORD;
p: PRocessInfo;
begin
if ListBox1.ItemIndex >= 0 then
begin
p := Current.Items[ListBox1.ItemIndex];
h := openProcess(Process_All_Access, true, p.ProcessID);
GetExitCodeProcess(h, a);
if Integer(TerminateProcess(h, a)) < > 0 then
begin
My_RunFileScan(ListBox1);
end;
end
else
Application.MessageBox('请先选择一个进程!', '黑洞', MB_ICONERROR + MB_OK);
end;
procedure TForm1.My_RunFileScan(ListboxRunFile: TListBox);
var
i: Integer;
p: PRocessInfo;
begin
current := TList.Create;
Current.Clear;
ListboxRunFile.Clear;
ProcessList(Current);
for i := 0 to Current.Count - 1 do
begin
new(p);
p := Current.Items[i];
ListboxRunFile.Items.Add(p.ExeFile);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
My_RunFileScan(ListBox1);
end;
end.
这类的api太多了。
可
调用winexexc();参数省略,参见delphi的帮助吧,要注意第一个参数不是pchar,要用另一个api转一下