请帮忙检查关闭进程的程序!(100分)

  • 主题发起人 主题发起人 sds
  • 开始时间 开始时间
S

sds

Unregistered / Unconfirmed
GUEST, unregistred user!
想通过循环找出已运行的“计算器”程序,并关闭其进程,<br>下面是我的程序,就是调试不好,请求帮助!<br><br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,TLHelp32,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; ListBox1: TListBox;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>type &nbsp;TProcessInfo = Record<br>&nbsp; &nbsp; ExeFile &nbsp; &nbsp;: String;<br>&nbsp; &nbsp; &nbsp;ProcessID &nbsp;: DWORD;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; pProcessInfo = ^TProcessInfo;<br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>p : pProcessInfo;<br>ContinueLoop:BOOL;<br>FSnapshotHandle:THandle;<br>FProcessEntry32:TProcessEntry32;<br>n:integer;<br>begin<br>n:=1;<br>FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);<br>&nbsp; FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);<br>&nbsp; ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);<br>&nbsp; while integer(ContinueLoop)&lt;&gt;0 do<br>&nbsp; begin<br>&nbsp; New(p);<br>&nbsp; p.ExeFile:= FProcessEntry32.szExeFile;<br>&nbsp; ListBox1.Items.Add(inttostr(n)+')'+inttostr(FProcessEntry32.th32ProcessID)+' &nbsp;* &nbsp;'+p.ExeFile);<br>&nbsp; inc(n);<br>&nbsp; &nbsp;if p.ExeFile='C:/WINDOWS/CALC.EXE' THEN TerminateProcess(FSnapshotHandle,0);<br>&nbsp;ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);<br>&nbsp; &nbsp; &nbsp;end;<br>end;<br><br>end.
 
以下的程序可以实现<br>var<br>&nbsp; HWndCalculator : HWnd;<br>begin<br>&nbsp; &nbsp;// find the exist calculator window<br>&nbsp; HWndCalculator := Winprocs.FindWindow(nil, '计算器'); // close the exist Calculator<br>&nbsp; if HWndCalculator &lt;&gt; 0 then <br>SendMessage(HWndCalculator, WM_CLOSE, 0, 0);<br>end;<br>在uses语句中加入Winprocs
 
sds:对你的程序做了小改动,如下:<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs,TLHelp32, &nbsp;StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; ListBox1: TListBox;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>type &nbsp;TProcessInfo = Record<br>&nbsp; &nbsp; ExeFile &nbsp; &nbsp;: String;<br>&nbsp; &nbsp; &nbsp;ProcessID &nbsp;: DWORD;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; pProcessInfo = ^TProcessInfo;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; p: pProcessInfo;<br>&nbsp; ContinueLoop: BOOL;<br>&nbsp; FSnapshotHandle, hProcess: THandle;<br>&nbsp; FProcessEntry32: TProcessEntry32;<br>&nbsp; n: integer;<br>begin<br>&nbsp; n:=1;<br>&nbsp; New(p);<br>&nbsp; FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);<br>&nbsp; FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);<br>&nbsp; ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);<br>&nbsp; while integer(ContinueLoop)&lt;&gt;0 do<br>&nbsp; begin<br>&nbsp; &nbsp; p.ExeFile:= FProcessEntry32.szExeFile;<br>&nbsp; &nbsp; ListBox1.Items.Add(inttostr(n)+':'+inttostr<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (FProcessEntry32. th32ProcessID)+' &nbsp;* &nbsp;'+p.ExeFile);<br>&nbsp; &nbsp; inc(n);<br>&nbsp; &nbsp; if p.ExeFile='C:/WINDOWS/CALC.EXE' then <br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; hProcess := OpenProcess(PROCESS_ALL_ACCESS, FALSE,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FProcessEntry32. th32ProcessID);<br>&nbsp; &nbsp; &nbsp; TerminateProcess(hProcess,0);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);<br>&nbsp; end;<br>end;<br><br>end.<br><br>为了阅读,加了几个回车,请你调试时删掉。主要的改动只有一处: hProcess;<br>不过我还是倾向于geshengping的方法(9x, NT都能用),而前者只有9x支持。 &nbsp;<br>
 
多人接受答案了。
 
后退
顶部