救救我,一个终止进程(应用程序)的问题。(300分)

  • 主题发起人 主题发起人 PowerDelphi
  • 开始时间 开始时间
P

PowerDelphi

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大虾一定要救救我:
我有一段程序不知错在什么地方,我在implementation部分声明了如下类型:
type
;TProcessInfo=Record
; ; ExeFile:string;
; ; ProcessID:DWORD;
;end;
然后有以下过程:
procedure TForm1.Timer1Timer(Sender: TObject);
var
;p:PProcessInfo;
;i:integer;
;ContinueLoop:Boolean;
;FSnapshotHandle:THandle;
;FProcessEntry32:TProcessEntry32;
;hh:HWND;
begin
;i:=0;
;FSnapshotHandle:=CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS,0);
;FProcessEntry32.dwSize :=SizeOf(FProcessEntry32);
;ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);
;while integer(ContinueLoop)<>0 do
;begin
;new(p);
;p.ExeFile :=FProcessEntry32.szExeFile ;
;while i<=checklistbox1.Items.Count-1 do
;begin
;if lowercase(extractfilename(p.ExeFile))=checklistbox1.Items then
;begin
;p.ProcessID :=FProcessEntry32.th32ProcessID ;
;hh:=OpenProcess(PROCESS_ALL_ACCESS,true,p.ProcessID);
;TerminateProcess(hh,0);
;application.MessageBox ('此程序被禁止在本机运行,请与管理员联系!','禁止运行',
; ;MB_ICONINFORMATION+MB_OK);
;end;
;inc(i);
;end;
;ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
;end;
;end;
1、终止一个应用程序是终止它的进程ID,还是父进程ID,有的不同的应用程序父进程ID相同,为什么?
(菜鸟级问题,请多多指教!!!)
2、好像用TerminateProcess()终止一个应用程序对系统有害,不知还有没有更好办法(根据文件名来终止)?若可能请附代码,感激不尽!!!
3、这是一个根据文件名来关闭应用程序的程序,但是根本不能达到目的,不知道是什么原因,好像陷入了死循环,搞了一个星期了也不知怎么处理,请各位大侠一定帮帮我,对我来说简直是救命!!!多谢,多谢
!!!
 
用CloseHandle或ExitProcess关闭进程教好!
 
改为下面这种方式:
var
; P: PProcessInfo;
; I: Integer;
; ContinueLoop: Boolean;
; FSnapshotHandle: THandle;
; FProcessEntry32: TProcessEntry32;
; hh: HWND;
begin
; I := 0;
; FSnapshotHandle := CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0);
; FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
; ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
; while Integer(ContinueLoop) <> 0 do
; begin
; ; New(P);
; ; P.ExeFile := FProcessEntry32.szExeFile;
; ; I := checklistbox1.Items.IndexOf(LowerCase(ExtractFileName(P.ExeFile)));
; ; if I >= 0 then
; ; begin
; ; ; P.ProcessId := FProcessEntry32.th32ProcessID;
; ; ; hh := OpenProcess(PROCESS_ALL_ACCESS, True, P.ProcessId);
; ; ; TerminateProcess(hh, 0);
; ; ; Application.MessageBox(PChar(Format('程序"%s"被禁止在本机运行,请与管理员联系!', [P.ExeFile])),
; ; ; ; '禁止运行', MB_ICONINFORMATION + MB_OK);
; ; end;
; ; ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
; end;

1、关闭当前应用程序的进程就行了。
2、对WIN2K等现代操作系统来说,是能完全释放一个进程所占用的资源的,所有不会有什么害
; ;倒是对应用程序本身不利,因为相当于是非正常退出。
3、你那个While循环有问题,经我改过以后就可以成功运行了,我的环境是Win2kAS + Delphi6

祝你好运。
 
多人接受答案了。
 
后退
顶部