如何判断某一进程是否结束 ( 积分: 100 )

  • 主题发起人 主题发起人 zqm999a
  • 开始时间 开始时间
Z

zqm999a

Unregistered / Unconfirmed
GUEST, unregistred user!
这一进程就别人提供的应用程序,由我触发,我要判断这个进程是否结束,先谢了<br>
 
这一进程就别人提供的应用程序,由我触发,我要判断这个进程是否结束,先谢了<br>
 
如果这个程序是自动退出的,就找它的窗口句柄,如果不带窗口的,就直接找进程名。下面的程序给你参考:<br><br>话题915786的标题是: 如何列出所有运行程序进程,并可以杀掉指定的进程,1000分在等各位高手啊! (300分)<br>分类:系统相关 壹平 (2002-02-08 14:45:00) &nbsp;<br>如何列出所有运行程序进程,并可以杀掉指定的进程,1000分在等各位高手啊!<br><br>delphi 和 C++B 的都可以! <br> <br>honestman (2002-02-08 14:48:00) &nbsp;<br>翻翻旧贴,N年前就已经讨论过了。 <br> <br>zlj555 (2002-02-08 15:00:00) &nbsp;<br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Classes, Graphics, TLHelp32, Controls, Forms, Dialogs,<br> &nbsp;StdCtrls;<br><br>type<br> &nbsp;TProcessInfo = record<br> &nbsp; &nbsp;ExeFile: string;<br> &nbsp; &nbsp;ProcessId: DWORD;<br> &nbsp;end;<br> &nbsp;ProcessInfo = ^TProcessInfo;<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;ListBox1: TListBox;<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;Button2: TButton;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure ProcessList(var pList: TList);<br> &nbsp; &nbsp;procedure My_RunFileScan(ListboxRunFile: TListBox);<br> &nbsp; &nbsp;procedure Button2Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;Current: TList;<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.ProcessList(var pList: TList);<br>var<br> &nbsp;p: ProcessInfo;<br> &nbsp;ok: Bool;<br> &nbsp;ProcessListHandle: THandle;<br> &nbsp;ProcessStruct: TProcessEntry32;<br>begin<br> &nbsp;PList := TList.Create;<br> &nbsp;PList.Clear;<br> &nbsp;ProcessListHandle := CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0);<br> &nbsp;ProcessStruct.dwSize := Sizeof(ProcessStruct);<br> &nbsp;ok := Process32First(ProcessListHandle, ProcessStruct);<br> &nbsp;while Integer(ok) &amp;lt;&amp;gt; 0 do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;new(p);<br> &nbsp; &nbsp; &nbsp;p.ExeFile := ProcessStruct.szExeFile;<br> &nbsp; &nbsp; &nbsp;p.ProcessID := ProcessStruct.th32ProcessID;<br> &nbsp; &nbsp; &nbsp;PList.Add(p);<br> &nbsp; &nbsp; &nbsp;ok := Process32Next(ProcessListHandle, ProcessStruct);<br> &nbsp; &nbsp;end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;h: THandle;<br> &nbsp;a: DWORD;<br> &nbsp;p: PRocessInfo;<br>begin<br> &nbsp;if ListBox1.ItemIndex &amp;gt;= 0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;p := Current.Items[ListBox1.ItemIndex];<br> &nbsp; &nbsp; &nbsp;h := openProcess(Process_All_Access, true, p.ProcessID);<br> &nbsp; &nbsp; &nbsp;GetExitCodeProcess(h, a);<br><br> &nbsp; &nbsp; &nbsp;if Integer(TerminateProcess(h, a)) &amp;lt;&amp;gt; 0 then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;My_RunFileScan(ListBox1);<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end<br> &nbsp;else<br> &nbsp; &nbsp;Application.MessageBox('请先选择一个进程!', '黑洞', MB_ICONERROR + MB_OK);<br>end;<br><br>procedure TForm1.My_RunFileScan(ListboxRunFile: TListBox);<br>var<br> &nbsp;i: Integer;<br> &nbsp;p: PRocessInfo;<br>begin<br> &nbsp;current := TList.Create;<br> &nbsp;Current.Clear;<br> &nbsp;ListboxRunFile.Clear;<br> &nbsp;ProcessList(Current);<br> &nbsp;for i := 0 to Current.Count - 1 do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;new(p);<br> &nbsp; &nbsp; &nbsp;p := Current.Items;<br> &nbsp; &nbsp; &nbsp;ListboxRunFile.Items.Add(p.ExeFile);<br> &nbsp; &nbsp;end;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> &nbsp;My_RunFileScan(ListBox1);<br>end;<br><br>end.<br><br> <br>荷塘新月 (2002-02-08 15:09:00) &nbsp;<br>我有一个程序,专门用来查看、清除程序,DELPHI5下编译通过。 <br> <br>壹平 (2002-02-08 15:48:00) &nbsp;<br>荷塘新月:把源代码放出来看看! <br> <br>壹平 (2002-02-08 16:01:00) &nbsp;<br>zlj555:<br><br> &nbsp;ProcessStruct: TProcessEntry32;<br><br>编译不同过~~~!!!!!!!!!! <br> <br>yyxhcf (2002-02-08 16:12:00) &nbsp;<br>我有一个。来自一个远程控制程序的。<br>列出所有进程,可以正常关闭进程,也可以强制杀死进程。<br>远程控制计算机的哦,在上网时要取对方资料易如反掌,对方做什么看得清清楚楚。<br>还有很多很多功能。。。。程序大小只有100多K。<br>经我修改后非常厉害,除了NT,否则用户根本找不到杀不死,哈哈。<br>自编的程序,所有反病毒工具无效。<br>价值不只1000分,呵呵。春节没空了,以后再给。 <br> <br>壹平 (2002-02-08 16:42:00) &nbsp;<br>yyxhcf,能否发给我,谢谢! <br> <br>qq74 (2002-02-08 17:04:00) &nbsp;<br>能否给我一份,谢谢!jw_yan@163.com <br> <br>shiner (2002-02-08 17:41:00) &nbsp;<br>到这里来,有源码的啊.http://delphi.7i24.com <br> <br>bubble (2002-02-08 19:49:00) &nbsp;<br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls;<br><br>type<br> TForm1 = class(TForm)<br> &nbsp; Button1: TButton;<br> &nbsp; ListBox1: TListBox;<br> &nbsp; &nbsp;Label1: TLabel;<br> &nbsp; &nbsp;Button2: TButton;<br> &nbsp; procedure Button1Click(Sender: TObject);<br> &nbsp; procedure ListBox1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure Button2Click(Sender: TObject);<br> private<br> &nbsp; { Private declarations }<br> public<br> &nbsp; { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br> &nbsp;Wnd: HWND;<br>implementation<br><br>{$R *.DFM}<br><br>Function EnumWindowsProc (Wnd: HWND; lb: TListbox): BOOL; stdcall;<br>var<br> caption: Array [0..128] of Char;<br>begin<br> Result := True;<br> if { skip invisible windows }<br> &nbsp; &nbsp;IsWindowVisible(Wnd) and<br> &nbsp; &nbsp;{ only process truly top-level windows. GetWindowLong must be used, not<br>GetParent }<br> &nbsp; &nbsp;((GetWindowLong(Wnd, GWL_HWNDPARENT) = 0) or<br> &nbsp; &nbsp; (HWND(GetWindowLong(Wnd, GWL_HWNDPARENT)) = GetDesktopWindow)) and<br> &nbsp; &nbsp;{ skip WS_EX_TOOLWINDOW windows }<br> &nbsp; &nbsp;((GetWindowLong(Wnd, GWL_EXSTYLE) and WS_EX_TOOLWINDOW) = 0)<br> then begin<br> &nbsp; SendMessage( Wnd, WM_GETTEXT, Sizeof( caption ), integer(@caption));<br> &nbsp; lb.Items.AddObject( caption, TObject( Wnd ));<br> end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> listbox1.clear;<br> EnumWindows( @EnumWindowsProc, integer( listbox1 ));<br>end;<br><br>procedure TForm1.ListBox1Click(Sender: TObject);<br>var<br> theClassname: Array [0..128] of Char;<br><br> tid, pid: DWORD;<br> intExitCode:DWORD;<br>begin<br> With Sender As TListbox Do Begin<br> &nbsp; If ItemIndex &amp;gt;= 0 Then Begin<br> &nbsp; &nbsp; Wnd:= HWND(Items.Objects[ itemindex ]);<br> &nbsp; &nbsp; If Wnd &amp;lt;&amp;gt; 0 Then Begin<br> &nbsp; &nbsp; &nbsp; Windows.GetClassname( Wnd, theClassname, Sizeof( classname ));<br> &nbsp; &nbsp; &nbsp; tid := GetWindowThreadProcessID( Wnd, @pid );<br> &nbsp; &nbsp; &nbsp; label1.caption :=<br> &nbsp; &nbsp; &nbsp; &nbsp; Format(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'HWND: %8.8x'#13#10+<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Class: %s'#13#10+<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Process ID: %8.8x'#13#10+<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Thread ID: %8.8x',<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [Wnd, theClassname, pid, tid] );<br> &nbsp; &nbsp; End;<br> &nbsp; End;<br> End;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> &nbsp;PostMessage(Wnd, WM_CLOSE, 0, 0);<br>end;<br><br><br>end. <br> <br>snail_kbb (2002-02-09 11:22:00) &nbsp;<br>yyxhcf,也给我一个,谢谢 <br> <br>blue_winds (2002-02-09 13:00:00) &nbsp;<br>不对啊<br>我调试了 &nbsp;label1.caption不显示 button2不执行啊<br><br> <br>董海滨 (2002-11-08 11:55:00) &nbsp;<br>yyxhcf,也给我一个,谢谢<br>seawater001@yeah.net <br> <br>chillinson (2004-03-06 16:28:19) &nbsp;<br>zlj555 的代码没有问题,<br>问题是你在程序uses区没有加TLHelp32,加上就OK.
 
等待计算器结束的例子<br>var<br> &nbsp;StartupInfo:TStartupInfo;<br> &nbsp;ProcessInfo:TProcessInformation;<br>begin<br> &nbsp;FillChar(StartupInfo,SizeOf(StartupInfo),#0);<br> &nbsp;StartupInfo.cb:=SizeOf(StartupInfo);<br> &nbsp;StartupInfo.dwFlags:=STARTF_USESHOWWINDOW;<br> &nbsp;StartupInfo.wShowWindow:=sw_show;<br> &nbsp;CreateProcess(nil,'calc.exe',nil,nil,false,Create_new_console or Normal_priority_class,nil,nil,StartupInfo,ProcessInfo);<br> &nbsp;WaitForsingleobject(ProcessInfo.hProcess, INFINITE);<br>end;
 
上面讲的很详细,受教育了!楼主放分吧
 
后退
顶部