求,终止当前某一个程序的运行有方法(50分)

  • 主题发起人 主题发起人 knifepj
  • 开始时间 开始时间
K

knifepj

Unregistered / Unconfirmed
GUEST, unregistred user!
求,终止当前某一个程序的运行有方法
最好付上一段代码。
 
来自:jsxjd, 时间:2002-11-22 12:19:00, ID:1450931
----------------------------------------------------
获得进程列表,并终止 Excel 进程
procedure TForm1.Button1Click(Sender: TObject);
var
FSnapshotHandle:THandle;
FProcessEntry32:TProcessEntry32;
Ret : BOOL;
ProcessID : integer;
s:string;
begin
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
Ret:=Process32First(FSnapshotHandle,FProcessEntry32);
Memo1.clear;
while Ret do
begin
Memo1.lines.add(FProcessEntry32.szExeFile);
s:=ExtractFileName(FProcessEntry32.szExeFile);
if s='EXCEL.EXE' then
begin
ProcessID:=FProcessEntry32.th32ProcessID;
TerminateProcess(OpenProcess(PROCESS_TERMINATE,false,ProcessID),1);
s:='';
end;
Ret:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
end;
 
function TerminateByCaption(FormCaption: string): Boolean;
var
hClose, hForm: THandle;
ProcessId: DWORD;
begin
hForm := FindWindow(nil, PChar(FormCaption));
if hForm > 0 then
begin
GetWindowThreadProcessId(hForm, ProcessId);
CloseHandle(hForm);
hClose := OpenProcess(PROCESS_TERMINATE, False, ProcessId);
TerminateProcess(hClose, 0);
CloseHandle(hClose);
Result := True;
end
else
Result := False;
end;

使用:TerminateByCaption('笔记本');
 
下面给出一段在 Delphi 中关闭“计算器”程序为例:
...
var
HWndCalculator : HWnd;
begin
// find the exist calculator window
HWndCalculator := Winprocs.FindWindow(nil, '计算器');

// close the exist Calculator }
if HWndCalculator <> 0 then
SendMessage(HWndCalculator, WM_CLOSE, 0, 0);
end;
 
用TerminateProcess函数,最直接,彻底!
 
//==============================================================================
//强制终止某应用程序运行********************************************************
//==============================================================================
procedure AppForceExit(const AppName: string);
var lppe: TProcessEntry32;
ssHandle: THandle;
AppFound: Boolean;
Wnd: HWND;
begin
ssHandle := CreateToolHelp32SnapShot(TH32CS_SNAPALL, 0);
AppFound := Process32First(sshandle, lppe);
while AppFound do
begin
//其中lppe.szExefile就是程序名**********************************************
if UpperCase(ExtractFileName(lppe.szExeFile))=UpperCase(AppName) then
begin
Wnd := OpenProcess(PROCESS_ALL_ACCESS, true, lppe.th32ProcessID);
TerminateProcess(Wnd, 0);
end;
AppFound := Process32Next(ssHandle, lppe);
end;
end;
 
//==============================================================================
//在进程中查找某应用程序是否正在运行********************************************
//==============================================================================
function ProcessFound(const AppName: string): Boolean;
var lppe: TProcessEntry32;
ssHandle: THandle;
AppFound: Boolean;
begin
Result := false;
ssHandle := CreateToolHelp32SnapShot(TH32CS_SNAPALL, 0);
AppFound := Process32First(sshandle, lppe);
while AppFound do
begin
//其中lppe.szExefile就是程序名**********************************************
if UpperCase(ExtractFileName(lppe.szExeFile))=UpperCase(AppName) then Result := true;
AppFound := Process32Next(ssHandle, lppe);
end;
end;
 
同意QUAEK
 
我也同意QUAEK
 
下面是一段源代码,只要你把你想要终止的程序名(包括路径)写入
D:/Program Files/text1.txt这个文件中(注意每行写一个程序名).那么这个
程序无法运行下去.程序里有详细的注释.祝你好运!
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,tlhelp32,stdCtrls, ExtCtrls;//注意加上tlhelp32这个单元;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Timer1: TTimer;
ListBox2: TListBox;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
procedure processlist(var prolist:tlist);//自定义函数列举所有的进程;
procedure exitcode ;//自定义函数终止进程;
{ Private declarations }
public
{ Public declarations }
end;
type tproinfo=record
filename:string;
proid:dword;
end;proinfo=^tproinfo;//自定义记录用来记录进程的文件名和ID;

var
Form1: TForm1;
curr:tlist;
temp,b,a1:integer;
implementation

{$R *.DFM}


procedure TForm1.processlist(var prolist: tlist);
var p:proinfo;
ok:bool;
prolisthandle:thandle;
prostruct:tprocessentry32; //记录进程的数据结构;
begin
prolist:=tlist.Create ;
prolist.Clear ;
prolisthandle:=createtoolhelp32snapshot(th32cs_snapprocess,0);
prostruct.dwSize :=sizeof(prostruct);
ok:=process32first(prolisthandle,prostruct);//发现第一个进程;
while integer(ok)<>0 do
begin
new(p);
p.filename :=prostruct.szExeFile ;
p.proid :=prostruct.th32ProcessID ;
prolist.Add (p);
ok:=process32next(prolisthandle,prostruct);//发现下一个进程;
end;
end;


procedure TForm1.FormCreate(Sender: TObject);
var
a:string;
f:textfile;
begin
listbox1.Clear;
listbox2.Clear;
if fileexists('D:/Program Files/text1.txt') then
begin //该文件记录你所想禁止运行的程序的路径;
assignfile(f,'D:/Program Files/text1.txt');
reset(f);
while not eof(f) do
begin
readln(f,a);
a:=uppercase(a); //转化成大写字母;
listbox2.Items.Add (a); //记录所有被禁止运行程序的路径;
end;
closefile(f)
end
else application.Terminate ;

end;


procedure Tform1.exitcode;
var h:thandle;
a:dword;
p:proinfo;

begin
begin
p:=curr.items; //指向禁止运行的进程的数据结构;
h:=openprocess(process_all_access,true,p.proid);
getexitcodeprocess(h,a); //得到进程退出代码;
terminateprocess(h,a) ; //终止进程
end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var i:integer;
p:proinfo;
begin
listbox1.Clear ;
processlist(curr); //调用进程列举的函数;
for i:=0 to curr.Count-1 do
begin
new(p);
p:=curr.Items;
listbox1.Items.Add(p.filename); //记录所有的进程的路径;
end; //listbox2是记录所有禁止运行的程序的路径;
for i:=0 to listbox2.Items.Count-1 do
if (listbox1.Items.IndexOf(listbox2.Items.Strings)>=0) then
begin
b:=listbox1.Items.IndexOf(listbox2.Items.Strings);
exitcode;//调用终止进程的函数;
end;
end;
end.
 
多人接受答案了。
 
后退
顶部