请问如何知道某一个程序在不在运行。就像windows任务管理器一样。请给出源代码(100分)

A

ajim

Unregistered / Unconfirmed
GUEST, unregistred user!
请问如何知道某一个程序在不在运行。就像windows任务管理器一样。请给出源代码
 
creation-zy前两天贴出来的,应该能够满足你的需求了。<br><br>{下面代码可以得到Windows 2000下面进程的ID以及文件名}<br>uses TLHelp32;<br>procedure TForm1.Button1Click(Sender: TObject);<br>&nbsp; procedure GetProcessInfo;<br>&nbsp; var<br>&nbsp; &nbsp; tw: Word;<br>&nbsp; &nbsp; ts: array[0..MAX_PATH] of char;<br>&nbsp; var<br>&nbsp; &nbsp; i: Integer;<br>&nbsp; &nbsp; snap: THandle;<br>&nbsp; &nbsp; pe32: TPROCESSENTRY32;<br>&nbsp; begin<br>&nbsp; &nbsp; snap := 0;<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; snap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);<br>&nbsp; &nbsp; &nbsp; if snap &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; pe32.dwSize := SizeOf(TPROCESSENTRY32);<br>&nbsp; &nbsp; &nbsp; &nbsp; if Process32First(snap, pe32) then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ListBox1.Items.Add(pe32.szExeFile + ' &nbsp;' + IntToStr(pe32.th32ProcessID));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while Process32Next(snap, pe32) do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ListBox1.Items.Add(pe32.szExeFile + ' &nbsp;' + IntToStr(pe32.th32ProcessID));<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; CloseHandle(snap);<br>&nbsp; &nbsp; end;<br>&nbsp; End;<br><br>begin<br>&nbsp; GetProcessInfo;<br>end;
 
再问一下。我确认他在运行,要得到他的THandle;<br>该怎么做??
 
参考下面的例子:<br>var<br>w1,w2,w3:integer;<br>implementation<br><br>{$R *.DFM}<br><br>function enumwinproc(handle:hwnd;param:longint):boolean;<br>stdcall;<br>var<br>sz:array[0..132] of char;<br>begin<br>result:=true;<br>if getwindowtext(handle,sz,sizeof(sz))&lt;&gt;0 then<br>begin<br>&nbsp; &nbsp; &nbsp; if strpas(sz)='appclose' then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if w1=0 then w1:=handle else w2:=handle;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>end;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>//判断当前程序是否只有一个在运行<br>w1:=0;w3:=0;<br>enumwindows(@enumwinproc,0);<br>if (w1&lt;&gt;0) and (w2&lt;&gt;0) then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; w3:=1;//判断是否在FormDestroy出现警告框的标记,等于1则不出现<br>&nbsp; &nbsp; &nbsp; &nbsp; sendmessage(w2,wM_close,0,0);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>end;<br>procedure TForm1.FormDestroy(Sender: TObject);<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp;if w3&lt;&gt;1 then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; application.messagebox('关闭','警告',48);<br>&nbsp; &nbsp; &nbsp; &nbsp; end; &nbsp; &nbsp; &nbsp; <br>end;<br><br>end.<br>
 
to forgot2002, 你的的确能得到thandle.但问题是我要从文件名上检测这一点.问题仍然没解决<br>如asde.exe正在运行 我怎么知道她的thandle?
 
如果你有ProcessID或者ProcessHandle的话可以用GetProcessTimes;<br>判断结束时间是不是0,是0就在运行,不是就说明已结束
 
to angle, <br>你好像说错了吧<br>有ProcessID或者ProcessHandle那就是一定在运行了。那还有必要确认他在运行马?<br>就如同明知道你是男人还问你你是男人吗?:)好可笑
 
同志,你有时间不妨看看Delphi关于GetProcessTimes的说明就明白了。
 
顶部