就像任务管理器那样,只需要知道进程名立即可终止。
解决方案 »
- 请教:枚举类型与数字能否互相转换?
- 请GDTOPONE 和SmallHand过来接分
- 一个小问题,关于拖放:如何没有选中某文本(比如只选中标题),将文本拖到别的应用程序中?
- 英雄!关于cxgrid获得鼠标焦点信息的问题!!!
- 如何实现断点续传功能
- 简单问题!我真不知道
- 紧急:通过HTTP得到图片的Stream后,如何获取它的宽、高、颜色数
- 关于DBGIRD控件,100求助!在线给分。
- 关于table.post很奇怪的错误???近者有分!!
- 如何实现像金山词霸那样朗诵中文,微软的TTS怎样才可以发出中文?急用!
- 如何将两个表关联后select的结果放到一个临时的表中?
- 揭露广州粤建三和公司的黑暗
BOOL TerminateProcess(
HANDLE hProcess,
UINT uExitCode
);Parameters
hProcess
[in] Handle to the process to terminate.
The handle must have the PROCESS_TERMINATE access right. For more information, see Process Security and Access Rights.uExitCode
[in] Exit code to be used by the process and threads terminated as a result of this call. Use the GetExitCodeProcess function to retrieve a process's exit value. Use the GetExitCodeThread function to retrieve a thread's exit value.
Return Values
If the function succeeds, the return value is nonzero.If the function fails, the return value is zero. To get extended error information, call GetLastError.
unit Unit1;interfaceuses
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.
的方法没有把进程关掉,反而把EXPLORER关掉了,只得重启:~~(
其中第一个过程中的Sendtaskname是进程名,他的值可以作为第二个过程的参数procedure tform1.getclienttask(); //取得客户端的所有进程
const
PROCESS_TERMINATE=$0001;
//进程的PROCESS_TERMINATE访问权限
var
SendCode:array[0..250] of char;
SendCodeStr:string;
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
FSnapshotHandle := CreateToolhelp32Snapshot
(TH32CS_SNAPPROCESS, 0);
//获取系统所有进程快照
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
//调用Process32First前用Sizeof(FProcessEntry32)填充FProcessEntry32.dwSize
ContinueLoop := Process32First(FSnapshotHandle,
FProcessEntry32);
//获取快照中第一个进程信息并保存到FProcessEntry32结构体中
while integer(ContinueLoop) <> 0 do
begin
Sendtaskname:=ExtractFileName(FProcessEntry32.szExeFile);
begin
sendCodeStr:='task'+sendtaskname;
StrpCopy(sendCode,sendCodeStr);
cUDP.RemoteHost:='192.168.0.4';
cUDP.SendBuffer(sendCode,250);
end;
ContinueLoop := Process32Next(FSnapshotHandle,FProcessEntry32);
end;
end;function tform1.KillTask(ExeFileName: string): integer;
const
PROCESS_TERMINATE=$0001;
//进程的PROCESS_TERMINATE访问权限
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
result:= 0;
FSnapshotHandle := CreateToolhelp32Snapshot
(TH32CS_SNAPPROCESS, 0);
//获取系统所有进程快照
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
//调用Process32First前用Sizeof(FProcessEntry32)填充FProcessEntry32.dwSize
ContinueLoop := Process32First(FSnapshotHandle,
FProcessEntry32);
//获取快照中第一个进程信息并保存到FProcessEntry32结构体中
while integer(ContinueLoop) <> 0 do
//循环枚举快照中所有进程信息
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile))
=UpperCase(ExeFileName))
or (UpperCase(FProcessEntry32.szExeFile)
=UpperCase(ExeFileName))) then
//找到要中止的进程名
Result := Integer(TerminateProcess(OpenProcess(
PROCESS_TERMINATE, BOOL(0),
FProcessEntry32.th32ProcessID), 0));
//中止进程
ContinueLoop := Process32Next(FSnapshotHandle,FProcessEntry32);
//查找下一个符合条件进程
end;
end;
ExtractFileName出来的是EXE的文件名,而我需要的是进程名。例如,一个aa.exe的程序在任务管理器里显示的可能bb,而它的主窗体名却是CC。
uses
ShareMem,SysUtils,TLHelp32,windows,
Classes;{$R *.res}function zhaodaojincheng(shuru : string):longword; stdcall;var
jubing : longword;//进程快照句柄
fprocessentry32 : TProcessEntry32;//结构类型的变量
zhenjia : Boolean; //返回一个布尔值
processid : longword;//储存进程ID
mingcheng : String;//储存进程名称
pid : longword;//用来储存进程id
jinchengjubing : longword; //用来储存进程句柄
begin
jubing := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //获得进程快照句柄
fprocessentry32.dwSize := sizeof(fprocessentry32); //给TProcessEntry32结构的第一个参数赋值(也可以理解为把这个结构的第一个参数初始化)
zhenjia := Process32First(jubing,fprocessentry32);//取得第一个进程信息
while zhenjia = true do
begin
zhenjia := Process32Next(jubing,FprocessEntry32);//取得第下一个进程信息
mingcheng := fprocessentry32.szExeFile;//取得一个进程的名称 if mingcheng = shuru then//如果进程名等于这个字符串
pid := fprocessentry32.th32ProcessID;//找到想要的进程的pid
jinchengjubing := OpenProcess(PROCESS_ALL_ACCESS, FALSE, pID); //找到进程句柄并打开
terminateprocess(jinchengjubing,0); //关闭进程句柄
result := pid; //该函数返回值 end;
closehandle(jubing); //释放进程快照的句柄归还系统资源
end; exports
zhaodaojincheng;
exe部分:implementationfunction zhaodaojincheng(shuru : string):longword;stdcall;external'jinchengdll.dll';procedure TForm1.Timer1Timer(Sender: TObject);
var
shuru : string;//给自定义函数输入参数用的变量
fanhuizhi : longword;begin shuru := edit1.text; //把进程名存进变量shuru里作为dll里的参数使用
fanhuizhi := zhaodaojincheng(shuru);//调用dll里的函数end;
procedure TForm1.Button1Click(Sender: TObject);begin
timer1.Enabled := true;end;